How to write locust test result api to file

I call the test through the API,

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

and got the result

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

I would like to write this result to a file. Can anyone help me with this?

Below is the code in 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")

Many thanks.

+4
source share
4 answers

Another option, until the statsfile parameter is available, will redirect stderr to the output file, which, apparently, is where the statistics are recorded:

 locust -f locustfile.py --host=http://example.com --no-web --clients=20  --hatch-rate=20 --num-request=1000  --only-summary  > locust.log   2>&1
+2
source

UPDATE

Saving the csv file with the option is --csvadded using release . Thus, you can run the following command to save the test result as foo_requests.csvandfoo_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

Locust .

+5

, event hook: http://docs.locust.io/en/latest/api.html#available-hooks.

request_success request_failure, , hook, , .

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)
+2

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

0
source

Source: https://habr.com/ru/post/1623646/


All Articles