Table of Contents
A “Locust load test” is a type of test used to measure the performance of a web application and evaluate how it responds under a specific load. Locust is an open-source tool that is used for such load tests and is based on Python. Locust allows the creation of scenarios that simulate specific user behaviors and generate a load on the web application.
The key components of Locust include:
- User Class: Locust uses Python classes to represent “users” performing the load test. These classes define specific scenarios, and by running these scenarios under a specified number of users, the tool applies load to the web application.
- Task: Within user classes, specific tasks are defined. For example, a task might represent an HTTP request, symbolizing a user making a request to the web application.
- Locust Web Interface: Locust provides a web interface for monitoring and analyzing the load test. This interface displays important metrics and statistics during the test.
The process of a load test may involve:
- Scenario Definition: Writing scenarios that simulate specific user behaviors. Each scenario may include one or more tasks.
- Determining User Count: Deciding how many users will participate simultaneously in the test. Each user follows the predefined scenario, sending requests to the web application.
- Starting the Load Test: Locust initiates the load test with the specified number of users and scenarios.
- Monitoring and Analyzing Results: Monitoring the results of the test through the Locust web interface or various metrics to evaluate the web application’s performance and identify potential issues.
This process helps understand how a web application performs under user traffic and provides crucial insights for development or infrastructure improvements.
1. Update Python and Pip:
Before installing Locust, update Python and pip:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3 python3-pip
2. Install Locust:
After updating Python and pip, install Locust using pip:
pip3 install locust
3. Create a Locust Scenario:
Create a Locust scenario. For example, create a file named locustfile.py
and add a sample scenario like the following:
from locust import HttpUser, task, between
class MyUser(HttpUser):
wait_time = between(1, 3)
@task
def my_task(self):
self.client.get("/path/to/your/endpoint")
4. Start Locust:
Start Locust using the file containing your scenario:
locust -f locustfile.py
Replace locustfile.py
with the name of the file where you created your scenario.
5. View the Web Interface:
Locust, by default, provides a web interface at http://localhost:8089
. Open this address in your browser to view and control your test through the Locust web interface.
6. Start the Test:
Adjust the number of users and other parameters from the web interface and click “Start Swarming” to initiate the test.
These steps will help you install Locust on Ubuntu and start a basic load test. For more information and customization options, refer to the Locust documentation: Locust Documentation
Locust Commands:
- Number of Users and Hatch Rate:
- Set the number of users and hatch rate from the web interface or command line:
locust -f locustfile.py --users 100 --spawn-rate 10
- Set the number of users and hatch rate from the web interface or command line:
- Headless Mode:
- Run Locust in headless mode (without the web interface):
locust -f locustfile.py --headless -u 100 -r 10
- Run Locust in headless mode (without the web interface):
- Specify Host:
- Define the host to test (replace “http://example.com” with your host):
locust -f locustfile.py --host http://example.com
- Define the host to test (replace “http://example.com” with your host):
Locust Scenarios:
- Task Execution:
- Define tasks in the scenario and execute them:
@task def my_task(self): self.client.get("/path/to/your/endpoint")
- Define tasks in the scenario and execute them:
- Task Weighting:
- Assign weights to tasks to control their relative frequency:
@task(1) def task1(self): # ... @task(2) def task2(self): # ...
- Assign weights to tasks to control their relative frequency:
Locust Metrics:
- Request Success/Failure Metrics:
- View request success and failure metrics in the web interface.
Locust Stopping:
- Stop Test:
- Press “Stop” in the web interface or send a SIGINT signal (Ctrl+C in the terminal) to stop the test.