• 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/

  • 相关阅读:
    cerr与cout区别
    Apache Flink 进阶(三):Checkpoint 原理解析与应用实践
    一张图轻松掌握 Flink on YARN 应用启动全流程(上)
    开篇 | 揭秘 Flink 1.9 新架构,Blink Planner 你会用了吗?
    阿里第一颗芯片问世,平头哥发布最强AI芯片含光800
    Kubernetes 从懵圈到熟练:集群服务的三个要点和一种实现
    研发效能提升 36 计第二课:照亮问题,效能提升从可视化交付过程开始
    如何抢占云栖大会C位?史上最强强强攻略来了
    从零开始入门 K8s| 阿里技术专家详解 K8s 核心概念
    Serverless Kubernetes全面升级2.0架构:支持多命名空间、RBAC、CRD、PV/PVC等功能
  • 原文地址:https://www.cnblogs.com/ToDoToTry/p/4438523.html
Copyright © 2020-2023  润新知