使用Apache基准测试的PM2 nodeJS集群模式测试

时间:2020-03-14 14:21:33

标签: node.js pm2 apachebench

我已经建立了一个nodeJS应用程序,它只有一条路由“ /”,并且我使用Nginx作为反向代理。因此应用程序流程如下:

  • 用户将请求发送到Nginx服务器。
  • 并且根据位置'/'Nginx服务器将请求传递给节点服务器。

从nodeJS,“ /”路由向客户端发送一个HTML文件作为响应。对于负载测试,我使用了Apache基准测试。

用于测试的Apache基准命令:

ab -k -c 250 -n 10000 http://localhost/

在以下两种情况下,请检查apache基准测试响应:

情况1:未启用群集模式时。 (没有pm2,没有集群的简单nodeJS服务器,例如: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启动index.js -i 4(4个集群))

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)

现在,如果您在两种情况下都比较每秒请求数,则将在没有使用高于pm2集群的集群模式时看到每秒请求数(1049.26 [#/ sec](平均))模式(708.79 [#/ sec](平均))我不明白为什么会这样?据我所知,集群模式用于实现更高级别的并发,但是为什么结果会有冲突?

1 个答案:

答案 0 :(得分:0)

我尝试使用不同的参数进行聚类:

  • 无进程

  • 计算

    for(let i = 1; i <= 50000000; i++){
       r += i;
    }
    
  • 发送文件

  • 并发请求数

这里是git repo

这是我的结论:

  • 对于提供文件,群集没有意义。我认为网络是这里的瓶颈,而群集却无济于事。
  • 对于计算而言,群集确实有意义,因为它使事件循环保持繁忙,而如果进行群集,则有多个事件循环需要保持繁忙。在进行计算测试时,我按htop检查了服务器核心进程,并认为与CPU 100%繁忙的集群数量相同。性能乘以群集计数,例如,如果我制作了6个节点群集,性能将提高6倍。
  • 群集多于计算机上的CPU内核没有任何意义。我建议为操作系统保留一个内核。

我建立了一个存储库,并在readme文件中写下了详细的结果。