Skip to content

Instantly share code, notes, and snippets.

@mbrevoort
Created February 19, 2012 02:57
Show Gist options
  • Save mbrevoort/1861747 to your computer and use it in GitHub Desktop.
Save mbrevoort/1861747 to your computer and use it in GitHub Desktop.
Simple ab style load generator
// requires request, measured, optimist and microtime npm modules
var util = require('util')
, request = require('request')
, microtime = require('microtime')
, measured = require('measured')
, collection = new measured.Collection('http')
, argv = require('optimist').usage('node load.js -c [concurrent] -n [total] url').argv;
var c = argv.c || 1
, n = argv.n || 1, uri = argv._[0]
, running = 0, ran=0, finished=0
, timer = collection.timer(uri);
var statusInterval = setInterval(function() {
console.log([running, ran, finished, (Math.round(100*finished/n)) + "%"].join(', '));
}, 2000);
(function run(uri) {
running++; ran++;
if( running < c ) run(uri);
var start = microtime.now();
request(uri, function (error, response, body) {
running--; finished++;
timer.update(microtime.now() - start);
if(ran !== n) run(uri);
if(finished === n) {
console.log(util.inspect(collection.toJSON(), false, 10));
clearInterval(statusInterval);
process.exit(0);
}
});
}(uri));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment