今天介绍一个gRPC压测的工具:ghz
1、参考文档:
https://github.com/bojand/ghz
https://ghz.sh/
2、下载
(1) mac用户直接:brew install ghz
(2) linux源码编译,在根据github官网的介绍,先使用git下载下来,然后按照里面的步骤编译即可
3、ghz -h 看一下命令常用的参数:
--insecure 使用明文及不安全传输测试
--proto 指定proto文件
--call proto文件中的方法
-d proto请求的参数
--cpus 使用的cpu核数,看自己电脑,我的电脑是4核的就设置为4
--concurrency 工作的线程数,默认50个,这个值建议设置为CPU核数的2倍,我的电脑是4核就设置为8
-n 所有请求的数量,默认200个
-t 每个请求的超时时间,默认20s,0的话表示不限制
4-1、启动自己之前写的基于kratos的helloworld项目的学生服务,测试里面的gRPC接口
❗️项目地址:https://gitee.com/huoyingwhw/kratos_study/tree/master/helloworld
4-2、先使用BloomRPC工具测试一下RPC接口是否通
5、
(1) ⚠️使用下面的压测命令会报错:
```
ghz --insecure --proto /Users/wanghongwei/Desktop/kratos_study/helloworld/api/student/v1/student.proto \
--call student.StudentService.ListStudent -d '{"pageNum": 1,"pageSize": 10}' \
--cpus 4 --concurrency 8 -n 200 -t 5s 127.0.0.1:9001
```
❗️报错内容如下:student.proto:5:8: open /Users/wanghongwei/Desktop/kratos_study/helloworld/api/student/v1/google/api/annotations.proto: no such file or directory
❗️是因为没有指定导入包的目录,由于我们使用的是kratos框架,框架的三方目录导入的地方在third_party中,需要加 --import-paths 参数指定一下
(2) ⚠️下面的命令还报错
```
ghz --insecure --proto /Users/wanghongwei/Desktop/kratos_study/helloworld/api/student/v1/student.proto \
--call student.StudentService.ListStudent -d '{"pageNum": 1,"pageSize": 10}' \
--import-paths /Users/wanghongwei/Desktop/kratos_study/helloworld/third_party \
--cpus 4 --concurrency 8 -n 200 -t 5s 127.0.0.1:9001
```
❗️报错内容如下:cannot find service "student.StudentService"
❗️原因是go package内容指定错了,在student.proto文件中,有一个go_package的设置,需要在--call命令后加上项目中使用的配置
(3) ✅正确的命令:
```
ghz --insecure --proto /Users/wanghongwei/Desktop/kratos_study/helloworld/api/student/v1/student.proto \
--call api.student.v1.StudentService.ListStudent -d '{"pageNum": 1,"pageSize": 10}' \
--import-paths /Users/wanghongwei/Desktop/kratos_study/helloworld/third_party \
--cpus 4 --concurrency 8 -n 200 -t 5s 127.0.0.1:9001
```
6、测试报告解析:
(1)⚠️ 如果服务不可访问(比如写错了ip或端口等-如果RPC服务跑在了9001端口但是实际测试写错了写成了9000试试),会出现下面的报错:
Summary:
Count: 200
Total: 26.83 ms
Slowest: 0 ns
Fastest: 0 ns
Average: 0.02 ms
Requests/sec: 7453.66
Response time histogram:
Latency distribution:
Status code distribution:
[Unavailable] 200 responses
Error distribution:
[200] rpc error: code = Unavailable desc = connection closed before server preface received
(2)✅ 正常情况下返回的结果:
Summary:
Count: 200 ❗️总请求数量
Total: 246.09 ms ❗️总耗时
Slowest: 63.60 ms ❗️最短耗时
Fastest: 2.86 ms ❗️最快耗时
Average: 8.75 ms ❗️平均耗时
Requests/sec: 812.72 ❗️QPS
Response time histogram: ❗️响应时间直方图
2.864 [1] |
8.938 [161] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
15.011 [28] |∎∎∎∎∎∎∎
21.084 [2] |
27.158 [0] |
33.231 [0] |
39.305 [0] |
45.378 [0] |
51.451 [0] |
57.525 [1] |
63.598 [7] |∎∎
Latency distribution: ❗️延迟分布
10 % in 4.17 ms
25 % in 4.74 ms
50 % in 5.77 ms
75 % in 8.00 ms
90 % in 12.22 ms
95 % in 14.34 ms
99 % in 62.60 ms
Status code distribution:
[OK] 200 responses