Locust: Users, Tasks & wait_time
Locust is an open-source load-testing tool where test scenarios are written as plain Python code -- not a proprietary DSL or record-and-replay script -- letting developers use familiar language constructs and existing libraries directly in their test logic.
Defining a User
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(1, 5) # think time between tasks
@task(3)
def browse_home(self):
self.client.get("/")
@task(1)
def checkout(self):
self.client.post("/checkout", json={"item": "widget"})Task Weights
@task(3) is chosen 3x as often as a default-weight task -- letting a scenario model realistic action frequency, like browsing happening far more often than checking out.
wait_time: Realistic Think Time
wait_time simulates the pause a real user takes between actions -- producing more realistic load patterns than tasks executing back-to-back with zero delay.
Handling Authentication
class AuthenticatedUser(HttpUser):
def on_start(self):
# runs once per simulated user before its tasks begin
response = self.client.post("/login", json={"user": "test"})
self.token = response.json()["token"]
@task
def view_dashboard(self):
self.client.get("/dashboard", headers={"Authorization": self.token})Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free