【发布时间】:2020-06-26 06:02:39
【问题描述】:
我已经设置了一个 nodeJS 应用程序,它只有一个路由'/',并且我使用 Nginx 作为反向代理。所以应用流程如下:
- 用户将请求发送到 Nginx 服务器。
- 并根据位置'/' Nginx 服务器将请求传递给节点服务器。
从 nodeJS 中,'/' 路由将一个 HTML 文件作为响应发送给客户端。对于负载测试,我使用了 apache 基准测试。
用于测试的 Apache 基准测试命令:
ab -k -c 250 -n 10000 http://localhost/
请在以下两种情况下检查 apache benchmark 响应:
案例 1:集群模式未开启时。 (没有pm2,没有集群的简单nodeJS服务器ex:node index.js)
rails@rails-laptop:~$ ab -k -c 250 -n 10000 http://localhost/
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.10.3
Server Hostname: localhost
Server Port: 80
Document Path: /
Document Length: 134707 bytes
Concurrency Level: 250
Time taken for tests: 9.531 seconds
Complete requests: 10000
Failed requests: 0
Keep-Alive requests: 10000
Total transferred: 1350590000 bytes
HTML transferred: 1347070000 bytes
Requests per second: 1049.26 [#/sec] (mean)
Time per request: 238.264 [ms] (mean)
Time per request: 0.953 [ms] (mean, across all concurrent requests)
Transfer rate: 138390.37 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.8 0 6
Processing: 38 237 77.6 213 626
Waiting: 31 230 73.8 209 569
Total: 44 237 77.5 213 626
Percentage of the requests served within a certain time (ms)
50% 213
66% 229
75% 247
80% 280
90% 373
95% 395
98% 438
99% 538
100% 626 (longest request)
案例2:PM2集群模式开启时。(pm2 start index.js -i 4 (4 cluster))
rails@rails-laptop:~$ ab -k -c 250 -n 10000 http://localhost/
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.10.3
Server Hostname: localhost
Server Port: 80
Document Path: /
Document Length: 134707 bytes
Concurrency Level: 1
Time taken for tests: 14.109 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1350540000 bytes
HTML transferred: 1347070000 bytes
Requests per second: 708.79 [#/sec] (mean)
Time per request: 1.411 [ms] (mean)
Time per request: 1.411 [ms] (mean, across all concurrent requests)
Transfer rate: 93481.05 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 9
Processing: 1 1 1.2 1 35
Waiting: 0 1 0.9 1 21
Total: 1 1 1.2 1 35
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 2
95% 3
98% 5
99% 6
100% 35 (longest request)
现在,如果您比较两种场景中的每秒请求数,您将看到没有使用集群模式时的每秒请求数 (1049.26 [#/sec] (mean)) 高于 pm2 集群模式(708.79 [#/sec](平均值))。 我不明白为什么会这样?据我所知,集群模式是用来实现更高级别的并发,但是为什么结果会冲突呢?
【问题讨论】:
-
电脑有多少CPU?不知道是不是只有一个CPU,通过集群,性能越来越低。同样据我了解,您所做的只是网络任务,因此瓶颈没有处理,在这种情况下,集群不会做任何事情。尝试在响应之前进行一些耗时的计算,并检查基准。
-
它是一个 4 核 CPU。
标签: node.js pm2 apachebench