Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created March 17, 2010 19:21
Show Gist options
  • Save isaacs/335623 to your computer and use it in GitHub Desktop.
Save isaacs/335623 to your computer and use it in GitHub Desktop.
APACHE:
$ ab -k -c 50 -n 20000 http://127.0.0.1/statread.js 2>&1 | grep "Requests per second"
Requests per second: 12370.69 [#/sec] (mean)
NGINX:
$ ab -k -c 50 -n 20000 http://127.0.0.1/statread.js 2>&1 | grep "Requests per second"
Requests per second: 20262.73 [#/sec] (mean)
NODE:
$ ab -k -c 50 -n 20000 http://127.0.0.1:8000/statread.js 2>&1 | grep "Requests per second"
Requests per second: 13687.51 [#/sec] (mean)
var sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
cache = {},
CACHE_TIMEOUT = 200,
DOCROOT = process.cwd(),
PORT = 8000;
function fail (response, code, msg) {
if (!code) code = 404;
if (!msg) msg = http.STATUS_CODES[code] || "unknown error";
msg = code + " " + msg
response.writeHead(code,
{"content-type": "text/plain", "content-length":msg.length});
response.write(msg);
response.close();
}
function ok (response, data) {
response.writeHead(200, {"content-length":data.length});
response.write(data);
response.close();
}
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
var filename = path.join(DOCROOT, path.normalize(uri));
if (filename in cache) {
var cached = cache[filename];
if (cached.time < Date.now() + CACHE_TIMEOUT) {
if (cached.data === false) return fail(response);
else return ok(response, cached.data);
}
}
fs.stat(filename, function (er, newstat) {
if (er) {
cache[filename] = {time:Date.now(), data:false};
return fail(response);
}
if (cached && cached.time >= newstat.mtime.getTime()) {
return ok(response, cached.data);
}
fs.readFile(filename, function (er, data) {
if (er) return fail(response, 500, er.stack);
cache[filename] = { time:newstat.mtime.getTime(), data:data };
return ok(response, data);
});
});
}).listen(PORT);
sys.puts("Server running at http://localhost:"+PORT+"/");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment