我被要求粗略计算单个 socket.io 实例可以在大型 EC2 实例(16Gb Ram,4 Core Xeon)上处理多少个连接。
我更新了简单的 socket.io 基准测试示例。
服务器:
var io = require('socket.io').listen(80);
var exec = require('child_process').exec;
io.configure(function() {
io.set('log level', 1);
var transport = process.argv.length >= 2 ? process.argv[2] : null;
if (transport) {
io.set('transports', [transport]);
}
});
var payload = "Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item " +
"Payload: Payload line item ";
var users = 0;
function roundNumber(num, precision) {
return parseFloat(Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision));
}
setInterval(function() {
io.sockets.emit("message", payload);
console.log("Users: " + users);
}, 3000);
io.sockets.on('connection', function(socket) {
users++;
socket.on('disconnect', function() {
users--;
})
});
客户
var io = require('socket.io-client');
var message = "o bispo de constantinopla nao quer se desconstantinopolizar";
var receivedMessages = 0;
function user(shouldBroadcast, host, port) {
var socket = io.connect('http://' + host + ':' + port, {
'force new connection': true
});
socket.on("message", function(){
receivedMessages += 1;
})
};
var argvIndex = 2;
var users = parseInt(process.argv[argvIndex++]);
var rampUpTime = parseInt(process.argv[argvIndex++]) * 1000; // in seconds
var newUserTimeout = rampUpTime / users;
var shouldBroadcast = process.argv[argvIndex++] === 'broadcast' ? true : false;
var host = process.argv[argvIndex++] ? process.argv[argvIndex - 1] : 'localhost';
var port = process.argv[argvIndex++] ? process.argv[argvIndex - 1] : '3000';
for(var i=0; i<users; i++) {
setTimeout(function() { user(shouldBroadcast, host, port); }, i * newUserTimeout);
};
setInterval(function() {
console.log("Messages received: " + receivedMessages);
receivedMessages = 0;
}, 3000);
我运行了这个示例,客户端在中型实例上启动,在大型服务器上启动。我得到的结果是每 3 秒收到大约 4500 条消息。
但网络、cpu 和 ram 使用率在 2-10% 范围内。
是否可以增加服务连接的数量?
服务器操作系统是 Windows Server 2008 R2。