javascript - Node.js 集群没有改进。一些博客声称的每秒交易数

标签 javascript node.js performance

我尝试了 Node 集群,但没有任何性能改进。可能是我测量错误。

使用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);

我按照本教程创建了集群服务器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 个核心上的 master 和worker 的 pid

$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 倍一样,我几乎没有从集群中获得任何性能。

我在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.js 的表现很差。

我做错了什么吗?是node.js版本的变化吗?或者这些说法是错误的吗?

https://keyholesoftware.com/2015/01/26/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

https://www.sitepoint.com/how-to-create-a-node-js-cluster-for-speeding-up-your-apps/

非常感谢任何帮助。

最佳答案

所以这类似于:

NodeJS clustering sending all the request to one worker (Windows)

clustering in node.js is not working. Only one worker is always responding

尝试如下:

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

关于javascript - Node.js 集群没有改进。一些博客声称的每秒交易数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41553832/

相关文章:

Java高效文件写入: String concatentation versus extra call to write()

javascript - react JS : Calling two functions in an onClick event does not run functions

javascript - 使用 jquery 拖放

Node.js 和 Socket.io 内存泄漏修复

javascript - Linux 上的 NodeJS/V8/JavaScript : Upredictable ramp-up to full performance

javascript - 如何增加 Vue 应用程序的 max-http-header-size

java - 如果有一个列表一直持有300,000个对象,gc会不会有不好的表现?

iphone - 圆 UIView 角落的最佳和最快方法?

javascript - 如何在特定单元格或表格的td中添加多个输入?

javascript - moment.js 日期会添加,但时间不会