温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

swoole HTTP服务器中异步MySQL的示例分析

发布时间:2021-03-17 11:44:15 来源:亿速云 阅读:150 作者:清风 栏目:编程语言

这篇“swoole HTTP服务器中异步MySQL的示例分析”除了程序员外大部分人都不太理解,今天小编为了让大家更加理解“swoole HTTP服务器中异步MySQL的示例分析”,给大家总结了以下内容,具有一定借鉴价值,内容详细步骤清晰,细节处理妥当,希望大家通过这篇文章有所收获,下面让我们一起来看看具体内容吧。

直接上代码:

<?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("<h2>Hello Swoole. #".count($res1).count($res2)."</h2>");

});$http->start();

使用浏览器访问。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("<h2>Hello Swoole. #".$res1->num_rows.$res2->num_rows."</h2>");

});$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的示例分析”这一关键问题有了一定的理解,具体使用情况还需要大家自己动手实验使用过才能领会,快去试试吧,如果想阅读更多相关知识点的文章,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI