0

I'm doing some load testing using Locust. For what it's worth, I am also using Graphite and Grafana for analytics of the results, but I can produce this issue without loading or using either one in my code.

At it's most simple, the issue can be reproduced with the following very simple locust file:

from locust import HttpLocust, TaskSet, task, between
import locust.events


class Tasks(TaskSet):
    @task
    def make_request(self):
        self.client.get('/')
        print('doing thing')


class Locust(HttpLocust):
    wait_time = between(1, 3)
    task_set = Tasks

    def __init__(self):
        super(Locust, self).__init__()
        locust.events.request_success += self.hook_request_success

    def hook_request_success(self, request_type, name,
                             response_time, response_length):
        # This is where I would make a call to send the request's metadata to Graphite
        print("sending thing")

Called like so:

locust -H <host> -t 10s -c 1000 -r 10 --no-web -f test.py

As you can see, the spec is basic. I have a one-task plan, and I want to perform a single request sending the result of each request to Graphite. However, in the stdout of all of my runs, I get nearly sixty (60) times more instances of "sending thing" than "doing thing" when I would assume they would be exactly one to one! I've confirmed the function is being called with the same parameters and represents the exact same request, just called numerous times for every "actual" request locust makes. I don't want this at all, I only want to send the request once.

Why is this? Is there something I'm doing wrong?

Answorth
  • 147
  • 1
  • 2
  • 10

1 Answers1

1

You're adding the event listener every time you spawn/instantiate a locust.

Make hook_request_success a global function and add it from there as well, changing it to something like this:

locust.events.request_success += hook_request_success

That way it will only be added once, which is what you want.

Cyberwiz
  • 11,027
  • 3
  • 20
  • 40