Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bench: make number of response body chunks configurable in http_simple

  • Loading branch information...
commit 63607a0304e99259d3b7a24c44f7d29384b93cb4 1 parent 8e8f36f
@bnoordhuis bnoordhuis authored
Showing with 14 additions and 4 deletions.
  1. +14 −4 benchmark/http_simple.js
View
18 benchmark/http_simple.js
@@ -39,7 +39,7 @@ var server = http.createServer(function (req, res) {
var command = commands[1];
var body = "";
var arg = commands[2];
- var chunked = (commands[3] === "chunked");
+ var n_chunks = parseInt(commands[3], 10);
var status = 200;
if (command == "bytes") {
@@ -82,18 +82,28 @@ var server = http.createServer(function (req, res) {
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",
"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 {
var content_length = body.length.toString();
res.writeHead(status, { "Content-Type": "text/plain",
"Content-Length": content_length });
+ res.end(body);
}
- res.end(body);
-
});
server.listen(port, function () {
Please sign in to comment.
Something went wrong with that request. Please try again.