压测工具 hey/ghz/wrk/JMeter
这个工具干嘛的
一句话,gRPC服务压测、分析工具
安装
brew install ghz
常用参数说明
- -protoset 指定的 protoset 文件
- --call 调用的方法名(包名.服务.方法名)
- -proto 指定的proto文件
- -d proto请求中的参数
- -cpus 使用的cpu核数,看自己电脑,我的电脑是4核的就设置为4
- -concurrency 工作的线程数,默认50个,这个值建议设置为CPU核数的2倍
- -n 所有请求的数量,默认200个
- -t 每个请求的超时时间,默认20s,0的话表示不限制
- -c 并发请求数
开始使用
cli运行模式
ghz --insecure \
--proto ./user.proto \
--call user.User.getUser \
-d '{"id":"3"}' \
127.0.0.1:8081
输出说明
1000ms毫秒 = 1秒
| 输出项 | 含义 |
| ---- | ---- |
| count | 已完成的请求总数,包括成功的和失败的请求 |
| total | 从开始到结束的测试总时间。 |
| slowest | 最慢的请求的耗时(ms) |
| fastest | 最快的请求的耗时(ms) |
| average | 平均的请求耗时(ms) |
| requests/sec | 每秒的请求数量,计算公式:count/total |
| Response time histogram | 所有的请求按照响应时间的直方图统计 |
| Latency distribution | 延迟分布统计 |
| Status code distribution | 都产生了哪些状态码 |
Summary:
Count: 200
Total: 67.09 ms
Slowest: 29.19 ms
Fastest: 0.63 ms
Average: 8.10 ms
Requests/sec: 2981.01
Response time histogram:
0.629 [1] |∎
3.485 [16] |∎∎∎∎∎∎∎∎∎
6.341 [54] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
9.197 [71] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
12.053 [15] |∎∎∎∎∎∎∎∎
14.910 [27] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
17.766 [11] |∎∎∎∎∎∎
20.622 [1] |∎
23.478 [3] |∎∎
26.334 [0] |
29.191 [1] |∎
Latency distribution:
10 % in 3.82 ms
25 % in 4.86 ms
50 % in 6.87 ms
75 % in 10.75 ms
90 % in 14.44 ms
95 % in 15.23 ms
99 % in 21.29 ms
Status code distribution:
[OK] 200 responses
如若不习惯这种输出样式,也可以生成html文件,更加直观查看
ghz --insecure \
--proto ./user.proto \
--call user.User.getUser \
-d '{"id":"3"}' \
-o test.html -O html \
127.0.0.1:8081
ghz版本为0.96.0及以上
运行后,目录下会多出一个 test.html文件
代码中使用
示例
package main
import (
"log"
"os"
"github.com/bojand/ghz/printer"
"github.com/bojand/ghz/runner"
"github.com/golang/protobuf/proto"
pb "github.com/lixd/grpc-go-example/helloworld/helloworld"
)
// 官方文档 https://ghz.sh/docs/intro.html
func main() {
// 组装BinaryData
item := pb.HelloRequest{Name: "lixd"}
buf := proto.Buffer{}
err := buf.EncodeMessage(&item)
if err != nil {
log.Fatal(err)
return
}
report, err := runner.Run(
// 基本配置 call host proto文件 data
"helloworld.Greeter.SayHello", // 'package.Service/method' or 'package.Service.Method'
"localhost:50051",
runner.WithProtoFile("../helloworld/helloworld/hello_world.proto", []string{}),
runner.WithBinaryData(buf.Bytes()),
runner.WithInsecure(true),
runner.WithTotalRequests(10000),
// 并发参数
runner.WithConcurrencySchedule(runner.ScheduleLine),
runner.WithConcurrencyStep(10),
runner.WithConcurrencyStart(5),
runner.WithConcurrencyEnd(100),
)
if err != nil {
log.Fatal(err)
return
}
// 指定输出路径
file, err := os.Create("report.html")
if err != nil {
log.Fatal(err)
return
}
rp := printer.ReportPrinter{
Out: file,
Report: report,
}
// 指定输出格式
_ = rp.Print("html")
}
延伸
- 支持 prototest 模式请求,应对多个proto嵌套
- 支持调度算法逻辑策略请求
- 支持将测试用的各个参数写到配置文件中请求,方便重复利用、归档
- 不仅仅支持命令行模式运行,也支持在代码编程中运行
………………