Locust: Support for tests that use multiple hosts

Created on 8 May 2014  ·  6Comments  ·  Source: locustio/locust

Our system runs two different hostnames (one for web, one for api requests), and it's not clear to me how to write a locust that would handle this. The --host option being passed to locust only allows passing one hostname (and assumes that the host resolves to the IP in question -- in a testing setup, the Host: header in the request to the web server might not resolve, as in testing during a build).

Suggestions?

Thanks,
Jeff

Most helpful comment

You _can_ actually make requests to different hosts, simply by specifying a full URL, however the hostname will not appear in the statistics entry, so requests to different hosts, but with the same path, will appear as a single stats entry:

self.client.get("/some/path")
self.client.get("http://other.host/some/path")

If you want them to be separate stats entries, you could override the entry name using the name parameter:

self.client.get("http://other.host/some/path", name="http://other.host/some/path")

If you could live with that, you could then specify a second host using an environment variable. Perhaps something like this:

import os

from locust import HttpLocust
from locust.clients import HttpSession

class MultipleHostsLocust(HttpLocust):
    abstract = True

    def __init__(self, *args, **kwargs):
        super(MultipleHostsLocust, self).__init__(*args, **kwargs)
        self.api_client = HttpSession(base_url=os.environ["API_HOST"])

All 6 comments

You _can_ actually make requests to different hosts, simply by specifying a full URL, however the hostname will not appear in the statistics entry, so requests to different hosts, but with the same path, will appear as a single stats entry:

self.client.get("/some/path")
self.client.get("http://other.host/some/path")

If you want them to be separate stats entries, you could override the entry name using the name parameter:

self.client.get("http://other.host/some/path", name="http://other.host/some/path")

If you could live with that, you could then specify a second host using an environment variable. Perhaps something like this:

import os

from locust import HttpLocust
from locust.clients import HttpSession

class MultipleHostsLocust(HttpLocust):
    abstract = True

    def __init__(self, *args, **kwargs):
        super(MultipleHostsLocust, self).__init__(*args, **kwargs)
        self.api_client = HttpSession(base_url=os.environ["API_HOST"])

Hi Jonatan,

Thanks! I was able to get this working this way, and it makes sense. Locust looks like a really great tool for running these sorts of tests, and looking forward to getting it used as part of our production system!

best,
Jeff

Glad to hear that :)!

You could also expand on my suggestion above, to make the api_client automatically set the name parameter, by subclassing the HttpSession class.

This also solved my problem. It would however be quite nice if I could leave "host" as blank totally. For cases where I write tests that always pass a full URL.

thank you guys, super useful

it's working on me, thanks

Was this page helpful?
0 / 5 - 0 ratings