我只是在Node.js
中尝试集群。我写了一个fibonacci
例子,在其中我使用集群服务器和非集群服务器调用函数,如下所示。
// 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)
}
现在,我尝试使用artillery
对这两个服务器的性能进行基准测试,但是非群集服务器似乎更快。怎么会来?
// 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
答案 0 :(得分:0)
node cluster文档本身说,由于操作系统调度程序的不稳定性,工作程序分布往往非常不平衡,这会影响性能。另请参阅cluster
nginx
和iptables
负载均衡器之间的this比较。
样本大小:为什么选择此样本大小?您确定不同样本量的结果是否相同?例如:AFAIK当单线程进程请求处理功能结束时,clustering
的主要优势就开始了。
测试环境:您如何运行测试?在哪个环境下?环境是孤立的吗?在多少个核心上?什么样的处理器?
重要性:为什么您认为结果代表significant差异?您如何测试样本之间的差异?