Maison > développement back-end > tutoriel php > Comparaison entre le mode de synchronisation Swoole et le mode coroutine (détails)

Comparaison entre le mode de synchronisation Swoole et le mode coroutine (détails)

不言
Libérer: 2023-04-04 07:28:02
original
1795 Les gens l'ont consulté

Le contenu de cet article concerne la comparaison entre le mode de synchronisation Swoole et le mode coroutine (détails). Il a une certaine valeur de référence. Les amis dans le besoin peuvent s'y référer.

Dans le développement PHP avancé moderne, Swoole apporte plus de possibilités à PHP, telles que : la mémoire résidente, les coroutines et l'énorme différence entre le mode Apache/FPM traditionnel et le mode mémoire résidente (synchronisation), j'ai fait tests avant, et tout le monde peut ressentir intuitivement l'énorme amélioration des performances, mais quelle amélioration des performances a été apportée après l'arrivée de la coroutine ? Quels aspects de la performance sont améliorés ? Testons-le étape par étape.

Articles de test sur le mode Apache/FPM traditionnel et le mode mémoire résidente (synchronisation) :
Test comparatif complet des performances de concurrence MixPHP

Avantages des coroutines

Comparaison entre le mode coroutine et le mode mémoire résident (synchrone)/mode traditionnel :

Le mode résident/mode traditionnel sont tous deux une programmation de blocage synchrone, car le même processus Les requêtes ne peuvent pas être traitées dans en parallèle, donc afin d'améliorer la concurrence, plus de processus ne peuvent être ouverts, généralement plus de 100 ou même plus. Chaque processus a une consommation de mémoire de base, ce qui représente beaucoup et est limitée par le processus Linux total. À la limite du nombre, le nombre total de concurrence ne peut pas être dépassé. Avec un grand nombre de processus, le CPU a besoin de plus de commutation de threads, ce qui gaspille bien sûr beaucoup de performances par rapport au mode FPM traditionnel, qui doit démarrer. à partir de zéro à chaque fois, le mode résident est toujours bien meilleur, mais les coroutines sont évidemment meilleures.

Méthode d'exécution du mode coroutine :

En mode coroutine, un processus peut exécuter N requêtes en même temps, mais une seule d'entre elles sera exécutée en même temps C'est-à-dire que lors de l'exécution sur des clients tels que MySQL/Redis, en raison de la nécessité d'attendre la réponse du client, le mode résident/mode traditionnel attend généralement bêtement la réponse, et la coroutine suspendra la coroutine actuelle. à ce moment-là et passer à D'autres requêtes sont traitées dans d'autres coroutines, de sorte que la coroutine peut gérer N requêtes en même temps. Chaque requête supplémentaire n'a besoin que d'augmenter une certaine consommation de mémoire. Par rapport à l'augmentation de la consommation de mémoire d'un processus, c'est évidemment le cas. beaucoup moins, car la coroutine peut effectuer un traitement parallèle, vous n'avez donc généralement besoin de configurer qu'un nombre de processus qui est environ 1 à 2 fois le nombre de processeurs. Moins de processus entraînent moins de commutation de threads de processeur et réduisent beaucoup de pertes de performances.

Commencer les tests

MixPHP est un framework PHP hautes performances basé sur les modes FPM, mémoire résidente et coroutine à trois modes de Swoole. Étant donné que le framework dispose à la fois d'un mode mémoire résident et d'un mode coroutine, il. peut être facilement des résultats de test pratiques.

Environnement de test :

conteneur docker, limité à 1 CPU.

Les autres paramètres sont les suivants :

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
Copier après la connexion

Code :

Simulez les exigences de développement HTTP courantes et exécutez trois requêtes 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();
}
Copier après la connexion

Résultats des tests

Mode mémoire résident (synchrone) :

进程数并发数RPS
8100838.65
8300683.78
8500688.56
50100770.69
50300304.90
50300378.95

Mode coroutine :

进程数并发数RPS
8100834.12
8300837.50
8500824.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)
Copier après la connexion

然后使用 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)
Copier après la connexion

然后使用 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)
Copier après la connexion

现在调整为 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)
Copier après la connexion

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)
Copier après la connexion

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)
Copier après la connexion

协程模式详细测试

首先 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)
Copier après la connexion

然后使用 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)
Copier après la connexion

然后使用 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)
Copier après la connexion

相关推荐:

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

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal