benchmark pm2的cluster模式发布web app的性能与相关问题解决方法

pm2以cluster集群方式发布app,能够高效地利用多核cpu,有效提高吞吐量。在上周对公司的redmine服务器进行性能调优后,深感ruby on rails的性能低下,此次测试nodejs的sails框架,被其性能深深折服。node

 

如下是使用pm2发布nodejs 应用的经历:git

一:记录出现的问题记录。github

1. pm2 start app.js -i 0 web

   当使用以上指令时,出现提示说pm2 的cluster模式很是不稳定,建议不使用。可是官网上面倒是推荐使用,为何呢?sql

原来个人node版本太低,只有0.10.36,建议安装0.11.x以上的版本,而后再运行此命令,才能启动cluster模式。apache

2. 当我升级node到了0.12.13版以后,运行却发现依然是mode:fork模式。以下图:ruby

──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name   │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ app            │ 0 │ fork    │ 15375 │ online │ 0 │ 0s │ 19.297 MB │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘服务器

缘由是:pm2在问题一中启动以后并无关闭,而是一直以deamon的形式运行着,并且会保持其启动模式,也就是说以前若是启动deamon是fork模式,那么以后你用pm2 start app.js -i 2时,即便指定了-i 也再也不生效。app

解决办法有两种:框架

1. 使用-f 参数强制其更换启动模式:pm2 start app.js -i 2 -f

2. 使用pm2 kill ,关闭deamon,而后从新使用pm2 start app.js开启。

 

2、nodejs性能

为了可以测试node在接近实际应用场景下地性能,我采用了一个sails框架,方便我快速搭建一个有sql数据访问的web app的性能。

而后在我买的一个ECS云服务器上测试,我云服务器的性能很是差,只有单核CPU+512内存,可是测试下来却发现,nodejs的异步特性使得其性能表现极为让人震惊。如下是用pm2以单进程形式发布的app, 请求一个包含sql查找操做的URL, ab测试结果以下:

ab -n 1000 -c 160 http://localhost:1337/User/find/1
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:
Server Hostname: localhost
Server Port: 1337

Document Path: /User/find/1
Document Length: 40 bytes

Concurrency Level: 160
Time taken for tests: 3.724 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 464566 bytes
HTML transferred: 40000 bytes
Requests per second: 268.52 [#/sec] (mean)
Time per request: 595.862 [ms] (mean)
Time per request: 3.724 [ms] (mean, across all concurrent requests)
Transfer rate: 121.82 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 2.9 0 14
Processing: 213 579 112.2 588 959
Waiting: 213 578 112.2 587 958
Total: 213 580 112.2 588 961

Percentage of the requests served within a certain time (ms)
50% 588
66% 600
75% 611
80% 618
90% 633
95% 787
98% 898
99% 943
100% 961 (longest request)
View Code

能够看出,单进程状况下,抗住了将近270次请求,并且该请求仍是包含sql操做的哦。

而后,换成用pm2 cluster模式,3个nodes, 性能反倒变成了120次,我想应该有两个缘由,1是内存受限,2是单核CPU下,采用cluster只会增长cpu切换带来的负面影响,下降了cpu的有效利用率。

 

好了,若是各位对与node的性能感兴趣能够到个人github上clone sailsBench项目下来【参考1】,尝试下用pm2发布,而后用ab测试其性能。

 

参考:

1. https://github.com/todototry/sailsBench/

相关文章
相关标签/搜索