协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
locust【开发与工程】Implements load testing strategies using Locust to simulate user behavior and assess application performance, ensuring that systems are prepared for real-world traffic conditions effectively while providing actionable insights for optimization, scalability, and performance validation.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: locust
description: Implements load testing strategies using Locust to simulate user behavior and assess application performance, ensuring that systems are prepared for real-world traffic conditions effectively while providing actionable insights for optimization, scalability, and performance validation. license: MIT compatibility: opencode metadata: version: "1.1.0" domain: performance triggers: locust, load testing, performance evaluation, user behavior simulation, scalability testing, automated testing integration, performance metrics archetypes: [implementation, testing] anti_triggers: [superficial assessment, incomplete user behavior modeling, ineffective load testing, lack of metrics] response_profile: verbosity: medium directive_strength: high abstraction_level: operational
role: implementation scope: implementation output-format: code
Executes performance and load tests using Locust to simulate realistic user behavior, measure system throughput, and identify bottlenecks under controlled traffic conditions with staged ramp-up and custom load shapes.
HttpUser class per distinct user role or workflow; keep tasks focused on realistic user actionswait_time — without it, Locust runs tasks back-to-back, creating unrealistic load--host CLI flag or environment variable; never hardcode URLs@task decorator with weights to reflect real-world traffic distribution across endpointsLoadTestShape class rather than abusing @events.initA complete locustfile.py with task weights, wait times, and on_start/on_stop hooks:
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
"""Simulates a typical website visitor with realistic think time."""
wait_time = between(1, 5) # 1-5 seconds between tasks
def on_start(self):
"""Login once per user session."""
self.client.post("/login", json={
"username": "test_user",
"password": "test_pass",
})
@task(3)
def view_homepage(self):
"""Weight 3: most common user action."""
self.client.get("/")
@task(2)
def view_product(self):
"""Weight 2: browse product catalog."""
self.client.get("/products/latest")
@task(1)
def checkout(self):
"""Weight 1: least common, most business-critical."""
with self.client.post(
"/cart/add", json={"product_id": 42},
catch_response=True,
) as resp:
if resp.status_code == 503:
resp.failure("Checkout service unavailable under load")
class ApiUser(HttpUser):
"""Simulates API client traffic with shorter think time."""
wait_time = between(0.1, 0.5)
@task(4)
def list_orders(self):
self.client.get("/api/orders?limit=20")
@task(1)
def create_order(self):
self.client.post("/api/orders", json={"item": "SKU-100", "qty": 1})
A locustfile.py using LoadTestShape to ramp traffic in defined phases:
from locust import HttpUser, task, constant
from locust import LoadTestShape
class ApiUser(HttpUser):
"""Simulates API client traffic with constant think time."""
wait_time = constant(0.5)
@task
def get_health(self):
self.client.get("/health")
@task(3)
def query_orders(self):
self.client.get("/api/orders?limit=20")
@task(1)
def create_order(self):
self.client.post("/api/orders", json={"item": "SKU-100", "qty": 1})
class StagesShape(LoadTestShape):
"""
Ramp up users in stages:
1. Warm-up: 10 users/s for 2 minutes
2. Ramp: 10->50 users/s over 3 minutes
3. Peak: 100 users/s for 5 minutes
4. Cooldown: 100->0 users/s over 1 minute
"""
stages = [
{"duration": 120, "users": 10, "spawn_rate": 10},
{"duration": 300, "users": 50, "spawn_rate": 13},
{"duration": 600, "users": 100, "spawn_rate": 17},
{"duration": 660, "users": 0, "spawn_rate": -50},
]
def tick(self):
elapsed = self.get_run_time()
for stage in self.stages:
if elapsed < stage["duration"]:
return (stage["users"], stage["spawn_rate"])
return None
wait_time between tasks to simulate realistic user think timeAuthoritative documentation links for this domain. The model follows markdown links at load time to resolve external references and inline content.