benchmark: add http header setting scenarios
PR-URL: https://github.com/nodejs/node/pull/10558 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
This commit is contained in:
parent
fa3836152d
commit
f53a6fb48b
@ -4,10 +4,10 @@ var http = require('http');
|
|||||||
|
|
||||||
var port = parseInt(process.env.PORT || 8000);
|
var port = parseInt(process.env.PORT || 8000);
|
||||||
|
|
||||||
var fixed = 'C'.repeat(20 * 1024),
|
var fixed = 'C'.repeat(20 * 1024);
|
||||||
storedBytes = {},
|
var storedBytes = Object.create(null);
|
||||||
storedBuffer = {},
|
var storedBuffer = Object.create(null);
|
||||||
storedUnicode = {};
|
var storedUnicode = Object.create(null);
|
||||||
|
|
||||||
var useDomains = process.env.NODE_USE_DOMAINS;
|
var useDomains = process.env.NODE_USE_DOMAINS;
|
||||||
|
|
||||||
@ -29,11 +29,13 @@ var server = module.exports = http.createServer(function(req, res) {
|
|||||||
dom.add(res);
|
dom.add(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
var commands = req.url.split('/');
|
// URL format: /<type>/<length>/<chunks>/<responseBehavior>
|
||||||
var command = commands[1];
|
var params = req.url.split('/');
|
||||||
|
var command = params[1];
|
||||||
var body = '';
|
var body = '';
|
||||||
var arg = commands[2];
|
var arg = params[2];
|
||||||
var n_chunks = parseInt(commands[3], 10);
|
var n_chunks = parseInt(params[3], 10);
|
||||||
|
var resHow = (params.length >= 5 ? params[4] : 'normal');
|
||||||
var status = 200;
|
var status = 200;
|
||||||
|
|
||||||
var n, i;
|
var n, i;
|
||||||
@ -45,7 +47,6 @@ var server = module.exports = http.createServer(function(req, res) {
|
|||||||
storedBytes[n] = 'C'.repeat(n);
|
storedBytes[n] = 'C'.repeat(n);
|
||||||
}
|
}
|
||||||
body = storedBytes[n];
|
body = storedBytes[n];
|
||||||
|
|
||||||
} else if (command === 'buffer') {
|
} else if (command === 'buffer') {
|
||||||
n = ~~arg;
|
n = ~~arg;
|
||||||
if (n <= 0)
|
if (n <= 0)
|
||||||
@ -57,7 +58,6 @@ var server = module.exports = http.createServer(function(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
body = storedBuffer[n];
|
body = storedBuffer[n];
|
||||||
|
|
||||||
} else if (command === 'unicode') {
|
} else if (command === 'unicode') {
|
||||||
n = ~~arg;
|
n = ~~arg;
|
||||||
if (n <= 0)
|
if (n <= 0)
|
||||||
@ -66,23 +66,30 @@ var server = module.exports = http.createServer(function(req, res) {
|
|||||||
storedUnicode[n] = '\u263A'.repeat(n);
|
storedUnicode[n] = '\u263A'.repeat(n);
|
||||||
}
|
}
|
||||||
body = storedUnicode[n];
|
body = storedUnicode[n];
|
||||||
|
|
||||||
} else if (command === 'quit') {
|
} else if (command === 'quit') {
|
||||||
res.connection.server.close();
|
res.connection.server.close();
|
||||||
body = 'quitting';
|
body = 'quitting';
|
||||||
|
|
||||||
} else if (command === 'fixed') {
|
} else if (command === 'fixed') {
|
||||||
body = fixed;
|
body = fixed;
|
||||||
|
|
||||||
} else if (command === 'echo') {
|
} else if (command === 'echo') {
|
||||||
const headers = {
|
switch (resHow) {
|
||||||
|
case 'setHeader':
|
||||||
|
res.statusCode = 200;
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
res.setHeader('Transfer-Encoding', 'chunked');
|
||||||
|
break;
|
||||||
|
case 'setHeaderWH':
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
res.writeHead(200, { 'Transfer-Encoding': 'chunked' });
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.writeHead(200, {
|
||||||
'Content-Type': 'text/plain',
|
'Content-Type': 'text/plain',
|
||||||
'Transfer-Encoding': 'chunked'
|
'Transfer-Encoding': 'chunked'
|
||||||
};
|
});
|
||||||
res.writeHead(200, headers);
|
}
|
||||||
req.pipe(res);
|
req.pipe(res);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
status = 404;
|
status = 404;
|
||||||
body = 'not found\n';
|
body = 'not found\n';
|
||||||
@ -91,11 +98,22 @@ var server = module.exports = http.createServer(function(req, res) {
|
|||||||
// example: http://localhost:port/bytes/512/4
|
// example: http://localhost:port/bytes/512/4
|
||||||
// sends a 512 byte body in 4 chunks of 128 bytes
|
// sends a 512 byte body in 4 chunks of 128 bytes
|
||||||
if (n_chunks > 0) {
|
if (n_chunks > 0) {
|
||||||
const headers = {
|
switch (resHow) {
|
||||||
|
case 'setHeader':
|
||||||
|
res.statusCode = status;
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
res.setHeader('Transfer-Encoding', 'chunked');
|
||||||
|
break;
|
||||||
|
case 'setHeaderWH':
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
res.writeHead(status, { 'Transfer-Encoding': 'chunked' });
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.writeHead(status, {
|
||||||
'Content-Type': 'text/plain',
|
'Content-Type': 'text/plain',
|
||||||
'Transfer-Encoding': 'chunked'
|
'Transfer-Encoding': 'chunked'
|
||||||
};
|
});
|
||||||
res.writeHead(status, headers);
|
}
|
||||||
// send body in chunks
|
// send body in chunks
|
||||||
var len = body.length;
|
var len = body.length;
|
||||||
var step = Math.floor(len / n_chunks) || 1;
|
var step = Math.floor(len / n_chunks) || 1;
|
||||||
@ -105,12 +123,22 @@ var server = module.exports = http.createServer(function(req, res) {
|
|||||||
}
|
}
|
||||||
res.end(body.slice((n_chunks - 1) * step));
|
res.end(body.slice((n_chunks - 1) * step));
|
||||||
} else {
|
} else {
|
||||||
const headers = {
|
switch (resHow) {
|
||||||
|
case 'setHeader':
|
||||||
|
res.statusCode = status;
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
res.setHeader('Content-Length', body.length.toString());
|
||||||
|
break;
|
||||||
|
case 'setHeaderWH':
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
res.writeHead(status, { 'Content-Length': body.length.toString() });
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.writeHead(status, {
|
||||||
'Content-Type': 'text/plain',
|
'Content-Type': 'text/plain',
|
||||||
'Content-Length': body.length.toString()
|
'Content-Length': body.length.toString()
|
||||||
};
|
});
|
||||||
|
}
|
||||||
res.writeHead(status, headers);
|
|
||||||
res.end(body);
|
res.end(body);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -7,14 +7,16 @@ var bench = common.createBenchmark(main, {
|
|||||||
type: ['bytes', 'buffer'],
|
type: ['bytes', 'buffer'],
|
||||||
length: [4, 1024, 102400],
|
length: [4, 1024, 102400],
|
||||||
chunks: [0, 1, 4], // chunks=0 means 'no chunked encoding'.
|
chunks: [0, 1, 4], // chunks=0 means 'no chunked encoding'.
|
||||||
c: [50, 500]
|
c: [50, 500],
|
||||||
|
res: ['normal', 'setHeader', 'setHeaderWH']
|
||||||
});
|
});
|
||||||
|
|
||||||
function main(conf) {
|
function main(conf) {
|
||||||
process.env.PORT = PORT;
|
process.env.PORT = PORT;
|
||||||
var server = require('./_http_simple.js');
|
var server = require('./_http_simple.js');
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
var path = '/' + conf.type + '/' + conf.length + '/' + conf.chunks;
|
var path = '/' + conf.type + '/' + conf.length + '/' + conf.chunks + '/' +
|
||||||
|
conf.res;
|
||||||
|
|
||||||
bench.http({
|
bench.http({
|
||||||
path: path,
|
path: path,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user