Skip to content

Instantly share code, notes, and snippets.

@polotek
Created August 16, 2011 21:41
Show Gist options
  • Save polotek/1150258 to your computer and use it in GitHub Desktop.
Save polotek/1150258 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
require('long-stack-traces');
var urlUtils = require('url')
, util = require('util')
, nopt = require('nopt')
, EventEmitter = require('events').EventEmitter
, _ = require('underscore')
, socketio = require('socket.io-client');
var opts = {
host: String
, port: Number
, concurrency: Number
, time: Number
, delay: Number
, log: Boolean
, verbose: Boolean
}
, shorts = {
h: '--host'
, p: '--port'
, c: '--concurrency'
, t: '--time'
, d: '--delay'
, l: '--log'
, v: '--verbose'
}
, parsed = nopt(opts, shorts)
var seeds = 0;
function StressTest(url, opts) {
this.clients = {};
this.url = url;
this.opts = _.extend({
concurrency: 10 // clients
, time: 10 // sec
, delay: 10 // ms
}, opts);
}
util.inherits(StressTest, EventEmitter);
StressTest.prototype.start = function(callback) {
for(var i = 0; i < this.opts.concurrency; i++) {
this.addClient();
}
var start = +new Date();
var send = function() {
_.each(this.clients, function(client, id) {
client.send({
type: 'COLLABROOM'
, component: 'pad'
, data: {
type: 'USER_CHANGES'
, baseRev: 41
, changeset: 'Z:1h>3|8=1g*0+3$adf'
, apool: {
numToAttrib: {}
, nextNum: 1
}
}
});
});
var lapsed = (new Date() - start) / 1000;
if(lapsed < this.time) {
this.setTimeout(send, delay);
} else {
callback && process.nextTick(callback);
}
}
send();
}
StressTest.prototype.addClient = function(client) {
var urlObj = urlUtils.parse(this.url);
console.error(urlObj);
client = client || socketio.connect(urlObj.protocol + '://' + urlObj.host, {
resource: urlObj.pathname
});
client.id = seeds++;
this.clients[client.id] = client;
var onError = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift('error', client);
this.emit.apply(this, args);
}
client.on('error', onError);
}
var opts = {}
, url = parsed.remain && parsed.remain[0] || '/socket.io'
, urlObj = urlUtils.parse(url);
opts.host = parsed.host || urlObj.hostname || 'localhost';
urlObj.protocol = 'http:';
urlObj.hostname = parsed.host || urlObj.hostname || 'localhost';
urlObj.port = parsed.port || urlObj.port || 9001;
url = urlUtils.format(urlObj);
var test = new StressTest(url, parsed);
test.on('error', console.error)
.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment