Node.js + socket.io确定每个实例的最大客户端数量

时间:2013-03-15 13:31:44

标签: node.js amazon-ec2 socket.io amazon benchmarking

我被要求粗略计算一个大型EC2实例(16Gb Ram,4 Core Xeon)上单个socket.io实例处理的连接数。

我更新了简单socket.io基准测试的示例。

Serever:

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。

1 个答案:

答案 0 :(得分:2)

最有可能是客户端和服务器之间的延迟,也可能是对客户端的传出连接数量的限制,以及操作系统上连接的操作系统限制。请注意,窗口的下限低于其他操作系统。

此外,您可能希望CPU,RAM和带宽使用率较低,因为您正在执行的操作既不是计算,也不是ram或带宽。

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

在任何情况下,使用来自不同网络的更多客户端计算机,以便您可以将我们的本地交换机或路由器作为瓶颈来管理