3

我被要求粗略计算单个 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。

4

1 回答 1

2

这很可能是您的客户端和服务器之间的延迟,也可能是对来自客户端的传出连接数量的限制,以及操作系统对服务器上传入连接的限制。请注意,Windows 的限制低于其他操作系统。

此外,您会期望 CPU、RAM 和带宽使用率较低,因为您正在执行的操作既不是计算、内存也不是带宽。

请查看 Apache Bench 作为测试服务器的一种方式,而不是编写可能是也可能不是最佳实现的客户端:https ://en.wikipedia.org/wiki/ApacheBench

无论如何,请使用更多来自不同网络的客户端计算机,这样您就可以将我们的本地交换机或路由器视为瓶颈

于 2013-03-15T14:09:18.430 回答