Go每日一库之102:go-wrk(HTTP基准测试)

2023-09-30 08:32:33 浏览数 (1)

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

安装 go-wrk

代码语言:javascript复制
go get github.com/adeven/go-wrk

go-wrk 选项说明

代码语言:javascript复制
#./go-wrk --help
Usage of ./go-wrk:
  -CA string
    	A PEM eoncoded CA's certificate file. (default "someCertCAFile")
  -H string
    	the http headers sent separated by 'n' (default "User-Agent: go-wrk 0.1 benchmarknContent-Type: text/html;")
  -b string
    	the http request body
  -c int
    	the max numbers of connections used (default 100)
  -cert string
    	A PEM eoncoded certificate file. (default "someCertFile")
  -d string
    	dist mode
  -f string
    	json config file
  -i	TLS checks are disabled
  -k	if keep-alives are disabled (default true)
  -key string
    	A PEM encoded private key file. (default "someKeyFile")
  -m string
    	the http request method (default "GET")
  -n int
    	the total number of calls processed (default 1000)
  -p string
    	the 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 string
    	if specified, it counts how often the searched string s is contained in the responses
  -t int
    	the numbers of threads used (default 1)

使用

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

代码语言:javascript复制
go-wrk [flags] url

常用的参数:

代码语言:javascript复制
-H="User-Agent: go-wrk 0.1 bechmarknContent-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的频率

执行测试:

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

输出结果:

代码语言:javascript复制
==========================BENCHMARK==========================
URL:				http://localhost:8082/test/query?lat=39.915&lng=116.404

Used Connections:		400
Used Threads:			8
Total number of calls:		100000

===========================TIMINGS===========================
Total time passed:		99.46s
Avg time per request:		395.89ms
Requests per second:		1005.39
Median time per request:	368.22ms
99th percentile time:		775.90ms
Slowest time for request:	1479.00ms

=============================DATA=============================
Total response body sizes:		501200000
Avg response body per request:		5012.00 Byte
Transfer 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,

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

[

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

0 人点赞