如何将测试api的Locust结果写入文件

Le *_*ang 6 python python-2.7 locust

我通过API调用测试,

locust -f locustfile.py --host=http://localhost --no-web  --hatch-rate=20 --clients=10000
Run Code Online (Sandbox Code Playgroud)

得到了一个结果

 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
 POST 8000/queries.json                                           137     0(0.00%)       5       2      23  |       5   11.00

--------------------------------------------------------------------------------------------------------------------------------------------
 Total                                                            708     0(0.00%)    
Run Code Online (Sandbox Code Playgroud)

我想把这个结果写成一个文件.谁能帮我这个?

下面是python中的代码

@task(1)
def test_topview(self):
    post_data_topview = """{ "category": "321",   "num": 20,   "genderCat" : ["23"] }"""
    with self.client.request(method="POST", url="http://192.168.1.107:8001/queries.json", headers= {"Content-Type" : "application/json"}, data = post_data_topview, catch_response = True ) as response:
        if not matched(response.content) :
            response.failure("No content")
Run Code Online (Sandbox Code Playgroud)

非常感谢你.

Mes*_*NEŞ 9

UPDATE

--csv版本添加了使用该选项保存csv文件.所以,你可以运行下面的命令来保存测试的结果foo_requests.csvfoo_distribution.csv

locust -f locustfile.py --host=http://localhost --no-web  --hatch-rate=20 --clients=10000 --only-summary --csv=foo
Run Code Online (Sandbox Code Playgroud)

对于低于0.8的版本

已经提交了保存Locust的结果,但它还没有合并到Locust.但是,您可以使用此提交手动更新它.它正在添加一个新参数--statsfile=result.log以保存结果.

那么完整的命令应该如下所示

locust -f locustfile.py --host=http://localhost --no-web  --hatch-rate=20 --clients=10000 --only-summary --statsfile=result.log
Run Code Online (Sandbox Code Playgroud)

您可以查看此帖子以更新Locust并检查日志结果.


Pau*_*aul 5

在 statsfile 选项生效之前的另一个选项是将 stderr 重定向到输出文件,这显然是记录统计信息的位置:

 locust -f locustfile.py --host=http://example.com --no-web --clients=20  --hatch-rate=20 --num-request=1000  --only-summary  > locust.log   2>&1
Run Code Online (Sandbox Code Playgroud)