Search code examples
performance-testinglocust

Locust Request Statistics


I'm considering using locust for some of my performance tests. I'm more familiar with Python and find locust much easier to read than JMeter JMX.

One thing I'm accustomed to doing with JMeter is generating my own average, 90pct, 95pct, and 99pct reports from multiple runs. To do this, I've written a script that parses the JMeter logs which contain information on every request (response time, payload size, etc.), and then combine all the runs into a single data set and generate average and percentiles.

I can't seem to find an option to get this level of detailed logging in locust. I've tried --logfile= but the file contains nothing about individual requests. I've tried --csv= and the output just contains summary information - which can't be used when trying to determine the percentiles in a combination of runs.

Is there a way to get detailed log information on each request?


Solution

  • I'm not sure if this is the easiest way but you could use locust event hooks mechanism.

    Let's start in the command line python built-in http server:

    python -m http.server
    

    and create file example.py with this content:

    #!/usr/bin/env python
    from locust import HttpUser, TaskSet, task, events
    
    stat_file = open('stats.csv', 'w')
    
    class UserBehavior(TaskSet):
        """ Defines user behaviour in traffic simulation """
    
        @task()
        def index(self):
            self.client.get("/")
    
    
    class WebsiteUser(HttpUser):
        """ Defines user that will be used in traffic simulation """
        tasks = {UserBehavior:2}
        min_wait = 3000
        max_wait = 5000
    
    
    # hook that is fired each time the request ends up with success
    @events.request_success.add_listener
    def hook_request_success(request_type, name, response_time, response_length, **kw):
        stat_file.write(request_type + ";" + name + ";" + str(response_time) + ";" + str(response_length) + "\n")
    
    
    @events.quitting.add_listener
    def hook_quitting(environment, **kw):
        stat_file.close()
    

    Now in the same folder where example.py lives run from the command line:

    locust -f example.py --headless -u 10 -r 1 --host=http://localhost:8000
    

    If you stop it after a while you will find in the stats.csv file details of each successful request.