【问题标题】:Maximum performance for node HTTP server?节点 HTTP 服务器的最大性能?
【发布时间】:2015-03-17 17:31:06
【问题描述】:

我正在运行一项测试,试图从 Node HTTP 服务器中获取最大传输速度。这是一个简单的服务器。 在我的测试中,我有 50K 虚拟客户端与服务器建立永久连接(我之前运行 ulimit -n 99999)。然后,根据另一个事件,即到不同端口的 HTTP 连接,服务器向每个虚拟客户端发送一条消息。最后,所有客户端都会收到相应的消息。 在我的测试中,发送所有消息需要几分钟时间。是否有任何建议可以帮助我改进这些测量,以便我可以在几秒钟而不是几分钟内发送 50K 条消息? 服务器在 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);

【问题讨论】:

    标签: node.js performance http network-programming server


    【解决方案1】:

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

    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);
    }
    

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 谢谢。我已经连接了 50K 个套接字。也许集群可以帮助我,因为 m1.medium 实例有 2 个 vcpu。也许我可以减少一半的时间。
    • 我根据@Ben 的评论添加了一些代码。关于集群,您必须适应您的具体情况。更有趣的是,连接的套接字(如果您正在进行长轮询,则不适用)是每秒请求数。你数过吗?
    • @oglu 你可以为集群添加一些代码吗? maxSockets 目前不是问题。我有 50K 连接的套接字。谢谢。
    • @rodolk 最大套接字不是关于连接到 nodejs 的套接字,而是来自 nodejs。也就是说,如果你有一个后端服务,使用默认设置,你只能有 5 个并发 http 请求,这是非常有限的(这就是为什么我告诉你 reqs/sec 是一个更好的指标)。现在,对于集群的事情,我将为您提供一个简单的 HTTP 示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2018-10-05
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多