Created
December 29, 2010 15:46
-
-
Save bcg/758652 to your computer and use it in GitHub Desktop.
A performance suite for 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
#!/usr/bin/env node | |
var net = require('net'), | |
util = require('util'), | |
stomp = require('stomped'); | |
var args = process.argv.slice(2); | |
var test_queue = args[0] || "q"; | |
var message_count = 0; | |
client = stomp.createClient('127.0.0.1', 61613); | |
client.on('connected', function() { | |
client.subscribe(test_queue); | |
client.on('message', function(queue, message) { | |
message_count += 1; | |
}); | |
}); | |
client.on('disconnected', function() { | |
process.exit(1); | |
}); | |
setInterval(function() { | |
if (message_count > 0) { | |
console.log('messages:'+message_count); | |
message_count = 0; | |
} | |
}, 1000); |
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 perf = require(__dirname + '/perf'); | |
var performer = new perf.Performer(); | |
performer.register('broker', __dirname + '/apps/broker.js', [], 0); | |
performer.register('consumer', __dirname + '/apps/consumer.js', ['q'], 100); | |
performer.register('producer', __dirname + '/apps/producer.js', ['q', '100000'], 200); | |
performer.run(); | |
performer.on('finished', function() { | |
console.log(performer.toString()); | |
process.exit(1); | |
}); |
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 util = require('util'), | |
sys = require('sys'), | |
events = require('events'), | |
spawn = require('child_process').spawn; | |
function Performer() { | |
var self = this; | |
this.apps = {}; | |
this.data = {}; | |
this.last_update = new Date(); | |
this.listenAfter = 0; | |
setInterval(function() { | |
var now = new Date(); | |
if ((now - self.last_update ) > 2500) { | |
for(tag in self.apps) { | |
var app = self.apps[tag]; | |
app.process.kill(); | |
} | |
self.emit('finished'); | |
} | |
}, 2500); | |
} | |
sys.inherits(Performer, events.EventEmitter); | |
Performer.prototype.register = function (tag, cmd, args, delay) { | |
this.apps[tag] = { | |
cmd: cmd, | |
args: args, | |
delay: delay, | |
process: null | |
}; | |
if (delay > this.listenAfter) { | |
this.listenAfter = delay; | |
} | |
} | |
Performer.prototype.run = function() { | |
var self = this; | |
for(tag in this.apps) { | |
(function(t) { | |
setTimeout(function() { | |
var app = self.apps[t]; | |
app.process = spawn(app.cmd, app.args); | |
app.process.stderr.on('data', function(data) { | |
console.log('ERROR: ' + data.toString()); | |
}); | |
app.process.stdout.on('data', function(data) { | |
self.fromData(t, data.toString()); | |
}); | |
}, self.apps[tag].delay); | |
})(tag); | |
} | |
} | |
Performer.prototype.fromData = function (tag, data) { | |
this.last_update = new Date(); | |
var lines = data.split('\n', -1); | |
for (var i=0; i<lines.length; i++) { | |
if (lines[i] === '') { | |
continue; | |
} | |
var parts = []; | |
if (lines[i].indexOf(',') != -1) { | |
parts = lines[i].split(',',2); | |
} else { | |
parts.push(lines[i]); | |
} | |
for (var n=0;n<parts.length;n++) { | |
var stat = parts[n].split(':',2); | |
if (!this.data[tag]) { | |
this.data[tag] = {}; | |
} | |
if (!this.data[tag][stat[0]]) { | |
this.data[tag][stat[0]] = []; | |
} | |
this.data[tag][stat[0]].push(parseInt(stat[1])); | |
} | |
} | |
} | |
Performer.prototype.toString = function() { | |
console.log('\nEVENTS PER SECOND\n'); | |
for (tag in this.data) { | |
console.log(tag+':'); | |
for (stat in this.data[tag]) { | |
var times = this.data[tag][stat].join(', '); | |
console.log(' '+stat+':'); | |
var total = 0; | |
for(var n=0; n<this.data[tag][stat].length; n++) { | |
total = total + this.data[tag][stat][n]; | |
} | |
console.log(' total: ' + total); | |
console.log(' average: '+ total / this.data[tag][stat].length + '/s'); | |
} | |
} | |
} | |
exports.Performer = Performer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment