10

I would like to invoke Locust load tests through an API to be able to start tests from a CI tool.

I dont see much documentation about such a scenario, there is no "Runner" or a similar class in the locust API documentation.

I checked the "locust" command which becomes available, after installation in Windows . It is a .exe so not sure what it does and how it actually starts the test

So , the specific question is, is there an interface to start the test from another Python program

binithb
  • 1,910
  • 4
  • 23
  • 44

4 Answers4

7

just do what you do in the locust web UI and do it in python.

if you monitor the network in the locust UI, you'll notice that invoking a swarm is just a GET request towards 127.0.0.1:8089/swarm with two arguments, locust_count and hatch_rate.

to answer your question, here's the api you asked for and example:

import requests

payload = {
'locust_count': 12,
'hatch_rate': 22,
}

res = requests.get('http://127.0.0.1:8089/swarm', params=payload)
print(res.json())

didn't test it, let me know if it doesn't work.

timfeirg
  • 1,426
  • 18
  • 37
6

Currently, there is no documented API for controlling locust, except for the command line interface. The CLI can be used to start running load tests, though currently one can't run locust distributed without the web UI.

You could also use the web UI as an API, and just make the HTTP requests, that the browser sends to the web UI, yourself from your program.

The locust.exe file that is created (by python's setuptools) in Windows, is just a small wrapper that will run main() in locust/main.py

heyman
  • 4,845
  • 3
  • 26
  • 19
  • Nope, it hasn't changed. The API documentation you refer to is the API that you use when writing your test scripts. The question was about invoking/starting Locust from another python script. – heyman Mar 12 '15 at 10:27
4

I like the answer from timfeirg above. The idea that you already have python installed when locust is being used and we just need to run a python file is good. Only the code from timfeirg didnt work, so modified it a bit:

import requests

lc = 10
hr = 10

response = requests.post("http://127.0.0.1:8089/swarm", {"locust_count":lc, "hatch_rate":hr})
print(response.content)
Deepak Garud
  • 977
  • 10
  • 18
2

try curl request on shell to simulate your browser:

curl 'http://localhost:8089/swarm' -H 'Cookie: l10n-locale=en_GB; l10n-submitter=; l10n-license-agreed=false; JSESSIONID.7094a8b9=16g03c8dktw4g1x8ag027nbvl5; screenResolution=1280x800' -H 'Origin: http://localhost:8089' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8,tr;q=0.6' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: */*' -H 'Referer: http://localhost:8089/' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' -H 'X-FirePHP-Version: 0.0.6' --data 'locust_count=5&hatch_rate=1' --compressed
{"message": "Swarming started", "success": true}ubuntu@ip-172-31-16-111:~$

to set the user and hatch rate edit data:

--data 'locust_count=5&hatch_rate=1'
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
  • Dear Mesut, can you show me the way to view the result in this case, how can I see min, max, average response time and req/s. Thank you very much. – Le Kim Trang Jan 15 '16 at 10:20
  • @LeKimTrang with this way you ca start running locust then you can check the result from `http://localhost:8089 `. is this what you want to do? – Mesut GUNES Jan 15 '16 at 13:23