首页 > 解决方案 > Node.js clustering is slower than single threaded mode

问题描述

I was just experimenting with clustering in Node.js. I wrote a little fibonacci example, where I called the function using a clustered server and a non-clustered server as follows.

// fib.js

module.exports = function () {
    const store = []
    store[0] = 0
    store[1] = 1

    return function fib(n) {
        if (store[n] === undefined) {
            store[n] = fib(n - 2) + fib(n - 1)
        }

        return store[n]
    }
}

// non-clustered server

const http = require('http')
const fib = require('./fib.js')

const f = fib()
http.createServer((req, res) => {
    res.writeHead(200)
    res.end(`From ${process.pid}: ${f(30)}\n`)
}).listen(8000)

// clustered server

const cluster = require('cluster')
const http = require('http')
const numCPUs = require('os').cpus().length
const fib = require('./fib.js')

const f = fib()
if (cluster.isMaster) {
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork()
    }
} else {
    http.createServer((req, res) => {
        res.writeHead(200)
        res.end(`From ${process.pid}: ${f(30)}\n`)
    }).listen(8000)
}

Now, I tried benchmarking the performance of these two servers using artillery but the non-clustered server seemed to be faster. How come?

// Clustered Server Report

Started phase 0, duration: 1s @ 07:53:24(+0000) 2018-11-23
Report @ 07:53:27(+0000) 2018-11-23
  Scenarios launched:  50
  Scenarios completed: 50
  Requests completed:  2000
  RPS sent: 803.21
  Request latency:
    min: 0.3
    max: 70.5
    median: 20.7
    p95: 38.7
    p99: 45.6
  Codes:
    200: 2000

All virtual users finished
Summary report @ 07:53:27(+0000) 2018-11-23
  Scenarios launched:  50
  Scenarios completed: 50
  Requests completed:  2000
  RPS sent: 796.81
  Request latency:
    min: 0.3
    max: 70.5
    median: 20.7
    p95: 38.7
    p99: 45.6
  Scenario counts:
    0: 50 (100%)
  Codes:
    200: 2000

// Non-clustered Server Report

Started phase 0, duration: 1s @ 07:53:39(+0000) 2018-11-23
Report @ 07:53:41(+0000) 2018-11-23
  Scenarios launched:  50
  Scenarios completed: 50
  Requests completed:  2000
  RPS sent: 806.45
  Request latency:
    min: 0.3
    max: 70.1
    median: 21.4
    p95: 38.3
    p99: 44.6
  Codes:
    200: 2000

All virtual users finished
Summary report @ 07:53:41(+0000) 2018-11-23
  Scenarios launched:  50
  Scenarios completed: 50
  Requests completed:  2000
  RPS sent: 800
  Request latency:
    min: 0.3
    max: 70.1
    median: 21.4
    p95: 38.3
    p99: 44.6
  Scenario counts:
    0: 50 (100%)
  Codes:
    200: 2000

标签: node.jscluster-computing

解决方案


节点集群文档自己说,由于操作系统调度程序变化无常,工作人员分布往往非常不平衡,这会影响性能。另请参见负载cluster nginx均衡器和iptables负载均衡器之间的比较。

如果我们想获得有意义的结果,AFAIK 性能测试往往要复杂得多。关于您的测试的一些主要与统计相关的问题:

  1. 样本量:为什么选择这个样本量?您确定不同样本量的结果会相同吗?clustering例如:当单线程处理请求处理能力结束时,AFAIK 的主要优势就开始了。

  2. 测试环境:你是如何运行你的测试的?在什么环境下?环境被隔离了?在多少个核心上?什么样的处理器?

  3. 意义:为什么你认为你的结果代表了显着差异?你是如何测试样本之间的差异的?


推荐阅读