如何使用群集提高NodeJS服务器的吞吐量?

时间:2015-06-23 12:36:45

标签: javascript node.js multithreading

我有一个NodeJS服务器(Express),我正在使用nodeJs站点上的集群模块示例将请求传播到多个处理器。

<main>

    <div class="container">

        <div id="stream"></div>

    </div>
</main>

<script id="live" type="text/template">
    {{#videos}}

        <h5>{{header}}</h5>

        <div class="card livestream"> 
           <iframe width="100%" height="100%" src="https://www.youtube.com/embed/{{url}}" frameborder="0" allowfullscreen></iframe>
        </div>

    {{/videos}}
</script>

<!-- Mustache.js -->  
<script src="js/mustache.js"></script>
<script>

        $(function() {

            $.getJSON('externalurl.com/events/json/livestream.json?callback=?', function(data) {
                var template = $('#live').html();
                var html = Mustache.to_html(template, data);
                $('#stream').html(html);
            }); //getjson
        }); //function

</script>

问题在于围攻的基准测试表明我的命中数没有增加。这是围攻的输出:

if (cluster.isMaster) {
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  };
  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
    cluster.fork();
  });
} else {  
  server.listen(app.get('port'), function(){
    console.log('HTTP server on port ' + app.get('port') + ' - running as ' + app.settings.env);
  });

  // setup socket.io communication
  io.sockets.on('connection', require('./app/sockets'));
  io.sockets.on('connection', require('./app/downloadSockets'));
}

聚类后:

$ siege -c100 192.168.111.1:42424  -t10S
** SIEGE 3.0.5
** Preparing 100 concurrent users for battle.
The server is now under siege...
Lifting the server siege...      done.

Transactions:               1892 hits
Availability:             100.00 %
Elapsed time:              10.01 secs
Data transferred:           9.36 MB
Response time:              0.01 secs
Transaction rate:         189.01 trans/sec
Throughput:             0.93 MB/sec
Concurrency:                1.58
Successful transactions:        1892
Failed transactions:               0
Longest transaction:            0.05
Shortest transaction:           0.00

这是否意味着我的服务器已经获得单个服务器的最大吞吐量,可能是因为它是本地计算机,或者由于运行的进程太多而无法获得4个处理器,我不确定。

如何使用群集模块增加throghput以及为什么我当前的代码没有成功?我还检查了它确实创建了4个服务器实例,即cluster.fork工作。 任何提示都非常有用。

2 个答案:

答案 0 :(得分:6)

通过群集或增长并发查询来实现效果(尝试将并发用户数增加到300-400)。或者给严重负担的任务。 让我们花更多有趣的测试:将下载大约1 MB的文件大小,另外我们会延迟5毫秒和50毫秒来模拟复杂的操作。对于本地测试的四核处理器将如下(分别为普通和集群):

$ siege -c100 http://localhost/images/image.jpg  -t10S

正常模式(5毫秒延迟):

Lifting the server siege...      done.
Transactions:                    1170 hits
Availability:                 100.00 %
Elapsed time:                   9.10 secs
Data transferred:             800.79 MB
Response time:                  0.27 secs
Transaction rate:             128.57 trans/sec
Throughput:                    88.00 MB/sec
Concurrency:                   34.84
Successful transactions:        1170
Failed transactions:               0
Longest transaction:            0.95
Shortest transaction:           0.01

群集模式(5毫秒延迟):

Lifting the server siege...      done.
Transactions:                    1596 hits
Availability:                 100.00 %
Elapsed time:                   9.04 secs
Data transferred:            1092.36 MB
Response time:                  0.06 secs
Transaction rate:             176.55 trans/sec
Throughput:                   120.84 MB/sec
Concurrency:                    9.81
Successful transactions:        1596
Failed transactions:               0
Longest transaction:            0.33
Shortest transaction:           0.00

正常模式(50毫秒延迟):

Lifting the server siege...      done.
Transactions:                     100 hits
Availability:                 100.00 %
Elapsed time:                   9.63 secs
Data transferred:              68.44 MB
Response time:                  5.51 secs
Transaction rate:              10.38 trans/sec
Throughput:                     7.11 MB/sec
Concurrency:                   57.18
Successful transactions:         100
Failed transactions:               0
Longest transaction:            7.77
Shortest transaction:           5.14

群集模式(50毫秒延迟):

Lifting the server siege...      done.
Transactions:                     614 hits
Availability:                 100.00 %
Elapsed time:                   9.24 secs
Data transferred:             420.25 MB
Response time:                  0.90 secs
Transaction rate:              66.45 trans/sec
Throughput:                    45.48 MB/sec
Concurrency:                   59.59
Successful transactions:         614
Failed transactions:               0
Longest transaction:            1.50
Shortest transaction:           0.50

答案 1 :(得分:2)

在你的例子中,你并没有真正做任何事情。连接到mysql并运行繁重的查询,或者发出需要几秒钟的http请求。您会注意到,最终,您将编写阻止事件循环的代码(或使用第三方库)。这是集群很重要的时候,因为你基本上每个处理器都有一个事件循环。如果一个查询很慢,并且事件循环需要等待它,它就不会停止那些命中你的API /应用程序的新请求。

此外,如果您计划使用或连接到数据库或获取外部资源,您可能想要读取连接池,特别是npm上的generic-pool。