Created
April 25, 2014 09:41
-
-
Save redism/11283852 to your computer and use it in GitHub Desktop.
Simple socket.io server performance test
This file contains 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
/** | |
* A simple socket.io client for performance benchmark | |
* | |
* Created by redism on 2014. 4. 22.. | |
*/ | |
var SocketIO = require('socket.io-client'), | |
argv = require('optimist').argv; | |
var n = argv.n || 10; | |
var b = argv.b || 100; // bucket-size | |
var host = argv.h || 'http://localhost:8082'; | |
var sockets = []; | |
var msg = 1; | |
var connectionCount = 0; | |
var disconnectionCount = 0; | |
var reconn = 0; | |
var setupSocket = function setupSocket(socket, id) { | |
socket.data = {id: id}; | |
socket.on('connect', function () { | |
connectionCount++; | |
}); | |
socket.on('disconnect', function () { | |
connectionCount--; | |
disconnectionCount++; | |
}); | |
socket.on('reconnect', function () { | |
reconn++; | |
}); | |
socket.on('error', function (err) { | |
console.log('Error:', err); | |
}) | |
socket.on('echo', function (msg) { | |
console.log(this.data.id, 'received', msg); | |
}); | |
} | |
var startTest = function startTest() { | |
setInterval(function () { | |
// emit message once per second for each socket. | |
var i, socket; | |
// for (i = 0; i < sockets.length; i++) { | |
// socket = sockets[i]; | |
// socket.emit('echo', 'Message from ', socket.data.id); | |
// } | |
// Monitor count | |
console.log('Connection count:', connectionCount, 'dis:', disconnectionCount, 're:', reconn); | |
}, 1000); | |
} | |
var socket, i; | |
var remaining = n * b; | |
var socketsToConnect = 0; | |
var tryMoreConnection = function tryMoreConnection() { | |
// If all previous connections have been established, | |
if (socketsToConnect == connectionCount) { | |
// try another bucket size | |
for (i = 0; i < b; i++) { | |
(function (j) { | |
socket = SocketIO.connect(host, { | |
'force new connection': true, | |
'reconnect': false, | |
'reconnection': false | |
}); | |
setupSocket(socket, j); | |
sockets.push(socket); | |
socketsToConnect++; | |
remaining--; | |
})(i); | |
} | |
} | |
// Check if we have more clients to simulate. | |
if (remaining > 0) { | |
setTimeout(function () { | |
tryMoreConnection(); | |
}, 100); | |
} | |
} | |
tryMoreConnection(); | |
console.log('Starting stress test client, host :', host + ', socket # :', n); | |
startTest(); |
This file contains 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
/** | |
* A simple socket.io server for performance benchmark | |
* | |
* Created by redism on 2014. 4. 22.. | |
*/ | |
var express = require('express'), | |
http = require('http'), | |
usage = require('usage'), | |
argv = require('optimist').argv, | |
SIO = require('socket.io'); | |
var app = express(); | |
var server = http.createServer(app); | |
var io = SIO.listen(server); | |
var port = argv.p || 8082; | |
var logLevel = argv.l || 1; | |
var connCount = 0; | |
server.listen(port); | |
io.set('log level', logLevel); | |
io.sockets.on('connection', function (socket) { | |
connCount++; | |
socket.on('echo', function (msg) { | |
socket.emit('echo', msg); | |
}); | |
socket.on('disconnect', function () { | |
connCount--; | |
}); | |
}); | |
var pid = process.pid; | |
console.log('Listening on port', port, ', log level', logLevel); | |
setInterval(function () { | |
usage.lookup(pid, {keepHistory: true}, function (err, result) { | |
var memInMb = result.memory / (1024 * 1024); | |
var cpu = result.cpu; | |
console.log('Conn:', connCount + ', Mem:', memInMb.toFixed(1) + ',cpu:', cpu); | |
}); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
server
$ taskset -c [cpu_num] node --stack-size=99999 server.js -p [port_num]
client
$ node client.js -h http://[ip_addr]:[port_num] -b 10 -n 500
b=10, n=500 means that client should see 10 connections established before trying another 10 connections, until 10*500 connections are made.