2

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?

cdm
  • 719
  • 2
  • 10
  • 22
  • Possibly related: https://stackoverflow.com/questions/52596327/any-way-to-track-custom-statistics-in-locust – alexandrul Nov 09 '18 at 06:50

1 Answers1

5

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.

Wojteks
  • 856
  • 5
  • 8
  • Thanks (and sorry for the delay), this works perfectly for our cases. – cdm Jan 29 '19 at 15:17
  • Thank you. This was useful for me too. – Seema Nair Dec 07 '19 at 16:39
  • @Wojteks - this approach doesn't seem to work for locust 1.0 onwards - any suggestions on how I can get it to work? I'm struggling to have access to the 'host' attribute (everything else is working) – cdm Aug 28 '20 at 13:55
  • @cdm - please see the updated version (tested with locust 1.3.1) – Wojteks Oct 27 '20 at 20:24
  • `request_success` is now deprecated, use `request` instead. Additionally, `fire` needs to be called somewhere to trigger the hook. – vanbastelaer May 12 '21 at 01:16