Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
apache bench like tester in node
var async = require('async');
var args = require('yargs').argv;
var request = require('request');
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};
const concurrency = args.c || 1,
count = args.n || 100,
data = args.d,
verb = args.verb || 'GET',
url = args.url;
var headers = args.h;
var array = new Array(count);
array.fill('a');
var current_count = 0;
if (!Array.isArray(headers)){
headers = [headers];
}
var headers_obj = {};
headers.forEach(h => {
var tmp = h.split(':');
headers_obj[tmp[0]] = tmp[1].trim();
})
async.parallelLimit(array.map(function(){
return function(cb){
var time = new Date().getTime();
var options = {
method: verb,
headers: headers_obj,
timeout: 15000
}
if (data){
options['body'] = data;
}
//console.log(options);
request(url, options, function(err, res, body){
current_count++;
if (current_count % 100 == 0){
console.log('Completed', current_count);
}
var diff = new Date().getTime() - time;
if (err){
console.log(err);
cb(null, {
error: err
});
} else {
cb(null, {
statusCode: res.statusCode,
diff: diff
});
}
});
}
}), concurrency,
function(err, results){
var success_cnt = 0;
var sum = 0;
results.filter(f => !f.error).forEach(item => {
success_cnt++;
sum += item.diff;
})
var avg = sum/success_cnt;
console.log('Count:', success_cnt, 'Avg: ', avg, 'Request Per Sec:', 1000 / (avg/concurrency));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.