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

标签 python python-2.7 locust

我通过 API 调用测试,

locust -f locustfile.py --host=http://localhost --no-web  --hatch-rate=20 --clients=10000

得到结果

 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%)    

我想把这个结果写入一个文件。谁能帮我解决这个问题?

下面是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")

非常感谢。

最佳答案

更新

使用此选项保存 csv 文件 --csv 添加了此 release .因此,您可以运行以下命令将测试结果保存为 foo_requests.csvfoo_distribution.csv

locust -f locustfile.py --host=http://localhost --no-web  --hatch-rate=20 --clients=10000 --only-summary --csv=foo

适用于0.8以下版本

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

那么完整的命令应该是这样的

locust -f locustfile.py --host=http://localhost --no-web  --hatch-rate=20 --clients=10000 --only-summary --statsfile=result.log

可以查看this post用于更新 Locust 并检查日志结果。

关于python - 如何将测试 api 的 Locust 结果写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34704617/

相关文章:

Python/BS - 从存储在目录中的 html 文件获取 URL,保存到 CSV

Bash - 如何检索 'or' 语句中第一个命令的退出状态

python - 为什么无法在浏览器中访问正在运行的 Docker 容器?

javascript - 如何用自定义变量替换 DJANGO 中 JAVASCRIPT 脚本生成的 Google map key

用于增量内循环的python

python - 如何检查字典中是否存在键值对?

python-2.7 - Raspberry Pi 2 的 Python 语音识别

locust - 使用带有 locust 的多 CPU 平台

python - 当我使用 json.loads 时,你是什么?

Python 3.x - 创建数据框并指定列名称