Node.js集群模式与非集群模式下Hello World服务器性能不符预期的求助
Node.js集群模式与非集群模式下Hello World服务器性能不符预期的求助
我在自己的10核Ubuntu机器上,对一个简单的Node.js "Hello World"服务器做了性能测试,分别测试了用PM2启动的集群模式和普通的非集群模式。测试工具用的是Apache Benchmark,设置了4种并发级别:10、100、1000和10000,所有测试的总请求数都固定为100000次。
结果让我非常意外:在所有并发级别下,非集群模式的性能至少比集群模式好1.5倍!
不同并发级别下的性能数据
| 并发级别 | 集群模式耗时(秒) | 非集群模式耗时(秒) |
|---|---|---|
| 10 | 20.1 | 6.4 |
| 100 | 17.4 | 7.0 |
| 1000 | 16.3 | 9.1 |
| 10000 | 27.3 | 18.1 |
我的预期与困惑
我原本的预期是:集群模式会把请求分发到10个CPU核心上并行处理,而非集群模式只有一个事件循环在单核心运行,所以集群模式的性能应该接近非集群模式的10倍才对。但从实际测试数据来看,结果和我的预期完全相反。
有没有其他开发者遇到过类似的情况?或者有人能帮忙分析一下,为什么集群模式的表现反而不如非集群模式?
服务器代码
//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"); });
服务器启动命令
# 集群模式(启动10个进程) $ pm2 start server.js -i 10 # 非集群模式 $ node server.js
性能测试命令
$ 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/
备注:内容来源于stack exchange,提问作者ariko stephen
相关产品推荐
相关产品推荐

