Skip to content

Instantly share code, notes, and snippets.

@KOBA789
Created November 16, 2011 15:36
Show Gist options
  • Select an option

  • Save KOBA789/1370376 to your computer and use it in GitHub Desktop.

Select an option

Save KOBA789/1370376 to your computer and use it in GitHub Desktop.
NodeBench
var http = require('http');
var maxConnections = 1000, maxRequests = 10000;
var connections = 0, requests = 0, errors = 0, done = 0;
var options = {
host: 'target',
port: 80,
path: '/'
};
function canRequest () {
return connections < maxConnections && requests < maxRequests;
}
function showResult () {
console.log('Total: ', requests);
console.log('Error: ', errors);
console.timeEnd('timer');
}
console.time('timer');
(function perform() {
if (canRequest()) {
process.nextTick(perform);
} else {
return;
}
requests ++;
connections ++;
http.get(options, function (res) {
done ++;
connections --;
/*
process.nextTick(function () {
console.log(requests, connections, done);
});
*/
if (done >= maxRequests) {
showResult();
}
process.nextTick(perform);
res.setEncoding('utf8');
res.on('data', function (chunk) {
/* NOP */
});
}).on('error', function (e) {
errors ++;
console.log(e.message);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment