cdm
cdm

Reputation: 799

Access Locust Host attribute - Locust 1.0.0+

I had previously asked and solved the problem of dumping stats using an older version of locust, but the setup and teardown methods were removed in locust 1.0.0, and now I'm unable to get the host (base URL).

I'm looking to print out some information about requests after they've run. Following the docs at https://docs.locust.io/en/stable/extending-locust.html, I have an request_success listener inside my sequential task set - some rough sample code below:

class SearchSequentialTest(SequentialTaskSet):

    @task
    def search(self):
        path = '/search/tomatoes'
        headers = {"Content-Type": "application/json",
        unique_identifier = uuid.uuid4()
        data = {
            "name": f"Performance-{unique_identifier}",
        }

        with self.client.post(
                path,
                data=json.dumps(data),
                headers=headers,
                catch_response=True,
        ) as response:
            json_response = json.loads(response.text)
            self.items = json_response['result']['payload'][0]['uuid']

            print(json_response)


    @events.request_success.add_listener
    def my_success_handler(request_type, name, response_time, response_length, **kw):
       print(f"Successfully made a request to: {self.host}/{name}")

But I cannot access the self.host - and if I remove it I only get a relative url.

How do I access the base_url inside a TaskSet's event hooks?

Upvotes: 1

Views: 1966

Answers (1)

Cyberwiz
Cyberwiz

Reputation: 11426

How do I access the base_url inside a TaskSet's event hooks?

You can do it by accessing the class variable directly in your request handler:

print(f"Successfully made a request to: {YourUser.host}/{name}")

Or you can use absolute URLs in your test (task) like this:

    with self.client.post(
        self.user.host + path,
        ...

Then you'll get the full url to your request listener.

Upvotes: 2

Related Questions