If you need help:
Option 1: Run on Locust Cloud (no setup needed)
Option 2: Run locally
> pip install locust
...
> locust -V
locust 2.42.1 from /Users/.../locust (Python 3.12.2)Option 3: Clone this repo and use uv to run in an isolated environment:
> git clone https://github.com/locustcloud/workshop.git
...
> cd workshop
> uv run locust -V
locust 2.42.1 from /Users/.../.venv/lib/python3.12/site-packages/locust (Python 3.12.7)Open locustfile.py (locally or in the hosted test editor)
from locust import HttpUser, task
class MyUser(HttpUser):
@task
def t(self):
self.client.get("/")
self.client.get("/product/42")Now, run your first test against a mock web shop hosted by us:
> locust --host https://mock-test-target.eu-north-1.locust.cloud
[2024-11-10 15:59:26,604] lars-mbp/INFO/locust.main: Starting Locust 2.42.1
[2024-11-10 15:59:26,608] lars-mbp/INFO/locust.main: Starting web interface at http://0.0.0.0:8089Open the web ui and try running it! Just use a single user for now to not spam the service.
import time
from locust import HttpUser, task
class MyUser(HttpUser):
@task
def t(self):
self.client.get("/")
for i in range(5):
time.sleep(1)
self.client.get(f"/product/{i}")- It is probably fairly obvious what this does, but go ahead and try it out anyway!
from locust import HttpUser, run_single_user, task
class MyUser(HttpUser):
@task
def t(self):
self.client.get("/")
for i in range(5):
response = self.client.get(f"/product/{i}")
self.client.get("/this_does_not_exist")
if __name__ == "__main__":
run_single_user(MyUser)If you're in VSCode, just launch the "Run current file" configuration. Also check out the Locust VSCode Extension. You'll need to set "gevent": true in your launch.json if you didn't clone this repo, otherwise vscode will give an error talking about It seems that the gevent monkey-patching is being used. ...
Then you can:
- Try setting a breakpoint & examine the response object in real time
from locust import HttpUser, run_single_user, task
class MyUser(HttpUser):
@task
def t(self):
with self.client.post(
"/authenticate", json={"username": "foo", "password": "bar"}, catch_response=True
) as response:
if err := response.json().get("error"):
response.failure(err)
if __name__ == "__main__":
run_single_user(MyUser)- Try running this with a changed password and examine the results.
Locust Cloud tests can be executed in the same way as local load tests, by just adding the --cloud flag.
Register for Locust Cloud if you haven't already. When you are done, log in to the service:
locust --cloud --loginAnd then run your first test.
locust --cloud --host https://mock-test-target.eu-north-1.locust.cloud --users 100Run a more advanced test against the mock server:
locust --cloud --host https://mock-test-target.eu-north-1.locust.cloud -f locustfile_advanced.py --users 100 --spawn-rate 5- Analyze the results
- Peak throughput that the mock can handle?
- Which requests are slow?
- Are there any periodic variations?
- ...