Locust

2.43.4 · active · verified Mon Apr 06

Locust is an open-source, developer-friendly load testing framework that allows you to define user behavior in plain Python code. It's designed for testing web applications, APIs, and other systems, supporting hundreds of thousands of concurrent users through its event-based architecture. Locust offers a real-time web-based UI for monitoring and analysis, and is actively maintained with frequent updates (approximately every 62 days) [1, 2, 11, 19, 31]. It currently requires Python 3.10 or newer [31].

Warnings

Install

Imports

Quickstart

This quickstart defines a `QuickstartUser` that inherits from `HttpUser` for HTTP load testing. It includes `wait_time` to simulate user think time and two tasks (`hello_world` and `view_items`) decorated with `@task` to define user actions. The `view_items` task demonstrates using the `name` parameter for grouping dynamic URLs. `on_start` and `on_stop` methods are shown for per-user setup and teardown, such as login/logout. Save this code as `locustfile.py` and run `locust` from the command line to start the web UI, or use the `--headless` option for command-line execution [2, 10, 20].

import os
from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(1, 2)  # Users wait between 1 and 2 seconds after each task
    host = os.environ.get('LOCUST_TARGET_HOST', 'http://localhost:8089') # Default host for testing

    def on_start(self):
        """ on_start is called when a Locust user starts running """
        # Example: Simulating a login if required by the target system
        # self.client.post("/login", json={"username":"test_user", "password":"test_password"})
        print(f"Starting user on host: {self.host}")

    @task
    def hello_world(self):
        self.client.get("/hello")
        self.client.get("/world")

    @task(3)
    def view_items(self):
        # Simulate viewing items with dynamic IDs, using 'name' to aggregate stats
        for item_id in range(10):
            self.client.get(f"/item?id={item_id}", name="/item")

    def on_stop(self):
        """ on_stop is called when a Locust user stops running """
        # Example: Simulating a logout
        # self.client.post("/logout")
        print("Stopping user.")

# To run this, save as `locustfile.py` and execute `locust` from the terminal.
# Then open your browser to http://localhost:8089 to use the web UI.
# Alternatively, run headless: `locust -f locustfile.py --headless --users 10 --spawn-rate 5 -H http://your-target-host.com`

view raw JSON →