swoole HTTP服务器中异步MySQL
来源:互联网 发布:金十数据新闻 编辑:程序博客网 时间:2024/06/05 09:22
还是直接上代码:
<?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("<h1>Hello Swoole. #".count($res1).count($res2)."</h1>");});$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("<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-serverServer 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] receivedConnection 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-serverServer 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] receivedConnection 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)
阅读全文
1 0
- swoole HTTP服务器中异步MySQL
- php swoole异步处理mysql
- [李景山php] swoole 异步 tcp服务器使用
- [李景山php] swoole 异步 mysql 连接
- Swoole中异步连接客户端/请求API
- 抛弃php-fpm,nginx +swoole 配置http服务器
- swoole创建异步客户端
- [Swoole入门]-异步毫秒定时器
- 使用swoole异步发送邮件
- swoole和redis异步任务
- 问题:swoole-http服务器发送POST请求服务器端超时(100-continue问题)
- 基于swoole的高性能的mvc的http服务器框架--swoolefy
- socketserver 模块异步http服务器代码.
- 异步http提交数据到服务器
- swoole实现socket服务器初探
- 在服务器上安装swoole
- swoole--php异步扩展,记录下
- PHP的异步并行网络扩展swoole
- 依赖注入和控制反转
- 洛谷Oj-约数研究-数学
- STM32F429之IO操作
- Chapter 1 Section 1.4 Vocabulary
- mysql大数据量之limit优化
- swoole HTTP服务器中异步MySQL
- Filter过滤器周期、自动登录、编码、
- 50天iOS挑战(Swift)
- iOS 文件预览的四种方法
- JSP属性范围
- 开源爬虫: Heritrix 1.14.4 安装/使用
- Python—切片操作
- 机器学习--决策树
- Jsp中<c:foreach>如何循环得到值,进行修改