go-wrk 是一个用Go语言实现的轻量级的http基准测试工具,类似于wrk(还有ab、siege),本文将简单介绍一下如何使用go-wrk实现接口的性能(压力)测试。

安装 go-wrk

  1. go get github.com/adeven/go-wrk

go-wrk 选项说明

  1. #./go-wrk --help
  2. Usage of ./go-wrk:
  3. -CA string
  4. A PEM eoncoded CA's certificate file. (default "someCertCAFile")
  5. -H string
  6. the http headers sent separated by '\n' (default "User-Agent: go-wrk 0.1 benchmark\nContent-Type: text/html;")
  7. -b string
  8. the http request body
  9. -c int
  10. the max numbers of connections used (default 100)
  11. -cert string
  12. A PEM eoncoded certificate file. (default "someCertFile")
  13. -d string
  14. dist mode
  15. -f string
  16. json config file
  17. -i TLS checks are disabled
  18. -k if keep-alives are disabled (default true)
  19. -key string
  20. A PEM encoded private key file. (default "someKeyFile")
  21. -m string
  22. the http request method (default "GET")
  23. -n int
  24. the total number of calls processed (default 1000)
  25. -p string
  26. the http request body data file
  27. -r in the case of having stream or file in the response,
  28. it reads all response body to calculate the response size
  29. -s string
  30. if specified, it counts how often the searched string s is contained in the responses
  31. -t int
  32. the numbers of threads used (default 1)

使用

使用方法同wrk类似,基本格式如下:

  1. go-wrk [flags] url

常用的参数:

  1. -H="User-Agent: go-wrk 0.1 bechmark\nContent-Type: text/html;": '\n'分隔的请求头
  2. -c=100: 使用的最大连接数
  3. -k=true: 是否禁用keep-alives
  4. -i=false: if TLS security checks are disabled
  5. -m="GET": HTTP请求方法
  6. -n=1000: 请求总数
  7. -t=1: 使用的线程数
  8. -b="" HTTP请求体
  9. -s="" 如果指定,它将计算响应中包含搜索到的字符串s的频率

执行测试:

  1. # 8个线程,400个连接, 模拟10w次请求
  2. ./go-wrk -c=400 -t=8 -n=100000 "http://localhost:8082/test/query?lat=39.915&lng=116.404"

输出结果:

  1. ==========================BENCHMARK==========================
  2. URL: http://localhost:8082/test/query?lat=39.915&lng=116.404
  3. Used Connections: 400
  4. Used Threads: 8
  5. Total number of calls: 100000
  6. ===========================TIMINGS===========================
  7. Total time passed: 99.46s
  8. Avg time per request: 395.89ms
  9. Requests per second: 1005.39
  10. Median time per request: 368.22ms
  11. 99th percentile time: 775.90ms
  12. Slowest time for request: 1479.00ms
  13. =============================DATA=============================
  14. Total response body sizes: 501200000
  15. Avg response body per request: 5012.00 Byte
  16. Transfer rate per second: 5038989.99 Byte/s (5.04 MByte/s)
  17. ==========================RESPONSES==========================
  18. 20X Responses: 100000 (100.00%)
  19. 30X Responses: 0 (0.00%)
  20. 40X Responses: 0 (0.00%)
  21. 50X Responses: 0 (0.00%)
  22. Errors: 0 (0.00%)

结果解释:
关于接口/test/query,

  1. 每秒可以处理1005次请求(即 QPS);
  2. 每秒传输5.04MB数据(吞吐量);
  3. 响应码为20x开头的请求为 100%, 即没有发生业务之外的错误(比如 502);
  4. %99 的请求的平均处理时间为775.90ms。
  5. 其他的一些数据也可以比较直观的看到,比如测试总用时和最长的耗时等。

[

](https://blog.csdn.net/moxiaomomo/article/details/107851892)