Load testing using LOCUST - GeeksforGeeks

Load testing using LOCUST

Last Updated : 21 Sep, 2026

Load Testing Using Locust is a performance testing technique that uses Python to simulate multiple concurrent users interacting with a web application or API. It helps evaluate application behavior under expected workloads.

  • Measures key performance metrics such as response time, requests per second, and failure rate.
  • Defines user behavior using Python-based test scripts.
  • Supports distributed testing to generate load from multiple machines.

Procedure for Load Testing Using Locust

The procedure involves creating a test script, configuring virtual users, executing the load test, and analyzing the resulting performance metrics.

Step 1: Install Locust

Install Locust using pip:

pip install locust

Verify the installation:

locust --version

The locust command should be available in the terminal after successful installation. To view the available command-line options:

locust --help

Step 2: Create the Locust Test File

Create a Python file named locustfile.py and define the behavior of the simulated users.

Python
from locust import HttpUser, task, between

class WebsiteUser(HttpUser):
    wait_time = between(1, 3)

    @task
    def home_page(self):
        self.client.get("/")

Here, HttpUser represents a simulated HTTP user, @task defines an action performed by the user, and wait_time specifies the waiting period between task executions.

Step 3: Start Locust

Open a terminal in the directory containing locustfile.py and run:

locust

By default, Locust looks for a file named locustfile.py in the current directory. After Locust starts, open the following address in a browser:

http://localhost:8089

Step 4: Configure the Load Test

In the Locust web interface, specify the required test parameters:

  • Number of users to simulate
  • User spawn rate
  • Host or base URL of the application

Example:

Users: 100
Spawn rate: 10 users/second
Host: https://example.com

Step 5: Execute the Load Test

Start the test from the Locust web interface. Locust gradually creates users according to the configured spawn rate, and each user executes the tasks defined in locustfile.py.

Step 6: Monitor Performance Metrics

During the test, Locust displays metrics such as:

  • Requests per second
  • Response time
  • Number of requests
  • Number of failures
  • Number of active users

These metrics help evaluate application performance under the generated load.

Step 7: Analyze the Results

After the test is completed, analyze the collected metrics to identify slow responses, failed requests, and potential performance bottlenecks.

Step 8: Run Locust in Headless Mode

Locust can also run from the command line without opening the web interface:

locust -f locustfile.py --headless -u 100 -r 10 -t 1m --host https://example.com

Here:

  • -f specifies the Locust test file.
  • -u specifies the number of users.
  • -r specifies the user spawn rate.
  • -t specifies the test duration.
  • --host specifies the target application's base URL.
  • --headless runs Locust without the web interface.

Testing an Authenticated API Using Locust

Locust can test APIs that require authentication by obtaining an access token during user initialization and using the token for subsequent authenticated requests.

Python
from locust import HttpUser, task, between


class APIUser(HttpUser):
    wait_time = between(1, 3)

    def on_start(self):
        response = self.client.post(
            "/login",
            json={
                "username": "admin",
                "password": "your_password"
            }
        )

        response.raise_for_status()
        self.token = response.json()["access"]

        self.headers = {
            "Authorization": f"Bearer {self.token}"
        }

    @task
    def get_users(self):
        self.client.get(
            "/users",
            headers=self.headers
        )

Explanation

  • on_start() runs once when each simulated user starts and obtains an access token from the login API.
  • self.token stores the access token returned by the authentication endpoint.
  • self.headers contains the Bearer token used for authenticated requests.
  • @task defines the API operation performed by each simulated user.
  • wait_time controls the delay between task executions.

Advantages of Locust

Locust provides a Python-based approach to load testing, making it suitable for testing web applications and APIs with customizable user behavior.

  • Allows test scenarios to be written and customized using Python.
  • Supports user tasks, wait times, and task execution patterns to model user behavior.
  • Supports distributed load testing across multiple worker processes or machines.
  • Provides a web UI for configuring tests and monitoring performance during execution.
  • Supports HTTP-based API load testing through its built-in HTTP client.

Limitations of Locust

Although Locust is flexible and lightweight, it has some limitations that should be considered when selecting it for performance testing.

  • Requires Python knowledge to create and customize test scenarios.
  • Large-scale tests can consume substantial CPU and memory resources on load-generation machines.
  • Primarily provides built-in support for HTTP-based testing, while other protocols may require additional libraries or custom implementations.
  • Built-in reporting provides standard performance metrics, while advanced analysis and visualization may require external tools.
  • Distributed testing requires additional configuration of workers and the load-generation environment.
Comment