go-wrk 是一个用Go语言实现的轻量级的http基准测试工具,类似于wrk(还有ab、siege),本文将简单介绍一下如何使用go-wrk实现接口的性能(压力)测试。
安装 go-wrk
go get github.com/adeven/go-wrk
go-wrk 选项说明
#./go-wrk --helpUsage of ./go-wrk:-CA stringA PEM eoncoded CA's certificate file. (default "someCertCAFile")-H stringthe http headers sent separated by '\n' (default "User-Agent: go-wrk 0.1 benchmark\nContent-Type: text/html;")-b stringthe http request body-c intthe max numbers of connections used (default 100)-cert stringA PEM eoncoded certificate file. (default "someCertFile")-d stringdist mode-f stringjson config file-i TLS checks are disabled-k if keep-alives are disabled (default true)-key stringA PEM encoded private key file. (default "someKeyFile")-m stringthe http request method (default "GET")-n intthe total number of calls processed (default 1000)-p stringthe http request body data file-r in the case of having stream or file in the response,it reads all response body to calculate the response size-s stringif specified, it counts how often the searched string s is contained in the responses-t intthe numbers of threads used (default 1)
使用
使用方法同wrk类似,基本格式如下:
go-wrk [flags] url
常用的参数:
-H="User-Agent: go-wrk 0.1 bechmark\nContent-Type: text/html;": 由'\n'分隔的请求头-c=100: 使用的最大连接数-k=true: 是否禁用keep-alives-i=false: if TLS security checks are disabled-m="GET": HTTP请求方法-n=1000: 请求总数-t=1: 使用的线程数-b="" HTTP请求体-s="" 如果指定,它将计算响应中包含搜索到的字符串s的频率
执行测试:
# 8个线程,400个连接, 模拟10w次请求./go-wrk -c=400 -t=8 -n=100000 "http://localhost:8082/test/query?lat=39.915&lng=116.404"
输出结果:
==========================BENCHMARK==========================URL: http://localhost:8082/test/query?lat=39.915&lng=116.404Used Connections: 400Used Threads: 8Total number of calls: 100000===========================TIMINGS===========================Total time passed: 99.46sAvg time per request: 395.89msRequests per second: 1005.39Median time per request: 368.22ms99th percentile time: 775.90msSlowest time for request: 1479.00ms=============================DATA=============================Total response body sizes: 501200000Avg response body per request: 5012.00 ByteTransfer rate per second: 5038989.99 Byte/s (5.04 MByte/s)==========================RESPONSES==========================20X Responses: 100000 (100.00%)30X Responses: 0 (0.00%)40X Responses: 0 (0.00%)50X Responses: 0 (0.00%)Errors: 0 (0.00%)
结果解释:
关于接口/test/query,
- 每秒可以处理1005次请求(即 QPS);
- 每秒传输5.04MB数据(吞吐量);
- 响应码为20x开头的请求为 100%, 即没有发生业务之外的错误(比如 502);
- %99 的请求的平均处理时间为775.90ms。
- 其他的一些数据也可以比较直观的看到,比如测试总用时和最长的耗时等。
[
](https://blog.csdn.net/moxiaomomo/article/details/107851892)
