zoukankan      html  css  js  c++  java
  • benchmark pm2的cluster模式发布web app的性能与相关问题解决方法

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

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

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

    1. pm2 start app.js -i 0 

       当使用以上指令时,出现提示说pm2 的cluster模式非常不稳定,建议不使用。但是官网上面却是推荐使用,为什么呢?

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

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

    ──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐
    │ 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 也不再生效。

    解决办法有两种:

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

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

    二、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/

  • 相关阅读:
    Webdriver启动Firefox浏览器后,页面显示空白
    Windows+Python+Selenium基础篇之1-环境搭建
    office2010无法卸载问题
    .NET使用FastDBF读写DBF
    并发编程相关概念
    C#的多线程简洁笔记
    asp.net core 3.1 入口:Program.cs中的Main函数
    【WPF学习】第四十四章 图画
    .NET知识梳理——1.泛型Generic
    C#个推SDK推送安卓+iOS
  • 原文地址:https://www.cnblogs.com/ToDoToTry/p/4438523.html
Copyright © 2011-2022 走看看