Chapter 4 · Task Weights and Wait Time
Chapter goals: Precisely control virtual user behavior distribution — use @task weights and tasks lists to control selection probability, and between/constant/constant_pacing three strategies to control execution cadence.
4.1 Two Ways to Set Task Weights
Decorator approach
from locust import HttpUser, task
class MallUser(HttpUser):
host = "https://httpbin.org"
@task(1)
def view_homepage(self):
self.client.get("/")
@task(3) # 3× more likely to be selected
def search_product(self):
self.client.get("/get?q=phone")
@task(2)
def add_to_cart(self):
self.client.post("/post", json={"item": "phone", "qty": 1})Total weight = 1+3+2 = 6. Selection probabilities: homepage 16.7%, search 50%, cart 33.3%.
tasks list approach
class WeightedUser(HttpUser):
host = "https://httpbin.org"
tasks = {view_homepage: 1, search_product: 3} # Dict {method: weight}Or use a list to express repetition (equivalent to weight):
tasks = [search_product] * 3 + [view_homepage]Both approaches are equivalent; decorators are more intuitive, dicts suit dynamic scenarios.
4.2 Three Wait Time Strategies
wait_time defines the pause between tasks, directly determining the RPS calculation model:
from locust import HttpUser, task, between, constant, constant_pacing
class WaitStrategies(HttpUser):
host = "https://httpbin.org"
# Strategy 1: random interval (most common)
wait_time = between(1, 5)| Strategy | Syntax | Behavior |
|---|---|---|
between(a, b) | between(1, 5) | Randomly wait a~b seconds after each task — closest to real human behavior |
constant(t) | constant(3) | Fixed wait of t seconds after each task |
constant_pacing(t) | constant_pacing(3) | Ensures one iteration starts every t seconds (including execution time) — precise RPS control |
The difference between constant and constant_pacing
The most commonly confused pair:
# constant(3): fixed 3s wait AFTER task execution completes
# If the task takes 2s, actual cycle = 2 + 3 = 5s
wait_time = constant(3)
# constant_pacing(3): guarantee a new iteration starts every 3s
# If the task takes 2s, only extra-wait 1s; if the task takes 4s, wait 0s and start immediately
wait_time = constant_pacing(3)When to use constant_pacing
Use it when you need a precise RPS target. For example, target RPS=100 with 50 concurrent users — each user's pacing should be 50÷100 = 0.5s. constant_pacing automatically compensates for task execution time variance.
4.3 SequentialTaskSet: Ordered Task Groups
By default, tasks are selected randomly. To execute a sequence of operations in order (e.g., a complete checkout flow), use SequentialTaskSet:
from locust import HttpUser, SequentialTaskSet, task
class CheckoutFlow(SequentialTaskSet):
"""Simulate a complete shopping flow: browse → add to cart → checkout"""
@task
def browse(self):
self.client.get("/products")
@task
def add_to_cart(self):
self.client.post("/cart", json={"item": "phone", "qty": 1})
@task
def pay(self):
resp = self.client.post("/checkout")
if resp.status_code == 200:
self.interrupt() # Flow complete, return to parent4.4 Chapter Summary
@task(N)sets relative selection weight; total weight determines probability distribution;between(a,b)for natural think time;constant(t)for fixed pauses;constant_pacing(t)for precise RPS control;constant_pacingcompensates for execution time variance — use when RPS target matters;SequentialTaskSetenforces ordered execution instead of random selection.
🧪 随堂测验
点击你认为正确的选项。答错时会展示正确答案与原因解析。
1. If @task weights are 1, 3, and 2, what is the selection probability of the second task?
2. What is the key difference between constant(3) and constant_pacing(3)?
3. When would you choose constant_pacing over between()?
4. What does SequentialTaskSet do differently from a regular HttpUser with @task methods?
🛠️ Hands-On Exercises
- Write a user with three tasks (weight 1, 2, 5); verify the selection distribution over 100 iterations matches expectations.
- Compare
constant(2)vsconstant_pacing(2)on a task that sometimes takes 1s and sometimes 3s; measure actual cycle times. - Build a
SequentialTaskSetfor a login→browse→checkout flow; interrupt on checkout success.
Proceed to Chapter 5: Web UI and Headless Mode.