node.js - 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
解决方案
节点集群文档自己说,由于操作系统调度程序变化无常,工作人员分布往往非常不平衡,这会影响性能。另请参见负载cluster
nginx
均衡器和iptables
负载均衡器之间的比较。
如果我们想获得有意义的结果,AFAIK 性能测试往往要复杂得多。关于您的测试的一些主要与统计相关的问题:
样本量:为什么选择这个样本量?您确定不同样本量的结果会相同吗?
clustering
例如:当单线程处理请求处理能力结束时,AFAIK 的主要优势就开始了。测试环境:你是如何运行你的测试的?在什么环境下?环境被隔离了?在多少个核心上?什么样的处理器?
意义:为什么你认为你的结果代表了显着差异?你是如何测试样本之间的差异的?
推荐阅读
- google-maps - 您可以从 SAP GUI 屏幕嵌入 html 视图(例如谷歌地图)吗?
- python - 使用拆分字符串命名文件
- python - STFT 和 DWT 输入数据的深度学习参数
- uipath - UiPath Orchestrator 机器人未在作业中显示
- sql - “文本”附近的语法不正确
- java - 使用 Java8 Stream 迭代 java 嵌套列表
- machine-learning - 谁能在下面给定的 CNN 模型代码中找到神经元的数量和内核大小?我想在图表中显示层内的每个神经元
- python-3.x - 我不知道我在脚本中的错在哪里(python3)
- javascript - 从 javascript 中的 JSP 访问 java 对象
- c# - 调用隐藏接口方法