Skip to content

Instantly share code, notes, and snippets.

@rmehner
Forked from felixge/package.json
Created October 6, 2011 15:31
Show Gist options
  • Save rmehner/1267695 to your computer and use it in GitHub Desktop.
Save rmehner/1267695 to your computer and use it in GitHub Desktop.
.DS_Store
node_modules
{
"author": "Robin Mehner <[email protected]>",
"name": "socketio-bench-server",
"description": "Small socket.io server bench",
"version": "0.0.0",
"repository": {
"url": ""
},
"engines": {
"node": "~0.4.12"
},
"dependencies": {
"socket.io": "~0.8.4",
"commander": "~0.2.0",
"socket.io-client": "~0.8.4"
},
"devDependencies": {}
}
var port = 3030;
var interval = 1000;
var duration = 10 * 1000;
var receivedCounter = 0;
var sockets = [];
var io = require('socket.io').listen(port);
io.configure(function() {
io.set('log level', 1);
});
io.sockets.on('connection', function (socket) {
sockets.push(socket);
socket.on('chat.send', function(data) {
receivedCounter++;
});
});
setInterval(function() {
sockets.forEach(function(socket) {
socket.emit('chat.msg', {
msg: 'JavaScript motherfucker. Do you speak it!'
});
});
}, interval);
setInterval(function() {
console.log('Received %d requests per second', receivedCounter / duration * 1000);
receivedCounter = 0;
}, duration);
var program = require('commander');
var io = require('socket.io-client');
var clients = [];
program
.option('-p, --port [port]', 'The port to connect to [3030]', 3030)
.option('-h, --host [host]', 'The host to connect to [localhost]', 'localhost')
.option('-c, --clients [clients]', 'The number of total clients', 1000)
.option('-m, --messages [messages]', 'The number of messages to send per sec [0.1]', 0.1)
.parse(process.argv);
function WebsocketClient() {
this.connection = null;
this.receivedMessages = 0;
}
WebsocketClient.prototype.connect = function() {
this.connection = io.connect(
'http://' + program.host + ':' + program.port,
{'force new connection': true}
);
this.connection.on('chat.msg', this.handleChatMessage.bind(this));
}
WebsocketClient.prototype.sendMsg = function() {
if (this.connection.socket.connected) {
this.connection.emit('chat.send', {msg: 'Your mum'});
}
}
WebsocketClient.prototype.handleChatMessage = function(data) {
this.receivedMessages++;
}
WebsocketClient.prototype.sendMessages = function() {
var that = this;
setInterval(function() {
that.sendMsg();
}, 1000 / program.messages);
}
for (var i = 0; i < program.clients; i++) {
var client = new WebsocketClient();
client.connect();
client.sendMessages();
clients.push(client);
}
setInterval(function() {
var sum = 0;
var receivedMessages = clients.map(function(client) {
var messageCount = client.receivedMessages;
client.receivedMessages = 0;
return messageCount;
});
receivedMessages.forEach(function(count) {
sum += count;
});
console.log('Received %d requests per second', sum / 10000 * 1000)
}, 10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment