Understanding Locust Wait Times with Complete Examples
In this chapter, we’ll cover,
What wait times are in Locust.
Built-in wait time options.
Creating custom wait times.
A full example with instructions to run the test.
What Are Wait Times in Locust?
In real-world scenarios, users don’t interact with applications continuously. After performing an action (e.g., submitting a form), they often pause before the next action. This pause is called a wait time in Locust, and it plays a crucial role in mimicking real-life user behavior.
Locust provides several ways to define these wait times within your test scenarios.
FastAPI App Overview
Here’s the FastAPI app that we’ll test,
from fastapi import FastAPI
# Create a FastAPI app instance
app = FastAPI()
# Define a route with a GET method
@app.get("/")
def read_root():
return {"message": "Welcome to FastAPI!"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
1. Constant Wait Time Example
Here, we’ll simulate constant pauses between user requests
2. Between wait time Example
Simulating random pauses between requests.
3. Custom Wait Time Example
Using a custom wait time function to introduce more complex user behavior
Full Test Example
Combining all the above elements, here’s a complete Locust test for your FastAPI app.
Running Locust for FastAPI
Run Your FastAPI App Save the FastAPI app code in a file (e.g.,
main.py) and start the server. By default, the app will run onhttp://127.0.0.1:8000.
Run Locust Save the Locust file as
locustfile.pyand start Locust.
3. Configure Locust Open http://localhost:8089 in your browser and enter:
Host:
http://127.0.0.1:8000Number of users and spawn rate based on your testing requirements.
4. Run in Headless Mode (Optional) Use the following command to run Locust in headless mode
-u 50: Simulate 50 users.
-r 10: Spawn 10 users per second.
Last updated