Search code examples
pythonpython-2.7locust

How to invoke Locust tests programmatically?


Tried this with Locust up on my localhost (127.0.0.1:8089), but it gives 400 bad request error:

import requests

response = requests.post('http://127.0.0.1:8089/swarm', params={"locust_count":10, "hatch_rate":5})
print response.text

Response:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>

I had verified that http://127.0.0.1:8089/ is indeed up by browsing it from the browser. The code for locustfile.py is available at this post.


Solution

  • If you want to start locust from your code then you have 3 options :

    1. (Not the preferred one but working )You can run Locust using the CLI (--no-web, -c , -r ) https://docs.locust.io/en/latest/running-locust-without-web-ui.html

    2. Implementing the locust loading logic on your own : https://github.com/locustio/locust/issues/222 (Using the code provided in the message thread)

    3. Using Invokust: https://github.com/FutureSharks/invokust

    The first option, even though not the most "pythonic" is the easiest to do. I am still trying my luck with the 2nd option.