Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
bench: backport http_simple benchmark from master
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Oct 29, 2011
1 parent ff942c6 commit 7fd1c08
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions benchmark/http_simple.js
Expand Up @@ -11,26 +11,6 @@ for (var i = 0; i < 20*1024; i++) {
fixed += "C";
}

var uname, rev;

exec('git rev-list -1 HEAD', function (e, stdout) {
if (e) {
console.error("Problem executing: 'git rev-list -1 HEAD'");
throw new Error(e);
}
rev = stdout.replace(/\s/g, '');
});

exec('uname -a', function (e, stdout) {
if (e) {
console.error("Problem executing: 'uname -a'");
throw new Error(e);
}
uname = stdout.replace(/[\r\n]/g, '');
});



stored = {};
storedBuffer = {};

Expand All @@ -39,6 +19,7 @@ var server = http.createServer(function (req, res) {
var command = commands[1];
var body = "";
var arg = commands[2];
var n_chunks = parseInt(commands[3], 10);
var status = 200;

if (command == "bytes") {
Expand Down Expand Up @@ -73,23 +54,39 @@ var server = http.createServer(function (req, res) {
} else if (command == "fixed") {
body = fixed;

} else if (command == "info") {
body = 'rev=' + rev + '\nuname="' + uname + '"\n';

} else {
status = 404;
body = "not found\n";
}

var content_length = body.length.toString();
// 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.writeHead(status, { "Content-Type": "text/plain",
"Content-Length": content_length });
res.end(body);
res.end();
} else {
var content_length = body.length.toString();

res.writeHead(status, { "Content-Type": "text/plain",
"Content-Length": content_length });
res.end(body);
}

});

server.listen(port, function () {
console.log('Listening at http://127.0.0.1:'+port+'/');
});

process.on('exit', function() {
console.error('libuv counters', process.uvCounters());
});

0 comments on commit 7fd1c08

Please sign in to comment.