swoole异步mysql有什么用,学习猿地-讲解swoole HTTP服务器中异步MySQL

本文探讨了使用Swoole框架进行异步MySQL查询的PHP示例,与同步查询的mysqli对比。实验显示,异步方式显著提高了并发请求性能。通过`ab`测试,异步查询速度达到676.82 qps,而同步仅361.67 qps,展示了在高并发场景下的优势。
摘要由CSDN通过智能技术生成

f3385ac578637072f3d69c4a3c1c2b8e.jpg

还是直接上代码:<?php $http = new swoole_http_server("0.0.0.0", 9501);$http->on('request', function($request, $response){

$swoole_mysql1 = new Swoole\Coroutine\MySQL(); $swoole_mysql2 = new Swoole\Coroutine\MySQL(); $swoole_mysql1->connect([ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => 'root', 'database' => 'swoole',

]); $swoole_mysql2->connect([ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => 'root', 'database' => 'swoole',

]); $res1 = $swoole_mysql1->query('SELECT * FROM data1'); $res2 = $swoole_mysql2->query('SELECT * FROM data2'); $response->header("Content-Type", "text/html; charset=utf-8"); $response->end("

Hello Swoole. #".count($res1).count($res2)."

");

});$http->start();

推荐(免费):swoole

使用浏览器访问。http://ip:9501

异步MySQL可以不需要等待第一条查询完成后再执行第二条,在访问不同服务器,不同数据库,不同的表时效果比较明显。

对比同步MySQL查询代码:<?php $http = new swoole_http_server("0.0.0.0", 9501);$http->on('request', function($request, $response){ $swoole_mysql1 = mysqli_connect('127.0.0.1', 'root', 'root', 'swoole', 3306); $swoole_mysql2 = mysqli_connect('127.0.0.1', 'root', 'root', 'swoole', 3306); $res1 = $swoole_mysql1->query('SELECT * FROM data1'); $res2 = $swoole_mysql2->query('SELECT * FROM data2'); $response->header("Content-Type", "text/html; charset=utf-8"); $response->end("

Hello Swoole. #".$res1->num_rows.$res2->num_rows."

");

});$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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值