近来有需求要对HTTP接口进行压测,于是去了解了一下JMeter,发现虽然功能强大,但本身依赖JAVA,并且依赖图形化界面,不够轻量化,所以想着自己写一个基于命令行的轻量级压测工具,于是就有了httptester。
httptester是采用GO语言来编写的,尽管目前仍是早期版本,但是基本功能已经没有问题了。
httptester是一个二进制可执行文件,无需安装。
下载最新版本:https://github.com/rocketk/httptester/releases
国内镜像地址:https://gitee.com/rocketk/httptester
找到自己平台对应的版本下载至本地,将其所在目录放入系统环境变量中。
获取帮助信息
31httptester -h
2httptester run -h
3httptester serve -h
最简单的压测命令(以压测百度首页为例)
11httptester run -u 'https://www.baidu.com/'
设定并发量、循环次数、超时时间
21httptester run --loop 10 --concurrency 100 --timeout 500ms -u 'https://www.baidu.com/'
2#httptester run --loop 10 --concurrency 100 --timeout 2s -u 'https://www.baidu.com/'
191100% [==============================]
2-- Configuration --
3Concurrency: 100 Loop: 10 Timeout: 2000 ms KeepAlive: true TimeUnit: ms Method: GET URL: https://www.baidu.com/
4Headers: []
5Body:
6
7-- Conclusion --
8total count: 1000
9success count: 981
10failed count: 0
11error count: 19
12nature duration: 2280 ms
13total cost: 195688 ms
14max: 1539 ms
15min: 0 ms
16median: 110 ms
17mean: 195 ms
18standard deviation: 248.309631
19throughput: 430 requests/second
字段 | 含义 |
---|---|
total count | 总共的http请求数量,等于loop*concurrency |
success count | 总共成功的http请求数量 |
failed count | 失败的请求数量,不同于 error count ,只有被【断言】校验不通过的才算失败 |
error count | 错误数量,一般是超时或http接口不可用 |
nature duration | 自然耗时(区别于下面的总体耗时) |
total cost | 总体耗时,每个请求的耗时加总起来的总耗时,在并发情况下会大于自然耗时 |
max | 最大单次请求耗时 |
min | 最小单次请求耗时 |
median | 单次请求耗时中位数 |
mean | 平均每次请求耗时 |
standard deviation | 每次请求耗时标准差 |
throughput | 吞吐量,数值等于 success_count / nature_duration |
为了更好地测试各种Assertion表达式,你可以通过以下命令启动一个Restful风格的API服务:
x
1httptester serve
此示例服务是一个典型的Restful风格的API服务,包含以下操作(以curl命令为例):
11curl http://localhost:1234/users
31curl -X POST \
2 -d '{"name":"NewUser","age":18,"stature":175,"weight":60,"available":true}' \
3 'http://localhost:1234/users'
31curl -X PUT \
2 -d '{"name":"NewUser","age":18,"stature":175,"weight":60,"available":true}' \
3 'http://localhost:1234/users/{id}'
注意将{id}
改为实际的用户id
11curl -X DELETE 'http://localhost:1234/users/{id}
在默认情况下,httptester并不会做断言检测,也就是说只要http请求得到了响应,不论其返回的响应是什么,不论响应码是什么,都会按照成功来计算。但很多情况下,你需要判断其结果是否正确。
当前版本的httptester支持3种断言,即 响应码断言 / JSON断言 / 正则表达式断言。
以上一节Restful-API服务接口中的“列出所有用户”为例,curl
格式如下:
11curl -i http://localhost:1234/users
返回结果:
x
1HTTP/1.1 200 OK
2Content-Type: application/json; charset=utf-8
3Date: Sat, 13 Mar 2021 13:28:48 GMT
4Content-Length: 459
5
6[
7 {
8 "age" : 18,
9 "id" : "732e930c-59ce-4087-b509-6288b5d2d6c5",
10 "stature" : 180,
11 "weight" : 62.5,
12 "name" : "Jack",
13 "available" : true
14 },
15 {
16 "age" : 25,
17 "id" : "30160f4c-a4ee-420b-ba61-fb0f78a3e312",
18 "stature" : 175,
19 "weight" : 60.5,
20 "name" : "Mary",
21 "available" : true
22 },
23 {
24 "age" : 32,
25 "id" : "04c7aca2-c27b-4ff1-8756-1bdf57ab7993",
26 "stature" : 185,
27 "weight" : 65,
28 "name" : "Benjamin",
29 "available" : true
30 },
31 {
32 "age" : 15,
33 "id" : "51784d48-4abd-4f01-83df-4cd43869027c",
34 "stature" : 160,
35 "weight" : 50.799999999999997,
36 "name" : "Lee",
37 "available" : false
38 }
39]
接下来我们来看在httptester
中如何来写断言。
使用--assert-status-codes
来设定响应码断言,下面的例子表示,只有当http响应码为200
或201
时才算请求成功
31httptester run -u 'http://localhost:1234/users' \
2 -c 100 -l 100 \
3 --assert-status-codes '200 201'
此次我们使用“新增一个用户”为例,它的返回值如下:
61HTTP/1.1 200 OK
2Content-Type: application/json; charset=utf-8
3Date: Fri, 12 Mar 2021 07:01:50 GMT
4Content-Length: 116
5
6{"id":"ddb6b9fe-f0af-40ef-8b44-90e5b150b3ac","name":"NewUser","age":18,"stature":175,"weight":60.5,"available":true}
假设我们认为返回值中的name
值要等于NewUser
,那么我可以使用--assert-json-expression
来达到这一目的。注意双等号两侧的空格是必须的。
41httptester run --method POST -u 'http://localhost:1234/users' \
2 -b '{"name":"NewUser","age":18,"stature":175,"weight":60,"available":true}' \
3 -c 100 -l 100 \
4 --assert-json-expression '$.name == NewUser'
添加header
设定method
添加body
添加timeout
101httptester run --method 'POST' -u 'http://localhost:1234/users' \
2 -H 'Content-Type:application/json' \
3 -H 'accept:application/json' \
4 -b '{"name":"NewUser","age":18,"stature":175,"weight":60,"available":true}' \
5 --timeout 2s \
6 --loop 100 \
7 --concurrency 100 \
8 --assert-status-codes '200 201' \
9 --assert-json-expression '$.name == NewUser'
10 -e
-e
表示如果出现失败或报错,将错误信息打印出来
如果对这个小工具感兴趣,欢迎给我点赞。 如果有任何问题或建议,也欢迎给在此项目中给我提issue或者pull request。