登录  /  注册
首页 > php框架 > Swoole > 正文

讲解swoole HTTP服务器中异步MySQL

coldplay.xixi
发布: 2021-03-17 10:37:47
转载
1732人浏览过

讲解swoole HTTP服务器中异步MySQL

还是直接上代码:

<?php$http = new <a style='color:#f60; text-decoration:underline;' href="https://www.php.cn/zt/53189.html" target="_blank">swoole</a>_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中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:CSDN网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号