This project demonstrates how to use Locust to perform load testing on a free-to-use site (http://test.k6.io
).
- macOS
- Homebrew (Package Manager)
- Python 3.7 or higher
-
Install Homebrew: If you don't have Homebrew installed, run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Install Python:
brew install python
-
Install Locust:
pip3 install locust
-
Create a New Directory for Your Project:
mkdir locust-k6-load-testing cd locust-k6-load-testing
-
Create a Locustfile:
touch locustfile.py
-
Define Your Load Test: Open
locustfile.py
in a text editor and add the following code:from locust import HttpUser, TaskSet, task, between class UserBehavior(TaskSet): @task def load_homepage(self): self.client.get("/") @task def load_contacts(self): self.client.get("/contacts.php") class WebsiteUser(HttpUser): tasks = [UserBehavior] wait_time = between(1, 5) host = "http://test.k6.io"
-
Navigate to Your Project Directory:
cd locust-k6-load-testing
-
Run Locust:
locust -f locustfile.py
-
Open the Locust Web Interface: Open your web browser and go to:
http://localhost:8089
-
Configure Your Test:
- Enter the number of total users to simulate (e.g., 100).
- Enter the spawn rate (users per second, e.g., 10).
-
Start the Test: Click the "Start swarming" button.
- The Locust web interface provides real-time statistics including the number of requests, response times, and failure rates.
- Use this data to analyze the performance and behavior of
http://test.k6.io
under load.
Here is the complete content of locustfile.py
for reference:
from locust import HttpUser, TaskSet, task, between
class UserBehavior(TaskSet):
@task
def load_homepage(self):
self.client.get("/")
@task
def load_contacts(self):
self.client.get("/contacts.php")
class WebsiteUser(HttpUser):
tasks = [UserBehavior]
wait_time = between(1, 5)
host = "http://test.k6.io"