Locust + Boomer In Docker

原文由花菜发表于TesterHome社区,点击原文链接可与作者直接交流。

背景

在压测时,使用locust + boomer,需要配置python和go环境,稍微有点麻烦。每次接口压测,都需要手动编写go脚本。因此想做对boomer做一个封装,可以快速的完成这些需求,需要支持单接口 + 动态读取csv,curl命令

启动Locust-master

# run locust master
docker run --name locust-master -it -d -p 8089:8089 -p 5557:5557 rikasai/locust-master:latest

Boomer

调试模式启动boomer

$ 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/[email protected]/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/post',请求url

--method=POST,请求方法

--content-type="application/json"

--raw-data='{"ids": [123,234]}'

--verbose 1,打印响应结果

locust worker模式启动

get请求

# 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

post请求

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"}'

直接使用curl命令

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" }'"

动态读取csv

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"
}

--json-value-type说明

--raw-data='{"a": "$a", "b": "$b"}'

csv内容是

1,2,3
3,4,5
4,5,6
455,56,633
  • --json-value-type=intArray,请求结果是

    {
        "a": [
            3
        ],
        "b": [
            4
        ]
    }
  • --json-value-type=string,请求结果是

    {
        "a": "3",
        "b": "4"
    }
  • --json-value-type=interface,请求结果是

    csv内容是

    3,4,5
    aa,bb,cc

    先尝试转换成int类型,转换成成功就是用int值,不成功就是用原始值

    3,4,5转换成

    {
        "a": 3,
        "b": 4
    }

    aa,bb,cc转换成int不成功,使用原始值

    {
        "a": "aa",
        "b": "bb"
    }
  • 不支持深层json,这里的$b是不支持的。

    {
        "a":{
                "b": "$b"
        }
    }

目前动态替换并不完美,只能是一层json和body。后续再优化一波~

原文由花菜发表于TesterHome社区,点击原文链接可与作者直接交流。

你可能感兴趣的:(docker压力测试)