2013-03-15 51 views
2

客戶要求我執行的許多連接如何挑選一個大EC2實例(16GB內存,4核至強)​​socket.io實例句柄粗略計算的最高金額。的Node.js + 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。

+0

所以CPU不是瓶頸,可能你的IO在某個地方。 – UpTheCreek 2013-03-15 13:38:32

+0

相關問題:[我可以通過socket.emit發送多少數據?](http://stackoverflow.com/q/12977719/201952) – josh3736 2013-03-15 14:35:32

回答

1

這是最有可能你的客戶端和服務器之間的延遲,也可能是從客戶端的傳出連接數的限制,並在您的服務器上進來的連接操作系統的限制。請注意,Windows有比其他操作系統低的限制。

而且你所期望的CPU,內存和帶寬佔用低,因爲你正在做的工作既不是計算,內存和帶寬重。

請看Apache的替補身份測試服務器,而不是寫這可能是也可能不是最佳的實現客戶端的方式:https://en.wikipedia.org/wiki/ApacheBench

在任何情況下,使用更多的客戶端計算機,從不同的網絡,所以你可以把我們的本地交換機或路由器當作瓶頸