Skip to content

Instantly share code, notes, and snippets.

@timchunght
Forked from redism/client.js
Created April 29, 2020 20:54
Show Gist options
  • Save timchunght/20ddf0073fedb38c1bcc84665611c8e9 to your computer and use it in GitHub Desktop.
Save timchunght/20ddf0073fedb38c1bcc84665611c8e9 to your computer and use it in GitHub Desktop.
Simple socket.io server performance test
/**
* 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();
/**
* 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