Last active
November 1, 2017 21:02
-
-
Save pennyfx/a7227e230e74e42ec558455f3d08824e to your computer and use it in GitHub Desktop.
apache bench like tester in node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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