search
HomeBackend DevelopmentPHP TutorialComparison between Swoole synchronization mode and coroutine mode (details)

The content of this article is about the comparison between Swoole synchronization mode and coroutine mode (details). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In modern advanced PHP development, Swoole brings more possibilities to PHP, such as: resident memory, coroutines, and the huge difference between the traditional Apache/FPM mode and the resident memory mode (synchronization) , I have done tests before, and everyone can intuitively feel the huge improvement in performance, but how much performance improvement has been brought after the arrival of the coroutine? What aspects of performance are improved? Let’s test it step by step.

Test articles on traditional Apache/FPM mode and resident memory mode (synchronization):
MixPHP comprehensive comparison test of concurrent performance

Advantages of coroutines

Comparison between coroutine mode and resident memory mode (synchronous)/traditional mode:

Resident mode/traditional mode are both synchronous blocking programming, because the same process Requests cannot be processed in parallel, so in order to improve concurrency, more processes can only be opened, usually more than 100 or more. Each process has a basic memory consumption, which adds up to a lot and is limited. Due to the limit on the total number of processes in Linux, the total number of concurrent processes cannot be exceeded. In addition, after there are too many processes, the CPU needs more thread switching, which wastes a lot of performance. Of course, compared with the traditional mode of FPM, which needs to start from scratch every time, the resident mode is still It's much better, but coroutines are obviously better.

Execution method of coroutine mode:

In coroutine mode, a process can execute N requests at the same time, but only one of them will be executed at the same time. That is to say, when executing to clients such as MySQL/Redis, due to the need to wait for the client's response, the resident mode/traditional mode is usually stupidly waiting for the response, and the coroutine will suspend the current coroutine at this time and switch to Other requests are processed in other coroutines, so the coroutine can handle N requests at the same time. Each additional request only needs to increase some memory consumption. Compared with increasing the memory consumption of a process, it is obviously much less, because the coroutine can Parallel processing, so usually you only need to configure the number of processes that is about 1 to 2 times the number of CPUs. Fewer processes bring less CPU thread switching and reduce a lot of performance losses.

Start testing

MixPHP is a high-performance PHP framework based on Swoole's three-mode FPM, resident memory, and coroutine modes. Since the framework has both resident memory mode and coroutine mode, it can be easily Convenient test results.

Test environment:

docker container, limited to 1 CPU.

Other parameters are as follows:

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

Code:

Simulate common HTTP development requirements and execute three SQL requests.

// 默认动作
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();
}

Test results

Resident memory mode (synchronous):

##8100838.65 8300683.788500688.5650100770.69##5050Coroutine mode:
Number of processes Number of concurrency RPS
300 304.90
300 378.95

#8100 834.128300837.50500
##RPS
##8
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 
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 
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 
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 
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 
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 
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 
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 
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 
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)

相关推荐:

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

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

The above is the detailed content of Comparison between Swoole synchronization mode and coroutine mode (details). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.