Chapter 1 · Performance Testing and Locust Overview
Chapter goals: Understand the differences between load testing, stress testing, and soak testing; learn about Locust's event-driven architecture; install and verify the Locust environment.
1.1 Three Types of Performance Testing
Performance testing is not a single test — it's a family. The three most commonly confused in engineering practice are:
| Type | Goal | Typical Question |
|---|---|---|
| Load Testing | Verify the system meets targets under expected concurrency | "Can the page still load in 2s with 500 concurrent users?" |
| Stress Testing | Find the system's breaking point | "How many more users before errors start appearing?" |
| Soak Testing | Detect resource leaks under long-running conditions | "Will memory fill up after 24h of continuous operation?" |
The relationship between the three
Load testing answers "is it fast enough?"; stress testing answers "how much can it handle?"; soak testing answers "can it last?". A complete performance evaluation usually requires all three.
1.2 What Is Locust
Locust is an open-source load testing tool written in Python. Its core appeal:
from locust import HttpUser, task
class HelloWorldUser(HttpUser):
@task
def hello_world(self):
self.client.get("/hello")A load test script is just a regular Python file — meaning you can use loops, functions, and third-party libraries to construct arbitrarily complex user behavior, without learning XML config or a proprietary DSL.
1.3 Architecture: Event-Driven + gevent
Locust's concurrency model rests on two pillars:
- gevent: a coroutine-based concurrency library where each virtual user runs in its own greenlet (micro-thread). A single process can easily simulate thousands of users, far below the memory overhead of thread or process-based approaches.
- Event-driven: request completion, task execution, and test start/stop all trigger event hooks — you can inject custom statistics, alerts, and logging logic onto these hooks.
┌─────────────┐ ┌──────────┐ ┌─────────────┐
│ Master Node │────▶│ Worker×N │────▶│ Target System │
│ (Web UI/Stats)│◀────│ (greenlets) │◀────│ │
└─────────────┘ └──────────┘ └─────────────┘When a single machine isn't enough, Locust supports a master-worker distributed mode: the Master node handles scheduling and stats aggregation, while Worker nodes each generate traffic.
1.4 Locust vs JMeter vs k6
| Feature | Locust | JMeter | k6 |
|---|---|---|---|
| Script Language | Python | XML / Groovy | JavaScript |
| Concurrency Model | gevent coroutines | Threads | Go goroutines |
| Distributed | Built-in master-worker | Requires JMeter Server | Cloud-native |
| Learning Curve | Low (know Python) | Medium-high | Low |
| Programmability | ⭐⭐⭐ Full Python | ⭐⭐ Groovy scripts | ⭐⭐ JS subset |
| GUI | Web UI (browser) | Swing desktop | None |
Recommendation: pick Locust if your team already uses Python; JMeter if you need record-and-playback; k6 if you pursue maximum single-machine throughput.
1.5 Installation and Verification
python -m venv .venv && source .venv/bin/activate
pip install locustVerify the install:
locust --version
# locust 2.46.x (Python 3.x.x)Create your first script locustfile.py:
from locust import HttpUser, task
class HelloWorldUser(HttpUser):
@task
def hello_world(self):
self.client.get("/hello")
self.client.get("/world")Run from the terminal:
$ locust
[2026-01-01 10:00:00] INFO/locust.main: Starting web interface at http://0.0.0.0:8089Open your browser to http://localhost:8089, enter the target host address, and start the load test.
1.6 Chapter Summary
- Performance testing splits into three types: load (fast enough?), stress (how much?), soak (lasts long?):
- Locust writes load test scripts as plain Python; gevent coroutines enable high concurrency;
- When a single machine isn't enough, use the built-in master-worker distributed mode;
- Compared to JMeter/k6, Locust's advantage is "full Python programmability + low learning curve."
🧪 随堂测验
点击你认为正确的选项。答错时会展示正确答案与原因解析。
1. Verifying "memory usage grows continuously after 48h of continuous operation" belongs to which type of performance test?
2. What technology does Locust's concurrency model build on?
3. What port does Locust's Web UI listen on by default?
4. Regarding the Locust vs JMeter comparison, which statement is correct?
🛠️ Hands-On Exercises
- Install Locust and run
locust --version; record the version number. - Write the
locustfile.pyfrom this chapter (swap the URL for a site you can reach); run 5 users for 30 seconds from the Web UI. - Read the Increasing the request rate page and list three methods to raise single-machine request rate.
Proceed to Chapter 2.