8

I invoke the test through API,

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

and got a 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")

Thank you very much.

rafaelc
  • 57,686
  • 15
  • 58
  • 82
Le Kim Trang
  • 369
  • 2
  • 5
  • 17

5 Answers5

10

UPDATE

Saving csv file with the option --csv is added with this release . So you can run the following command to save result of the test as foo_requests.csv and foo_distribution.csv

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

FOR the version below 0.8

There has been a commit for saving the result of Locust but it is not merged to Locust yet. However you can update it manually with this commit. It is adding a new parameters as --statsfile=result.log to save the result.

Then the complete command should look like this

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

You can check this post for updating Locust and checking the result of the log.

Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
  • Dear Mesut, which version of locust are you using? I used 'easy_install locustio pyzmq' and got the version 0.7.3. Seem that this version does not have --statsfile option. Thanks! – Le Kim Trang Jan 12 '16 at 08:34
  • @LeKimTrang I have same version. The commit didnt merged to Locust so you can not use `--statsfile` however you can update your local files as in mentioned in the blog post. – Mesut GUNES Jan 12 '16 at 10:38
5

Another option until the statsfile option is live would be to redirect stderr to an output file, which apparently is where the stats are logged to:

 locust -f locustfile.py --host=http://example.com --no-web --clients=20  --hatch-rate=20 --num-request=1000  --only-summary  > locust.log   2>&1
Paul
  • 7,155
  • 8
  • 41
  • 40
4

I have tried to print locust stats in a file without success, but you can do it in using event hook : http://docs.locust.io/en/latest/api.html#available-hooks.

You can add a function to the event request_success and request_failure, so each time a request success or fail your hook function will be called in order to get request data into a list or any variable you want.

Then you can easily print the data into a csv file for instance

Hope it would help you

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)
Ray
  • 41
  • 2
1

To add to Rays answer from 2016. In the current locust version 1.0.3 you'll need to add the event listeners like this

events.request_success.add_listener(self.hook_request_success)

instead of using +=

I used this to aggregate and write the responses into a database for further analysis

PichlerD
  • 11
  • 2
0

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

megan5
  • 11
  • 1