【问题标题】:How to write Locust result of test api to file如何将测试api的蝗虫结果写入文件
【发布时间】:2016-01-10 11:00:14
【问题描述】:

我通过 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")

非常感谢。

【问题讨论】:

    标签: python python-2.7 locust


    【解决方案1】:

    添加到 2016 年的 Rays 答案。 在当前 locust 版本 1.0.3 中,您需要像这样添加事件侦听器

    events.request_success.add_listener(self.hook_request_success)
    

    而不是使用 +=

    我用它来汇总响应并将其写入数据库以供进一步分析

    【讨论】:

      【解决方案2】:

      在 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
      

      【讨论】:

        【解决方案3】:

        更新

        使用 --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并查看日志结果。

        【讨论】:

        • 亲爱的 Mesut,您使用的是哪个版本的 locust?我使用了“easy_install locustio pyzmq”并获得了 0.7.3 版本。似乎这个版本没有 --statsfile 选项。谢谢!
        • @LeKimTrang 我有相同的版本。提交没有合并到 Locust,所以你不能使用 --statsfile 但是你可以更新你的本地文件,如博客文章中所述。
        【解决方案4】:

        我曾尝试在文件中打印蝗虫统计数据但没有成功,但您可以使用 事件挂钩 来做到这一点:http://docs.locust.io/en/latest/api.html#available-hooks

        您可以向事件 request_success 和 request_failure 添加一个函数,因此每次请求成功或失败时,都会调用您的钩子函数,以便将请求数据放入列表或您想要的任何变量中。

        然后您可以轻松地将数据打印到例如 csv 文件中

        希望对你有帮助

        import locust.events
        from locust import HttpLocust, TaskSet
        
        
        class LocustUser(HttpLocust):
            task_set = TaskSet
            min_wait = 1000
            max_wait = 1000
        
            request_success_stats = [list()]
            request_fail_stats = [list()]
        
            def __init__(self):
                locust.events.request_success += self.hook_request_success
                locust.events.request_failure += self.hook_request_fail
                locust.events.quitting += self.hook_locust_quit
        
            def hook_request_success(self, request_type, name, response_time, response_length):
                self.request_success_stats.append([name, request_type, response_time])
        
            def hook_request_fail(self, request_type, name, response_time, exception):
                self.request_fail_stats.append([name, request_type, response_time, exception])
        
            def hook_locust_quit(self):
                self.save_success_stats()
        
            def save_success_stats(self):
                import csv
                with open('success_req_stats.csv', 'wb') as csv_file:
                    writer = csv.writer(csv_file)
                    for value in self.request_success_stats:
                        writer.writerow(value)
        

        【讨论】:

          【解决方案5】:

          locust -f loustfile.py -c 1 -r 1 -n 100 --host=http://localhost:4000 --no-web --only-summary > ../result/locustTest.log 2>&1

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-18
            • 1970-01-01
            • 2022-01-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多