简体   繁体   English

为什么非集群应用程序在Node.js中表现优于集群应用程序?

[英]Why non-cluster app outperform cluster app in nodejs?

Many people said cluster module makes node.js app more faster than regular one. 许多人说集群模块使node.js应用比常规应用更快。 But what i experienced here is somehow confusing. 但是我在这里所经历的却有些令人困惑。 I create two scripts, first is the regular http server without using cluster module. 我创建了两个脚本,第一个是不使用群集模块的常规http服务器。 Second script is http server using cluster module. 第二个脚本是使用群集模块的http服务器。

I am using apache benchmark to send high request to these servers. 我正在使用apache基准测试向这些服务器发送高要求。 And here are the results: 结果如下:

Attempt#1:
non-cluster: 15,418 req/sec
cluster: 10,333 req/sec

Attempt#2:
non-cluster: 12,563 req/sec
cluster: 9,874 req/sec

how can non-cluster script outperform cluster script? 非集群脚本如何胜过集群脚本?

here is the github repo for the scripts. 这是脚本的github回购

non-cluster script: 非集群脚本:

const http = require('http');

const server = http.createServer((req, res) => {
  res.end('Hello World!');
});

server.listen(process.env.SERVER_PORT, () => {
  console.log('Server started on port ' + process.env.SERVER_PORT);
});

cluster script: 集群脚本:

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

  // Fork workers.
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('Hello World!');
  }).listen(process.env.SERVER_PORT);

  console.log(`Worker ${process.pid} started`);
}

My node version is 7.10 on Ubuntu 16.04. 我的节点版本在Ubuntu 16.04上为7.10。

I try to set the scheduling policy to SCHED_NONE on cluster script 我尝试在群集脚本上将调度策略设置为SCHED_NONE

cluster.schedulingPolicy = cluster.SCHED_NONE

Now cluster script constantly outperforms the non-cluster script. 现在,群集脚本的性能始终优于非群集脚本。 yeeah yeeah

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM