Swoole HTTP 서버의 비동기 MySQL 설명

coldplay.xixi
풀어 주다: 2021-03-17 10:37:47
앞으로
1878명이 탐색했습니다.

Swoole HTTP 서버의 비동기 MySQL 설명

또는 코드로 직접 이동:

<?php$http = new swoole_http_server("0.0.0.0", 9501);$http->on(&#39;request&#39;, function($request, $response){
    $swoole_mysql1 = new Swoole\Coroutine\MySQL();    $swoole_mysql2 = new Swoole\Coroutine\MySQL();    $swoole_mysql1->connect([        &#39;host&#39; => &#39;127.0.0.1&#39;,        &#39;port&#39; => 3306,        &#39;user&#39; => &#39;root&#39;,        &#39;password&#39; => &#39;root&#39;,        &#39;database&#39; => &#39;swoole&#39;,
    ]);    $swoole_mysql2->connect([        &#39;host&#39; => &#39;127.0.0.1&#39;,        &#39;port&#39; => 3306,        &#39;user&#39; => &#39;root&#39;,        &#39;password&#39; => &#39;root&#39;,        &#39;database&#39; => &#39;swoole&#39;,
    ]);    $res1 = $swoole_mysql1->query(&#39;SELECT * FROM data1&#39;);    $res2 = $swoole_mysql2->query(&#39;SELECT * FROM data2&#39;);    $response->header("Content-Type", "text/html; charset=utf-8");    $response->end("<h1>Hello Swoole. #".count($res1).count($res2)."</h1>");

});$http->start();
로그인 후 복사

권장(무료): swoole

브라우저를 사용하여 액세스하세요. http://ip:9501
비동기 MySQL은 두 번째 쿼리를 실행하기 전에 첫 번째 쿼리가 완료될 때까지 기다릴 필요가 없으며 다른 서버, 다른 데이터베이스 및 다른 테이블에 액세스할 때 그 효과가 더욱 분명해집니다.
동기식 MySQL 쿼리 코드 비교:

<?php$http = new swoole_http_server("0.0.0.0", 9501);$http->on(&#39;request&#39;, function($request, $response){    $swoole_mysql1 = mysqli_connect(&#39;127.0.0.1&#39;, &#39;root&#39;, &#39;root&#39;, &#39;swoole&#39;, 3306);    $swoole_mysql2 = mysqli_connect(&#39;127.0.0.1&#39;, &#39;root&#39;, &#39;root&#39;, &#39;swoole&#39;, 3306);    $res1 = $swoole_mysql1->query(&#39;SELECT * FROM data1&#39;);    $res2 = $swoole_mysql2->query(&#39;SELECT * FROM data2&#39;);    $response->header("Content-Type", "text/html; charset=utf-8");    $response->end("<h1>Hello Swoole. #".$res1->num_rows.$res2->num_rows."</h1>");

});$http->start();
로그인 후 복사

동기식 코드는 PHP 기본 방법을 사용하여 데이터를 쿼리합니다.
ab를 사용하여 성능 테스트에 두 가지 쿼리 방법을 넣어 보겠습니다.
ab -c 100 -n 1000 http://127.0.0.1:9501/
비동기 쿼리:

Server Software:        swoole-http-server
Server Hostname:        127.0.0.1Server Port:            9501Document Path:          /
Document Length:        30 bytesConcurrency Level:      100Time taken for tests:   1.477 secondsComplete requests:      1000Failed requests:        0Write errors:           0Total transferred:      193000 bytesHTML transferred:       30000 bytesRequests per second:    676.82 [#/sec] (mean)Time per request:       147.749 [ms] (mean)
Time per request:       1.477 [ms] (mean, across all concurrent requests)
Transfer rate:          127.57 [Kbytes/sec] received

Connection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    1   1.8      0       7Processing:     4  140  24.0    145     156Waiting:        0  140  24.1    145     156Total:          7  140  22.6    145     160Percentage of the requests served within a certain time (ms)  50%    145
  66%    146
  75%    148
  80%    148
  90%    150
  95%    152
  98%    153
  99%    154
 100%    160 (longest request)
로그인 후 복사

동기 쿼리:

Server Software:        swoole-http-server
Server Hostname:        127.0.0.1Server Port:            9501Document Path:          /
Document Length:        30 bytesConcurrency Level:      100Time taken for tests:   2.765 secondsComplete requests:      1000Failed requests:        0Write errors:           0Total transferred:      193000 bytesHTML transferred:       30000 bytesRequests per second:    361.67 [#/sec] (mean)Time per request:       276.493 [ms] (mean)
Time per request:       2.765 [ms] (mean, across all concurrent requests)
Transfer rate:          68.17 [Kbytes/sec] received

Connection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    0   0.4      0       2Processing:     4  262  48.5    272     295Waiting:        4  262  48.5    272     295Total:          6  262  48.2    272     295Percentage of the requests served within a certain time (ms)  50%    272
  66%    278
  75%    281
  80%    284
  90%    287
  95%    291
  98%    293
  99%    294
 100%    295 (longest request)
로그인 후 복사

위 내용은 Swoole HTTP 서버의 비동기 MySQL 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:csdn.net
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!