我有一个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以及为什么我当前的代码没有成功?此外,我检查它确实创建了4个服务器实例,即cluster.fork工作.
任何提示都非常有用.
解决方法
通过聚类或增长并发查询来实现这种效果(尝试将并发用户数增加到300-400).或者造成严重负担的任务.
让我们花更多有趣的测试:将下载大约1 MB的文件大小,另外我们会延迟5毫秒和50毫秒来模拟复杂的操作.对于本地测试的四核处理器将如下(分别为普通和集群):
让我们花更多有趣的测试:将下载大约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