Skip to content

Instantly share code, notes, and snippets.

@mlconnor
Last active August 29, 2015 14:09
Show Gist options
  • Save mlconnor/5dd84bdfa749fd59bdb2 to your computer and use it in GitHub Desktop.
Save mlconnor/5dd84bdfa749fd59bdb2 to your computer and use it in GitHub Desktop.
Performance test with node.js
var request = require('request');
var async = require('async');
var url = "...";
var payload = "...";
var calls = [];
var clients = 100;
var callsPerClient = 20;
var thinkTime = 0;
var totalRequests = callsPerClient * clients;
var testStartTime = (new Date()).getTime();
for ( var i = 0; i < totalRequests; i++ ) {
calls.push(runCall);
}
async.parallelLimit(calls, clients, function(err) {
console.log('complete', err);
var testEndTime = (new Date()).getTime();
var totalTime = (testEndTime - testStartTime) / 1000;
var requestsPerSecond = Math.round(totalRequests / totalTime);
console.log('completed ' + totalRequests + ' in ' + totalTime + 's, requests/sec=' + requestsPerSecond);
});
function runCall(callback) {
var startTime = (new Date()).getTime();
var options = {
method:'POST',
body: payload,
url: url,
timeout: 30000,
strictSSL: false,
headers: {
'Content-Type': 'text/xml;charset=UTF-8',
'User-Agent': 'Connors NodeJS Perf Tester'
}
};
request(options, function(error,response,body) {
var endTime = (new Date()).getTime();
//console.log(response);
console.log("Time=" + (endTime - startTime) + "ms Err=" + error, "Response Code=" + response.statusCode + " Response Len=" + body.length);
setTimeout(function() {
callback();
}, thinkTime);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment