Skip to content

Instantly share code, notes, and snippets.

@lukegalea
Created March 8, 2011 19:41
Show Gist options
  • Save lukegalea/860869 to your computer and use it in GitHub Desktop.
Save lukegalea/860869 to your computer and use it in GitHub Desktop.
Stress Tester For Chat
// your_prompt_$ sudo sysctl -w kern.maxfiles=65536
// your_prompt_$ sudo sysctl -w kern.maxfilesperproc=32768
var exec = require('child_process').exec,
http = require('http'),
_ = require('underscore'),
crypto = require('crypto');
var md5 = function(str) {
var hash = crypto.createHash('md5');
hash.update(str);
return hash.digest('hex');
};
var concurrency = (Number)(process.env['CONNECTIONS'] || 10000),
host = process.env['HTTP_HOST'],
path = process.env['HTTP_PATH'],
port = process.env['HTTP_PORT'],
user = '[email protected]',
token = md5(user + 'em');
console.log(token);
console.dir([host, path, port]);
if (!host || !path || !port) {
throw new Error("HTTP_HOST, HTTP_PATH and HTTP_PORT must be specified");
}
var throwop = function(err) {
if (err) { throw err; }
},
logop = function(chunk) {
console.log(chunk.toString('utf8'));
},
printOpenSockets = function() {
console.log("Sockets open: " + http.getAgent(host, port).sockets.length );
};
var fdLimit = function(callback) {
exec('ulimit -n', function (error, stdout, stderr) {
try {
var limit = (Number)(/(\d+)/.exec(stdout)[1]);
callback(null, limit);
} catch(err) {
callback(err);
}
});
};
fdLimit( function(err, limit) {
throwop(err);
if (limit < concurrency) {
throw new Error("Concurrency is higher than max files limit. Please increase.");
}
http.getAgent(host, port).maxSockets = concurrency;
var requests = [];
for( var i = 0; i < concurrency; i++ ) {
var options = {
host: host,
port: port,
path: path,
headers: {
'Cookie': 'ajaxim_user=' + user + ';ajaxim_token=' + token
}
},
logResponseEnd = function(num) {
return function(res) {
console.log("Response " + num + ": " + res.statusCode);
if (res.statusCode != 200) {
res.on('data', logop);
}
printOpenSockets();
};
};
console.log("Starting request " + i);
printOpenSockets();
requests.push(http.request(options, logResponseEnd(i)));
}
_(requests).each(function(req) {
req.end();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment