Node.js集群不会改善no。 一些博客声称的每秒交易量
我尝试了没有任何性能改进的节点群集。 可能是我可能会错误地测量它。
使用Node.js 7.4.0
server.js const http = require('http'); const server = http.createServer((req, res) => { for (let i = 0; i < 10000; i++) { } res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end("Hello World!"); }).listen(4002);
我遵循本教程创build集群服务器http://rowanmanning.com/posts/node-cluster-and-express/
cluster.js const cluster = require('cluster'); const http = require('http'); const numCPUs = require('os').cpus().length; if (cluster.isMaster) { for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => { console.log(cluster.worker.id); cluster.fork(); }); } else { http.createServer((req, res) => { for (let i = 0; i < 10000; i++) { } res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end("Hello World!"); }).listen(4001); }
我可以看到4核心的主人和工人的pids
$ps axl | grep node 0 0 8799 8789 20 0 682196 28056 ep_pol Sl+ pts/1 0:00 node index.js 0 0 8805 8799 20 0 682196 27708 ep_pol Sl+ pts/1 0:00 /usr/bin/nodejs /home/ubuntu/index.js 0 0 8811 8799 20 0 682196 27608 ep_pol Sl+ pts/1 0:00 /usr/bin/nodejs /home/ubuntu/index.js 0 0 8812 8799 20 0 682196 27756 ep_pol Sl+ pts/1 0:00 /usr/bin/nodejs /home/ubuntu/index.js 0 0 8818 8799 20 0 682196 27604 ep_pol Sl+ pts/1 0:00 /usr/bin/nodejs /home/ubuntu/index.js
正如Rowan声称4倍的性能提升,我几乎没有得到聚集的任何performance。
我的围攻结果在t2.xlarge AWS实例(4核心)Ubuntu机器上 。
server.js - $siege -c100 -t1M http://xx.xx.xx.xx:4002/ Transactions: 13545 hits Availability: 100.00 % Elapsed time: 59.39 secs Data transferred: 0.16 MB Response time: 0.19 secs Transaction rate: 228.07 trans/sec Throughput: 0.00 MB/sec Concurrency: 42.30 Successful transactions: 13545 Failed transactions: 0 Longest transaction: 11.61 Shortest transaction: 0.02
现在cluster.js
cluster.js - $siege -c100 -t1M http://xx.xx.xx.xx:4001/ Transactions: 13223 hits Availability: 100.00 % Elapsed time: 59.96 secs Data transferred: 0.15 MB Response time: 0.16 secs Transaction rate: 220.53 trans/sec Throughput: 0.00 MB/sec Concurrency: 35.95 Successful transactions: 13223 Failed transactions: 0 Longest transaction: 11.56 Shortest transaction: 0.02
令人惊讶的是,与server.js相比,cluster.jsperformance不佳。
难道我做错了什么? 这是node.js版本的变化吗? 还是这些说法是错误的?
Improve Node.js Performance by Turning It into a Clusterfork
https://medium.com/node-and-beyond/the-incomplete-collection-of-node-js-performance-tips-94cc712661bd#.lmd19k2u1
任何帮助深表感谢。
所以这个类似于:
NodeJS集群将所有请求发送给一个工作者(Windows)
和
node.js中的集群不起作用。 只有一个工作人员总是回应
尝试像这样:
http.createServer(function(req, res) { console.log('worker:' + cluster.worker.id + " going to send response "); //(Heavy I/O operation...followed by res.send(result)) }).listen(8000);