如何增加使用集群的NodeJS服务器的吞吐量?

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

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 

聚类后​​:

 $ 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: 1884 hits Availability: 100.00 % Elapsed time: 9.52 secs Data transferred: 9.32 MB Response time: 0.01 secs Transaction rate: 197.90 trans/sec Throughput: 0.98 MB/sec Concurrency: 1.72 Successful transactions: 1884 Failed transactions: 0 Longest transaction: 0.07 Shortest transaction: 0.00 

这是否意味着我的服务器已经获得单个服务器的最大吞吐量,可能是因为它是一个本地机器,或者它不能获得4个处理器,因为有太多的进程运行,我不知道。

如何使用群集模块来增加throghput,为什么我的当前代码不成功? 另外我检查确实创build了4个服务器实例,即cluster.fork的作品。 任何提示将是非常有用的。

通过群集或增长并发查询(尝试将并发用户数量增加到300-400)可以达到这一效果。 或者给予沉重负担的任务。 让我们花更多有趣的testing:下载一个大约1 MB的文件大小,再加上我们延迟5毫秒和50毫秒来模拟复杂的操作。 对于本地testing的四个核心处理器将分别如下(分别为正常和集群):

 $ 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 

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

此外,如果您打算使用或连接到数据库或获取外部资源,则可能需要了解连接池,尤其是npm上的通用池。