binithb
binithb

Reputation: 2000

Locust : How to invoke the test through an API

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

Upvotes: 10

Views: 7982

Answers (4)

Mesut GUNES
Mesut GUNES

Reputation: 7401

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'

Upvotes: 2

Deepak Garud
Deepak Garud

Reputation: 1129

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)

Upvotes: 4

timfeirg
timfeirg

Reputation: 1512

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.

Upvotes: 7

heyman
heyman

Reputation: 4954

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

Upvotes: 6

Related Questions