在压测时,使用 locust + boomer,需要配置 python 和 go 环境,稍微有点麻烦。
每次接口压测,都需要手动编写 go 脚本。
因此想做对 boomer 做一个封装,可以快速的完成这些需求,需要支持单接口 + 动态读取 csv,curl 命令
# run locust master
docker run --name locust-master -it -d -p 8089:8089 -p 5557:5557 rikasai/locust-master:latest
$ docker run rikasai/boomer:latest --run-tasks worker -master-host=10.0.20.16 --url='http://httpbin.org/post' --method=POST --content-type="application/json" --raw-data='{"ids": [123,234]}'
--verbose 1
2022/05/17 13:30:44 /usr/src/app/boomer_fasthttp.go:295: Fasthttp is running with these args:
method: POST
url: http://httpbin.org/post
timeout: 10s
post-file:
raw-data: {"ids": [123,234]}
replace-str-index:
json-value-type:
content-type: application/json
disable-keepalive: false
remove-json-string-backslash: true
verbose: true
2022/05/17 13:30:44 /go/pkg/mod/github.com/myzhan/boomer@v1.6.0/boomer.go:214: Running worker
2022/05/17 13:30:44 /usr/src/app/boomer_fasthttp.go:211: resp: {
"args": {},
"data": "{\"ids\": [123,234]}",
"files": {},
"form": {},
"headers": {
"Content-Length": "18",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "fasthttp",
"X-Amzn-Trace-Id": "Root=1-6283a384-7d87389417a84df642d87a91"
},
"json": {
"ids": [
123,
234
]
},
"url": "http://httpbin.org/post"
}
参数说明
docker run rikasai/boomer:latest, 启动一个容器
--run-tasks worker,调试模式,固定写法
-master-host=10.0.20.16,指定 locust-master 的 ip
--url='http://httpbin.org/posturl',请求
--method=POST,请求方法
--content-type="application/json"
--raw-data='{"ids": [123,234]}'
--verbose 1,打印响应结果
# 10.0.20.16 is master ip addr
# 5557 is default port
docker run rikasai/boomer:latest -master-host=10.0.20.16 -master-port=5557 --url='http://httpbin.org/get?a=123' --method=GET
docker run rikasai/boomer:latest -master-host=10.0.20.16 --url='http://httpbin.org/post' --method=POST --content-type="application/json" --raw-data='{"ids": [123,234]}'
docker run rikasai/boomer:latest --run-tasks worker -master-host=10.0.20.16 --url='http://httpbin.org/post' --method=POST --content-type="application/json" --raw-data='{"ids": [123,234]}' --json-headers='{"User-Agent": "python-requests/2.22.0"}'
docker run rikasai/boomer:latest --run-tasks worker -master-host=10.0.20.16 --curl="curl --location --request POST 'http://httpbin.org/post' \ --header 'Content-Type: application/json' \ --data-raw '{ "string": "hello,boomer" }'"
docker run -d --name boomer -v /root/data/data.csv:/app/data.csv rikasai/boomer:latest -master-host=10.0.20.16 --url=http://httpbin.org/post --method=POST --content-type="application/json" --raw-data='{"a": "$a", "b": "$b"}' --replace-str-index='{"$a":0,"$b":1,"$c":2}' --json-value-type "int" --verbose 1
参数说明
-d,后台模式启动容器
--name boomer,指定容器的名字叫 boomer,可以随意起,不重名就行
-v /root/data/data.csv:/app/data.csv,把宿主机的 csv 文件挂载到容器里,/root/data/data.csv 是宿主机文件路径,/app/data.csv 是容器里固定路径
--raw-data='{"a": "$a", "b": "$b"}',$a 是取变量 a,$b 是取变量 b
--replace-str-index='{"$a":0,"$b":1,"$c":2}',把 csv 中每一行着的第 0 值赋给 $a,第 1 个值赋给 $b,第 2 个赋给 $c
--json-value-type "int" 指--raw-data 中变量的值类型是 int 类型
/root/data/data.csv 内容是:
1,2,3
3,4,5
4,5,6
455,56,633
请求 log
2022/05/17 14:37:56 /usr/src/app/boomer_fasthttp.go:211: resp: {
"args": {},
"data": "{\"a\":1,\"b\":2}",
"files": {},
"form": {},
"headers": {
"Content-Length": "13",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "fasthttp",
"X-Amzn-Trace-Id": "Root=1-6283b344-2ec1715202778cff233df950"
},
"json": {
"a": 1,
"b": 2
},
"url": "http://httpbin.org/post"
}
2022/05/17 14:37:56 /usr/src/app/boomer_fasthttp.go:211: resp: {
"args": {},
"data": "{\"a\":4,\"b\":5}",
"files": {},
"form": {},
"headers": {
"Content-Length": "13",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "fasthttp",
"X-Amzn-Trace-Id": "Root=1-6283b344-155b24075acdcb214e963d51"
},
"json": {
"a": 4,
"b": 5
},
"url": "http://httpbin.org/post"
}
以--raw-data='{"a": "$a", "b": "$b"}'
csv 内容是
1,2,3
3,4,5
4,5,6
455,56,633
{
"a": [
3
],
"b": [
4
]
}
{
"a": "3",
"b": "4"
}
csv 内容是
3,4,5
aa,bb,cc
先尝试转换成 int 类型,转换成成功就是用 int 值,不成功就是用原始值
3,4,5 转换成
{
"a": 3,
"b": 4
}
aa,bb,cc 转换成 int 不成功,使用原始值
{
"a": "aa",
"b": "bb"
}
{
"a":{
"b": "$b"
}
}
目前动态替换并不完美,只能是一层 json 和 body。后续再优化一波~