Skip to content

Commit f53a6fb

Browse files
committed
benchmark: add http header setting scenarios
PR-URL: #10558 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent fa38361 commit f53a6fb

File tree

2 files changed

+62
-32
lines changed

2 files changed

+62
-32
lines changed

benchmark/http/_http_simple.js

+58-30
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ var http = require('http');
44

55
var port = parseInt(process.env.PORT || 8000);
66

7-
var fixed = 'C'.repeat(20 * 1024),
8-
storedBytes = {},
9-
storedBuffer = {},
10-
storedUnicode = {};
7+
var fixed = 'C'.repeat(20 * 1024);
8+
var storedBytes = Object.create(null);
9+
var storedBuffer = Object.create(null);
10+
var storedUnicode = Object.create(null);
1111

1212
var useDomains = process.env.NODE_USE_DOMAINS;
1313

@@ -29,11 +29,13 @@ var server = module.exports = http.createServer(function(req, res) {
2929
dom.add(res);
3030
}
3131

32-
var commands = req.url.split('/');
33-
var command = commands[1];
32+
// URL format: /<type>/<length>/<chunks>/<responseBehavior>
33+
var params = req.url.split('/');
34+
var command = params[1];
3435
var body = '';
35-
var arg = commands[2];
36-
var n_chunks = parseInt(commands[3], 10);
36+
var arg = params[2];
37+
var n_chunks = parseInt(params[3], 10);
38+
var resHow = (params.length >= 5 ? params[4] : 'normal');
3739
var status = 200;
3840

3941
var n, i;
@@ -45,7 +47,6 @@ var server = module.exports = http.createServer(function(req, res) {
4547
storedBytes[n] = 'C'.repeat(n);
4648
}
4749
body = storedBytes[n];
48-
4950
} else if (command === 'buffer') {
5051
n = ~~arg;
5152
if (n <= 0)
@@ -57,7 +58,6 @@ var server = module.exports = http.createServer(function(req, res) {
5758
}
5859
}
5960
body = storedBuffer[n];
60-
6161
} else if (command === 'unicode') {
6262
n = ~~arg;
6363
if (n <= 0)
@@ -66,23 +66,30 @@ var server = module.exports = http.createServer(function(req, res) {
6666
storedUnicode[n] = '\u263A'.repeat(n);
6767
}
6868
body = storedUnicode[n];
69-
7069
} else if (command === 'quit') {
7170
res.connection.server.close();
7271
body = 'quitting';
73-
7472
} else if (command === 'fixed') {
7573
body = fixed;
76-
7774
} else if (command === 'echo') {
78-
const headers = {
79-
'Content-Type': 'text/plain',
80-
'Transfer-Encoding': 'chunked'
81-
};
82-
res.writeHead(200, headers);
75+
switch (resHow) {
76+
case 'setHeader':
77+
res.statusCode = 200;
78+
res.setHeader('Content-Type', 'text/plain');
79+
res.setHeader('Transfer-Encoding', 'chunked');
80+
break;
81+
case 'setHeaderWH':
82+
res.setHeader('Content-Type', 'text/plain');
83+
res.writeHead(200, { 'Transfer-Encoding': 'chunked' });
84+
break;
85+
default:
86+
res.writeHead(200, {
87+
'Content-Type': 'text/plain',
88+
'Transfer-Encoding': 'chunked'
89+
});
90+
}
8391
req.pipe(res);
8492
return;
85-
8693
} else {
8794
status = 404;
8895
body = 'not found\n';
@@ -91,11 +98,22 @@ var server = module.exports = http.createServer(function(req, res) {
9198
// example: http://localhost:port/bytes/512/4
9299
// sends a 512 byte body in 4 chunks of 128 bytes
93100
if (n_chunks > 0) {
94-
const headers = {
95-
'Content-Type': 'text/plain',
96-
'Transfer-Encoding': 'chunked'
97-
};
98-
res.writeHead(status, headers);
101+
switch (resHow) {
102+
case 'setHeader':
103+
res.statusCode = status;
104+
res.setHeader('Content-Type', 'text/plain');
105+
res.setHeader('Transfer-Encoding', 'chunked');
106+
break;
107+
case 'setHeaderWH':
108+
res.setHeader('Content-Type', 'text/plain');
109+
res.writeHead(status, { 'Transfer-Encoding': 'chunked' });
110+
break;
111+
default:
112+
res.writeHead(status, {
113+
'Content-Type': 'text/plain',
114+
'Transfer-Encoding': 'chunked'
115+
});
116+
}
99117
// send body in chunks
100118
var len = body.length;
101119
var step = Math.floor(len / n_chunks) || 1;
@@ -105,12 +123,22 @@ var server = module.exports = http.createServer(function(req, res) {
105123
}
106124
res.end(body.slice((n_chunks - 1) * step));
107125
} else {
108-
const headers = {
109-
'Content-Type': 'text/plain',
110-
'Content-Length': body.length.toString()
111-
};
112-
113-
res.writeHead(status, headers);
126+
switch (resHow) {
127+
case 'setHeader':
128+
res.statusCode = status;
129+
res.setHeader('Content-Type', 'text/plain');
130+
res.setHeader('Content-Length', body.length.toString());
131+
break;
132+
case 'setHeaderWH':
133+
res.setHeader('Content-Type', 'text/plain');
134+
res.writeHead(status, { 'Content-Length': body.length.toString() });
135+
break;
136+
default:
137+
res.writeHead(status, {
138+
'Content-Type': 'text/plain',
139+
'Content-Length': body.length.toString()
140+
});
141+
}
114142
res.end(body);
115143
}
116144
});

benchmark/http/simple.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ var bench = common.createBenchmark(main, {
77
type: ['bytes', 'buffer'],
88
length: [4, 1024, 102400],
99
chunks: [0, 1, 4], // chunks=0 means 'no chunked encoding'.
10-
c: [50, 500]
10+
c: [50, 500],
11+
res: ['normal', 'setHeader', 'setHeaderWH']
1112
});
1213

1314
function main(conf) {
1415
process.env.PORT = PORT;
1516
var server = require('./_http_simple.js');
1617
setTimeout(function() {
17-
var path = '/' + conf.type + '/' + conf.length + '/' + conf.chunks;
18+
var path = '/' + conf.type + '/' + conf.length + '/' + conf.chunks + '/' +
19+
conf.res;
1820

1921
bench.http({
2022
path: path,

0 commit comments

Comments
 (0)