各类知识收集,PHP技术分享与解决方案各类知识收集,PHP技术分享与解决方案各类知识收集,PHP技术分享与解决方案

Str Tom,为分享PHP技术和解决方案,贡献一份自己的力量!
收藏本站(不迷路),每天更新好文章!
当前位置:首页 > CMS教程 > PHP

Swoole同步模式与协程模式之间的对比(详细)

管理员 2023-09-05
PHP
138

Swoole同步模式与协程模式之间的对比(详细)

内容导读

收集整理的这篇技术教程文章主要介绍了Swoole同步模式与协程模式之间的对比(详细),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含16972字,纯文字阅读大概需要25分钟

内容图文

本篇文章给大家带来的内容是关于Swoole同步模式与协程模式之间的对比 (详细) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

在现代化 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-httpdFramework   Version:   1.1.0-RCPHP         Version:   7.2.9Swoole      Version:   4.1.0Listen      Addr:      127.0.0.1Listen      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
8100838.65
8300683.78
8500688.56
50100770.69
50300304.90
50300378.95

协程模式:

进程数并发数RPS
8100834.12
8300837.50
8500824.14

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

  • 在少量能快速响应的 SQL 请求中,协程的提升并不明显,应该要在响应时间更大时,才能感受到协程优势。

  • 常驻内存模式的进程数配置过多,并发性能反而会降低,该问题同样适用于传统 Apache/FPM 模式。

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

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

C:UsersEDZ>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 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        nginx/1.13.9Server Hostname:        www.a.comServer Port:            80Document Path:          /Document Length:        101 bytesConcurrency Level:      100Time taken for tests:   11.924 secondsComplete requests:      10000Failed requests:        0Total transferred:      2660000 bytesHTML transferred:       1010000 bytesRequests 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] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    0   0.2      0       4Processing:    20  118  18.3    118     195Waiting:       19  118  18.4    118     195Total:         20  118  18.4    119     195Percentage 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:UsersEDZ>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 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        nginx/1.13.9Server Hostname:        www.a.comServer Port:            80Document Path:          /Document Length:        101 bytesConcurrency Level:      300Time taken for tests:   14.624 secondsComplete requests:      10000Failed requests:        0Total transferred:      2660000 bytesHTML transferred:       1010000 bytesRequests 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] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    0  30.0      0    3000Processing:    62  432 493.4    354    3457Waiting:       54  431 488.1    354    3455Total:         62  433 494.1    354    3457Percentage 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:UsersEDZ>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 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        nginx/1.13.9Server Hostname:        www.a.comServer Port:            80Document Path:          /Document Length:        101 bytesConcurrency Level:      500Time taken for tests:   14.523 secondsComplete requests:      10000Failed requests:        0Total transferred:      2660000 bytesHTML transferred:       1010000 bytesRequests 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] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    0  30.0      0    3000Processing:   102  707 618.4    596    3632Waiting:       89  703 605.6    595    3629Total:        102  707 618.9    596    3633Percentage 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:UsersEDZ>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 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        nginx/1.13.9Server Hostname:        www.a.comServer Port:            80Document Path:          /Document Length:        101 bytesConcurrency Level:      100Time taken for tests:   12.975 secondsComplete requests:      10000Failed requests:        0Total transferred:      2660000 bytesHTML transferred:       1010000 bytesRequests 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] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    1  42.4      0    3000Processing:    10  128 499.8     56    7137Waiting:       10  127 495.8     55    7137Total:         11  129 503.3     56    7137Percentage 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:UsersEDZ>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 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        nginx/1.13.9Server Hostname:        www.a.comServer Port:            80Document Path:          /Document Length:        101 bytesConcurrency Level:      300Time taken for tests:   32.798 secondsComplete requests:      10000Failed requests:        0Total transferred:      2660000 bytesHTML transferred:       1010000 bytesRequests 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] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    3  90.0      0    3001Processing:    25  976 1339.8    189    3694Waiting:       23  954 1316.5    188    3691Total:         25  979 1341.0    189    3694Percentage 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:UsersEDZ>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 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        nginx/1.13.9Server Hostname:        www.a.comServer Port:            80Document Path:          /Document Length:        101 bytesConcurrency Level:      500Time taken for tests:   26.389 secondsComplete requests:      10000Failed requests:        0Total transferred:      2660000 bytesHTML transferred:       1010000 bytesRequests 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] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    2  79.4      0    3001Processing:    64 1306 1434.7    341    3962Waiting:       17 1224 1391.4    321    3959Total:         65 1308 1435.2    342    3963Percentage 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:UsersEDZ>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 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        nginx/1.13.9Server Hostname:        www.a.comServer Port:            80Document Path:          /Document Length:        101 bytesConcurrency Level:      100Time taken for tests:   11.989 secondsComplete requests:      10000Failed requests:        0Total transferred:      2660000 bytesHTML transferred:       1010000 bytesRequests 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] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    0   0.3      0       4Processing:    84  119   9.8    122     165Waiting:       84  119   9.8    122     164Total:         84  119   9.8    123     165Percentage 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:UsersEDZ>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 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        nginx/1.13.9Server Hostname:        www.a.comServer Port:            80Document Path:          /Document Length:        101 bytesConcurrency Level:      300Time taken for tests:   11.940 secondsComplete requests:      10000Failed requests:        0Total transferred:      2660000 bytesHTML transferred:       1010000 bytesRequests 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] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    1  42.4      0    3001Processing:    86  354 1043.0    161    7172Waiting:       86  344 1011.9    160    7172Total:         86  355 1044.5    161    7172Percentage 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:UsersEDZ>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 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software:        nginx/1.13.9Server Hostname:        www.a.comServer Port:            80Document Path:          /Document Length:        101 bytesConcurrency Level:      500Time taken for tests:   12.134 secondsComplete requests:      10000Failed requests:        0Total transferred:      2660000 bytesHTML transferred:       1010000 bytesRequests 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] receivedConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    0   0.3      0       4Processing:    92  332 585.3    198    6931Waiting:       91  331 585.5    196    6931Total:         92  332 585.3    198    6931Percentage 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)

相关推荐:

PHP设计模式之调解者模式的深入解析_PHP教程

php安装模式mod_php和Fastcgi的选择与对比

以上就是Swoole同步模式与协程模式之间的对比 (详细)的详细内容,更多请关注Gxl网其它相关文章!

内容总结

以上是为您收集整理的Swoole同步模式与协程模式之间的对比(详细)全部内容,希望文章能够帮你解决Swoole同步模式与协程模式之间的对比(详细)所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

相关推荐

扫码关注

qrcode

QQ交谈

回顶部