i在集群模式(使用pm2)和我的10-CoreubuntuMachine上的简单node.js“ Hello World”服务器上进行了性能测试。我使用Apache基准测试工具进行了测试,并并发水平为10、100、1000和10、000。对于所有并发级别,请求总数保留为100,000。集群和非集群列中的数字是在秒内花费的总时间,以在不同的并发级别完成所有100,000个请求。性能结果表明,在所有并发级别上,非群集模式至少比聚类模式更好。
在不同并发级别的表现表
| Concurrency Level | Cluster Mode (s) | Non-Cluster Mode(s) |
|---------------------|------------------|---------------------|
| 10 | 20.1 | 6.4 |
| 100 | 17.4 | 7.0 |
| 1000 | 16.3 | 9.1 |
| 10000 | 27.3 | 18.1 |
我的期望
集群模式应更快地执行10倍,因为它在10个CPU内部分布流量,与非集群模式不同,单个核心循环在单个核心上处理所有请求。从性能表中可以看出,实际结果远非期望
其他人有类似的结果吗?对为什么群集模式的性能更糟的任何见解?为什么群集模式用于生产中的节点服务器?服务器代码
//server.js
import http from "http"
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello, World!");
});
server.listen(3000, () => {
console.log("Server running on port 3000");
});
run服务器
$ pm2 start server.js -i 10 (cluster mode)
$ node server.js (non-cluster mode)
表现测试命令
$ ab -n 100000 -c 10 http://localhost:3000/
$ ab -n 100000 -c 100 http://localhost:3000/
$ ab -n 100000 -c 1000 http://localhost:3000/
$ ab -n 100000 -c 10000 http://localhost:3000/
我怀疑Hello World操作还不够资源,并且您被PM2负载平衡器固定了。
IT需要在多个实例上分发请求,这些实例固有地添加了开销。而且,由于您的服务器返回的速度差不多。结果听起来很合理。要检查,尝试使用
setTimeout
的500ms