hyperf操作mysql数据库

如何用hyperf操作mysql数据库

方法如下

1、安装包`composer require hyperf/db-connection`

2、修改配置文件`.env`和`config/autoload/database.php`中的数据库配置

3、添加路由

`Router::get('/db', 'App\Controller\DbController@index');`

4、添加controller

```
input('id', 1);
$list = Db::connection('default')->select("select *from user where id = $id");
return $list;
}
}

```

5、ab压力测试

```
[root@iZbp13ph356ra22ldly01lZ hyperf-skeleton]# ab -n 1000 -c 1000 http://118.31.251.197:9501/db?id=3
This is ApacheBench, Version 2.3
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 118.31.251.197 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software: Hyperf
Server Hostname: 118.31.251.197
Server Port: 9501

Document Path: /db?id=3
Document Length: 1280 bytes

Concurrency Level: 1000
Time taken for tests: 0.675 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 1425000 bytes
HTML transferred: 1280000 bytes
Requests per second: 1482.57 [#/sec] (mean)
Time per request: 674.504 [ms] (mean)
Time per request: 0.675 [ms] (mean, across all concurrent requests)
Transfer rate: 2063.15 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 2 49 3.4 50 54
Processing: 44 350 139.9 374 578
Waiting: 4 350 140.0 374 578
Total: 57 398 137.6 423 619

Percentage of the requests served within a certain time (ms)
50% 423
66% 475
75% 508
80% 530
90% 582
95% 598
98% 612
99% 616
100% 619 (longest request)

```

发现确实效率很高

更多参考`https://www.cnblogs.com/hu308830232/p/15338166.html`

    A+
发布日期:2021年12月28日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: