节点HTTP服务器的最高性能?

我正在运行一个testing,试图从节点HTTP服务器获得最大的传输速度。 这是一个简单的服务器。 在我的testing中,我有50K的虚拟客户端build立与服务器的永久连接(我之前运行ulimit -n 99999)。 然后,在另一个事件,一个HTTP连接到不同的端口,服务器发送一个消息到每个虚拟客户端。 最后,所有客户端都收到相应的消息。 在我的testing中发送所有消息需要几分钟。 有没有任何build议可以帮助我改进这些测量,以便我可以在几秒钟内发送5万条消息而不是几分钟? 服务器在m1.medium AWS实例中运行。 这个想法是用相同的平台来提高性能。

复制服务器代码:

var http = require("http"); var connectionResponse = []; var connectionIndex = 0; http.createServer(function(request, response) { console.log("Received connection " + connectionIndex); response.setTimeout(1200000, function() { console.log("Socket timeout"); }); connectionResponse[connectionIndex] = response; connectionIndex++; }).listen(8888); http.createServer(function(request, response) { console.log("8887 connected - Will respond"); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Triggered all responses"); response.end(); console.log("Begin notifications:" + new Date().toISOString()); for(var i = 0; i < connectionIndex; i++) { connectionResponse[i].writeHead(200, {"Content-Type": "text/plain", "Content-Length": 4, "transfer-encoding" : ""}); connectionResponse[i].write("CAFE"); connectionResponse[i].end(); } console.log("End notifications" + new Date().toISOString()); }).listen(8887); 

将此http://nodejs.org/api/http.html#http_agent_maxsockets设置为足够的数&#x5B57;

 var http = require('http'); http.globalAgent.maxSockets = xxx; var https = require('https'); https.globalAgent.maxSockets = xxx; 

使用nodejs集群模块, http: //nodejs.org/api/cluster.html

现在,关于聚类,这取决于你想要做什么。 默认的例子可以很长的路要你调整它。 一个例子是

 var cluster = require('cluster'); var http = require('http'); var numCPUs = require('os').cpus().length; if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); } else { // Workers can share any TCP connection // In this case its a HTTP server http.createServer(function(req, res) { res.writeHead(200); res.end("hello world\n"); }).listen(8000); }