在现代化 PHP 高级开发中,Swoole 为 PHP 带来了更多可能,如:常驻内存、协程,关于传统的 Apache/FPM 模式与常驻内存模式(同步)的巨大差异,之前我做过测试,大家能直观的感受到性能的巨大提升,但是协程到来后,又带来了多少性能的提升呢?提升的又是哪方面的性能?下面逐步测试一下。

传统的 Apache/FPM 模式与常驻内存模式(同步)的测试文章:
MixPHP 并发性能全面对比测试

协程的优势

协程模式与常驻内存模式(同步)/传统模式相比:

常驻模式/传统模式都属于同步阻塞编程,由于同一个进程不能并行处理请求,所以为了提高并发,只能开启更多的进程,通常超过 100 甚至更多,每个进程都有基础的内存消耗,加起来就很多了,而且受限于 Linux 总进程数限制,并发总数无法突破,加上进程非常多之后,CPU 需要更多的线程切换,浪费了很多性能,当然相比 FPM 的传统模式每次都需从头开始,常驻模式还是要好非常多的,但是协程显然更加优秀。

协程模式的执行方式:

协程模式中一个进程可以同时执行 N 个请求,但同一时刻只执行其中的某一个请求,也就是说,当执行到 MySQL/Redis 这些客户端时,由于需要等待客户端响应,常驻模式/传统模式通常是在傻傻的等待响应,而协程这个时候会挂起当前协程,切换到其他协程中去处理其他请求,所以协程能同时处理 N 个请求,每增加一个请求只需增加一些内存消耗,相比增加一个进程的内存消耗,显然是少太多的,由于协程能并行处理,所以通常只需配置于 CPU 数量 1~2 倍左右的进程数即可,更少的进程带来更少的 CPU 线程切换,又减少很多性能损耗。

开始测试

MixPHP 是一个基于 Swoole 的FPM、常驻内存、协程三模 PHP 高性能框架,由于该框架同时具备常驻内存模式、协程模式,所以能很方便的测试结果。

测试环境:

docker 容器,限制只能使用 1 CPU。

其他参数如下:

```Server Name: mix-httpd
Framework Version: 1.1.0-RC
PHP Version: 7.2.9
Swoole Version: 4.1.0
Listen Addr: 127.0.0.1
Listen Port: 9501
```

代码:

模拟常用的 HTTP 开发需求,执行三个 SQL 请求。


// 默认动作
public function actionIndex()
{
PDO::createCommand("select * from `test` where id = 1")->queryOne();
PDO::createCommand("select * from `test` where id = 2")->queryOne();
return PDO::createCommand("select * from `test` limit 5")->queryAll();
}

测试结果

常驻内存模式(同步):

进程数 并发数 RPS
8 100 838.65
8 300 683.78
8 500 688.56
50 100 770.69
50 300 304.90
50 300 378.95

协程模式:

进程数 并发数 RPS
8 100 834.12
8 300 837.50
8 500 824.14

协程在本次测试中并没有像之前的传统 Apache/FPM 模式与常驻内存模式(同步)的测试一样展现出巨大的性能提升,说明:

  • 在少量能快速响应的 SQL 请求中,协程的提升并不明显,应该要在响应时间更大时,才能感受到协程优势。
  • 常驻内存模式的进程数配置过多,并发性能反而会降低,该问题同样适用于传统 Apache/FPM 模式。

常驻内存模式(同步)详细测试

首先 8 个 Worker 进程,并发 100 测试,RPS 为 838.65。


C:\Users\EDZ>ab -n 10000 -c 100 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (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.13.9
Server Hostname: www.a.com
Server Port: 80 Document Path: /
Document Length: 101 bytes Concurrency Level: 100
Time taken for tests: 11.924 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 1010000 bytes
Requests per second: 838.65 [#/sec] (mean)
Time per request: 119.239 [ms] (mean)
Time per request: 1.192 [ms] (mean, across all concurrent requests)
Transfer rate: 217.85 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 4
Processing: 20 118 18.3 118 195
Waiting: 19 118 18.4 118 195
Total: 20 118 18.4 119 195 Percentage of the requests served within a certain time (ms)
50% 119
66% 126
75% 130
80% 133
90% 141
95% 147
98% 155
99% 161
100% 195 (longest request)

然后使用 8 个 Worker 进程,并发 300 测试,RPS 为 683.78。


C:\Users\EDZ>ab -n 10000 -c 300 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (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.13.9
Server Hostname: www.a.com
Server Port: 80 Document Path: /
Document Length: 101 bytes Concurrency Level: 300
Time taken for tests: 14.624 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 1010000 bytes
Requests per second: 683.78 [#/sec] (mean)
Time per request: 438.735 [ms] (mean)
Time per request: 1.462 [ms] (mean, across all concurrent requests)
Transfer rate: 177.62 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 30.0 0 3000
Processing: 62 432 493.4 354 3457
Waiting: 54 431 488.1 354 3455
Total: 62 433 494.1 354 3457 Percentage of the requests served within a certain time (ms)
50% 354
66% 373
75% 385
80% 392
90% 411
95% 432
98% 3170
99% 3266
100% 3457 (longest request)

然后使用 8 个 Worker 进程,并发 500 测试,RPS 为 688.56。


C:\Users\EDZ>ab -n 10000 -c 500 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (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.13.9
Server Hostname: www.a.com
Server Port: 80 Document Path: /
Document Length: 101 bytes Concurrency Level: 500
Time taken for tests: 14.523 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 1010000 bytes
Requests per second: 688.56 [#/sec] (mean)
Time per request: 726.150 [ms] (mean)
Time per request: 1.452 [ms] (mean, across all concurrent requests)
Transfer rate: 178.87 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 30.0 0 3000
Processing: 102 707 618.4 596 3632
Waiting: 89 703 605.6 595 3629
Total: 102 707 618.9 596 3633 Percentage of the requests served within a certain time (ms)
50% 596
66% 620
75% 635
80% 645
90% 679
95% 3125
98% 3401
99% 3495
100% 3633 (longest request)

现在调整为 50 进程,100 并发测试,RPS 为 770.69。
进程的增加并没有带来并发量的提升。


C:\Users\EDZ>ab -n 10000 -c 100 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (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.13.9
Server Hostname: www.a.com
Server Port: 80 Document Path: /
Document Length: 101 bytes Concurrency Level: 100
Time taken for tests: 12.975 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 1010000 bytes
Requests per second: 770.69 [#/sec] (mean)
Time per request: 129.754 [ms] (mean)
Time per request: 1.298 [ms] (mean, across all concurrent requests)
Transfer rate: 200.20 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 42.4 0 3000
Processing: 10 128 499.8 56 7137
Waiting: 10 127 495.8 55 7137
Total: 11 129 503.3 56 7137 Percentage of the requests served within a certain time (ms)
50% 56
66% 72
75% 86
80% 97
90% 133
95% 179
98% 312
99% 3052
100% 7137 (longest request)

50 进程,300 并发测试,RPS 为 304.90。
对比 8 个进程时的结果,并发量降低非常明显,看来进程数过多并不能提升性能,反而会降低性能。


C:\Users\EDZ>ab -n 10000 -c 300 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (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.13.9
Server Hostname: www.a.com
Server Port: 80 Document Path: /
Document Length: 101 bytes Concurrency Level: 300
Time taken for tests: 32.798 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 1010000 bytes
Requests per second: 304.90 [#/sec] (mean)
Time per request: 983.942 [ms] (mean)
Time per request: 3.280 [ms] (mean, across all concurrent requests)
Transfer rate: 79.20 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 90.0 0 3001
Processing: 25 976 1339.8 189 3694
Waiting: 23 954 1316.5 188 3691
Total: 25 979 1341.0 189 3694 Percentage of the requests served within a certain time (ms)
50% 189
66% 289
75% 3094
80% 3113
90% 3184
95% 3249
98% 3315
99% 3375
100% 3694 (longest request)

50 进程,500 并发测试,RPS 为 378.95。


C:\Users\EDZ>ab -n 10000 -c 500 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (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.13.9
Server Hostname: www.a.com
Server Port: 80 Document Path: /
Document Length: 101 bytes Concurrency Level: 500
Time taken for tests: 26.389 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 1010000 bytes
Requests per second: 378.95 [#/sec] (mean)
Time per request: 1319.431 [ms] (mean)
Time per request: 2.639 [ms] (mean, across all concurrent requests)
Transfer rate: 98.44 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 79.4 0 3001
Processing: 64 1306 1434.7 341 3962
Waiting: 17 1224 1391.4 321 3959
Total: 65 1308 1435.2 342 3963 Percentage of the requests served within a certain time (ms)
50% 342
66% 3142
75% 3168
80% 3195
90% 3292
95% 3374
98% 3467
99% 3516
100% 3963 (longest request)

协程模式详细测试

首先 8 个 Worker 进程,并发 100 测试,RPS 为 834.12。


C:\Users\EDZ>ab -n 10000 -c 100 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (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.13.9
Server Hostname: www.a.com
Server Port: 80 Document Path: /
Document Length: 101 bytes Concurrency Level: 100
Time taken for tests: 11.989 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 1010000 bytes
Requests per second: 834.12 [#/sec] (mean)
Time per request: 119.886 [ms] (mean)
Time per request: 1.199 [ms] (mean, across all concurrent requests)
Transfer rate: 216.68 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 4
Processing: 84 119 9.8 122 165
Waiting: 84 119 9.8 122 164
Total: 84 119 9.8 123 165 Percentage of the requests served within a certain time (ms)
50% 123
66% 124
75% 125
80% 125
90% 126
95% 128
98% 131
99% 137
100% 165 (longest request)

然后使用 8 个 Worker 进程,并发 300 测试,RPS 为 837.50。


C:\Users\EDZ>ab -n 10000 -c 300 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (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.13.9
Server Hostname: www.a.com
Server Port: 80 Document Path: /
Document Length: 101 bytes Concurrency Level: 300
Time taken for tests: 11.940 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 1010000 bytes
Requests per second: 837.50 [#/sec] (mean)
Time per request: 358.207 [ms] (mean)
Time per request: 1.194 [ms] (mean, across all concurrent requests)
Transfer rate: 217.55 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 42.4 0 3001
Processing: 86 354 1043.0 161 7172
Waiting: 86 344 1011.9 160 7172
Total: 86 355 1044.5 161 7172 Percentage of the requests served within a certain time (ms)
50% 161
66% 182
75% 199
80% 212
90% 251
95% 302
98% 6103
99% 6135
100% 7172 (longest request)

然后使用 8 个 Worker 进程,并发 500 测试,RPS 为 824.14。


C:\Users\EDZ>ab -n 10000 -c 500 http://www.a.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (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.13.9
Server Hostname: www.a.com
Server Port: 80 Document Path: /
Document Length: 101 bytes Concurrency Level: 500
Time taken for tests: 12.134 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 1010000 bytes
Requests per second: 824.14 [#/sec] (mean)
Time per request: 606.690 [ms] (mean)
Time per request: 1.213 [ms] (mean, across all concurrent requests)
Transfer rate: 214.08 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 4
Processing: 92 332 585.3 198 6931
Waiting: 91 331 585.5 196 6931
Total: 92 332 585.3 198 6931 Percentage of the requests served within a certain time (ms)
50% 198
66% 242
75% 284
80% 334
90% 587
95% 932
98% 1216
99% 2390
100% 6931 (longest request)

MixPHP

GitHub: https://github.com/mixstart/m...
官网:http://www.mixphp.cn/

原文地址:https://segmentfault.com/a/1190000016383096

Swoole 同步模式与协程模式的对比的更多相关文章

  1. python进阶:Python进程、线程、队列、生产者/消费者模式、协程

    一.进程和线程的基本理解 1.进程 程序是由指令和数据组成的,编译为二进制格式后在硬盘存储,程序启动的过程是将二进制数据加载进内存,这个启动了的程序就称作进程(可简单理解为进行中的程序).例如打开一个 ...

  2. swoole深入学习 8. 协程 转

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yangyi2083334/article/ ...

  3. python3 - 多线程和协程速率测试对比

    多线程和协程都属于IO密集型,我通过以下用例测试多线程和协程的实际速率对比. 实例:通过socket客户端以多线程并发模式请求不同服务器端(这里服务器端分2种写法:第一种服务器通过协程实现,第二种服务 ...

  4. python3下multiprocessing、threading和gevent性能对比----暨进程池、线程池和协程池性能对比

    python3下multiprocessing.threading和gevent性能对比----暨进程池.线程池和协程池性能对比   标签: python3 / 线程池 / multiprocessi ...

  5. Coroutine(协程)模式与线程

    概念 协程(Coroutine)这个概念最早是Melvin Conway在1963年提出的,是并发运算中的概念,指两个子过程通过相互协作完成某个任务,用它可以实现协作式多任务,协程(coroutine ...

  6. Swoole 4.1.0 正式版发布,支持原生 Redis/PDO/MySQLi 协程化

    重大新特性 支持 Redis/PDO/MySQLi 从4.1.0版本开始支持了对PHP原生Redis.PDO.MySQLi协程化的支持. 可使用Swoole\Runtime::enableCorotu ...

  7. 协程与Swoole的原理,相关应用以及适用场景等

    什么是协程 协程(Coroutine)也叫用户态线程,其通过协作而不是抢占来进行切换.相对于进程或者线程,协程所有的操作都可以在用户态完成,创建和切换的消耗更低.协程是进程的补充,或者是互补关系. 要 ...

  8. {python之协程}一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二

    python之协程 阅读目录 一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二 一 引子 本 ...

  9. Zend 官方框架增加 Swoole 协程支持 !

    前言 Zend Framework 是 PHP 的官方框架,随着 Zend-Expressive-Swoole 0.2.2 的发布,率先支持了 Swoole 4 的协程功能,现在可以仅通过一个配置即可 ...

随机推荐

  1. APP漏洞自动化扫描专业评测报告(中篇)

    前言 上一篇中通过对阿里聚安全[1].360App漏洞扫描[2].腾讯金刚审计系统[3].百度移动云测试中心[4]以及AppRisk Scanner[5] 在收费情况.样本测试后的扫描时间对比和漏洞项 ...

  2. C语言高速入门系列(一)

    C语言高速入门系列(一)  本系列引言: 本教程的宗旨是将C语言入门的内容进行关键知识点的提纯,将一些笼统的废话去除; 再进行压缩,然后将本章的关键知识点做成路线图的,能够更加方便地掌握学习的方向; ...

  3. oc2---类

    // main.m // 第一个OC类,OC中的类其实本质就是一个结构体, 所以p这个指针其实就是指向了一个结构体,创建一个对象就是创建一个结构体指针, #import <Foundation/ ...

  4. 3-3 第三天 Promise 如何使用

    回调的方式来处理异步,目的是要保证一个执行顺序,先完成什么再去完成什么,它们的作用其实是相同的,显然回调更容易来书写,但是它难以维护,很容易遗漏错误处理代码而且无法使用return语句来返回这个值. ...

  5. canvas的自动画图

    <!DOCTYPE HTML><html><body> <canvas id="myCanvas" width="200&quo ...

  6. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

  7. 3.Ventuz Designer新建项目Demo

    Ventuz Designer新建项目Demo 1.打开ventuz,点Recent Projects>New Project,在弹出的界面填写具体项目信息,如下图: 图1.1 图1.2 2.在 ...

  8. Vue-router记录

    一.嵌套路由默认选中第一个子路由 可以给主路由加一个重定向的属性,把路径指向相应的子路由. { path: '/home', name: 'Home', //重定向 redirect: '/home/ ...

  9. sql server中使用组合索引需要注意的地方

    一.使用组合索引需要注意的地方 1.索引应该建在选择性高的字段上(键值唯一的记录数/总记录条数),选择性越高索引的效果越好.价值越大,唯一索引的选择性最高: 2.组合索引中字段的顺序,选择性越高的字段 ...

  10. 使用Caffe预测遇到的问题

    1. 在使用网络预测图像时, prediction = net.predict( [input_image] ) 出现: net.image_dims[0] 不是整数情况, (2).甚至以为np.ze ...