Skip to content

Instantly share code, notes, and snippets.

@philipmat
Created October 9, 2011 15:38
Show Gist options
  • Save philipmat/1273810 to your computer and use it in GitHub Desktop.
Save philipmat/1273810 to your computer and use it in GitHub Desktop.
Benchmarking node
var MAX = 1E8;
function out(i, final, res) {
res.end(i + "+1 " + MAX + " times = " + final + "\n");
}
var inc_sync = function (i, callback) {
var k = 0, x = i;
for(var k = 0; k < MAX; k++)
x += 1;
callback(i, x);
}
var inc_async = function(i, callback) {
var inner = function(k, x) {
if (k >= MAX) {
callback(i, x);
return;
}
var max = Math.max(MAX, k+100);
for(var l = k; l < max; l++, k++) {
x += 1;
}
process.nextTick(function() { inner(k, x); });
}
inner(0,i);
}
module.exports = {
'sync' : function(req, res) {
inc_sync(1, function(i, final) {
out(i, final, res);
});
},
'async' : function(req, res) {
//xinc1(parseInt(req.params.id), function(i, final) {
inc_async(1, function(i, final) {
out(i, final, res);
});
},
}
var http = require('http');
var PORT = 1337;
var Tests = {};
load = function(name) {
if (typeof Tests[name] !== 'undefined') return Tests[name];
var x = Tests[name] = require('./' + name);
return x;
}
loadTest = function(module, testName) {
var tests = load(module);
//console.log(tests);
var test = tests[testName];
//console.log(test);
return test;
}
http.createServer( function (req, res) {
//console.log(req);
var params = req.url.slice(1).split('.');
var test = loadTest(params[0], params[1]);
res.writeHead(200, {'Content-Type' : 'text/plain' });
test(req, res);
}).listen(PORT);
console.log('Server running on port %d.', PORT);
{
"name": "nodebench"
, "version": "0.0.1"
, "private": true
, "dependencies": { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment