Browse Source

bench: make number of response body chunks configurable in http_simple

v0.7.4-release
Ben Noordhuis 13 years ago
parent
commit
63607a0304
  1. 18
      benchmark/http_simple.js

18
benchmark/http_simple.js

@ -39,7 +39,7 @@ var server = http.createServer(function (req, res) {
var command = commands[1]; var command = commands[1];
var body = ""; var body = "";
var arg = commands[2]; var arg = commands[2];
var chunked = (commands[3] === "chunked"); var n_chunks = parseInt(commands[3], 10);
var status = 200; var status = 200;
if (command == "bytes") { if (command == "bytes") {
@ -82,18 +82,28 @@ var server = http.createServer(function (req, res) {
body = "not found\n"; body = "not found\n";
} }
if (chunked) { // example: http://localhost:port/bytes/512/4
// sends a 512 byte body in 4 chunks of 128 bytes
if (n_chunks > 0) {
res.writeHead(status, { "Content-Type": "text/plain", res.writeHead(status, { "Content-Type": "text/plain",
"Transfer-Encoding": "chunked" }); "Transfer-Encoding": "chunked" });
// send body in chunks
var len = body.length;
var step = ~~(len / n_chunks) || len;
for (var i = 0; i < len; i += step) {
res.write(body.slice(i, i + step));
}
res.end();
} else { } else {
var content_length = body.length.toString(); var content_length = body.length.toString();
res.writeHead(status, { "Content-Type": "text/plain", res.writeHead(status, { "Content-Type": "text/plain",
"Content-Length": content_length }); "Content-Length": content_length });
res.end(body);
} }
res.end(body);
}); });
server.listen(port, function () { server.listen(port, function () {

Loading…
Cancel
Save