Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Locust result of test api to file

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.

like image 523
Le Kim Trang Avatar asked Jan 10 '16 11:01

Le Kim Trang


People also ask

How do I print a response from a locust?

Note: First, you need to assign a response to the output of the POST request. Second, you should convert the response body to a JSON string. Finally, you should extract the desired value by using a JSON expression. The last line is going to print the result on the terminal.

Is Locust better than JMeter?

The main benefit of this framework is that it allows you to write complete performance routines in Python. Due to its event-based methodology and “test as code” capabilities, Locust is very scalable. Locust has a huge and constantly growing group of users that prefer it to JMeter because of these reasons.

What is HttpUser in Locust?

HttpUser is the most commonly used User . It adds a client attribute which is used to make HTTP requests. from locust import HttpUser, task, between class MyUser(HttpUser): wait_time = between(5, 15) @task(4) def index(self): self. get("/") @task(1) def about(self): self.


2 Answers

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.

like image 118
Mesut GUNES Avatar answered Dec 01 '22 22:12

Mesut GUNES


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
like image 21
Paul Avatar answered Dec 01 '22 22:12

Paul