Skip to content

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:

TypeGoalTypical Question
Load TestingVerify the system meets targets under expected concurrency"Can the page still load in 2s with 500 concurrent users?"
Stress TestingFind the system's breaking point"How many more users before errors start appearing?"
Soak TestingDetect 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:

python
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.
text
┌─────────────┐     ┌──────────┐     ┌─────────────┐
│  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 ​

FeatureLocustJMeterk6
Script LanguagePythonXML / GroovyJavaScript
Concurrency Modelgevent coroutinesThreadsGo goroutines
DistributedBuilt-in master-workerRequires JMeter ServerCloud-native
Learning CurveLow (know Python)Medium-highLow
Programmability⭐⭐⭐ Full Python⭐⭐ Groovy scripts⭐⭐ JS subset
GUIWeb UI (browser)Swing desktopNone

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 ​

bash
python -m venv .venv && source .venv/bin/activate
pip install locust

Verify the install:

bash
locust --version
# locust 2.46.x (Python 3.x.x)

Create your first script locustfile.py:

python
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:

bash
$ locust
[2026-01-01 10:00:00] INFO/locust.main: Starting web interface at http://0.0.0.0:8089

Open 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 ​

  1. Install Locust and run locust --version; record the version number.
  2. Write the locustfile.py from this chapter (swap the URL for a site you can reach); run 5 users for 30 seconds from the Web UI.
  3. Read the Increasing the request rate page and list three methods to raise single-machine request rate.

Proceed to Chapter 2.