Configs2026年4月12日·1 分钟阅读

Locust — Scalable Load Testing in Pure Python

Locust is an open-source load testing tool where you define user behavior in plain Python code. Distributed, scalable, and with a real-time web UI for monitoring. No DSL to learn — just write Python.

AI
AI Open Source · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

pip install locust
# locustfile.py
from locust import HttpUser, task, between

class TokrepoUser(HttpUser):
    wait_time = between(1, 3)
    host = "https://api.tokrepo.com"

    @task(3)
    def list_assets(self):
        self.client.get("/api/assets")

    @task(1)
    def view_asset(self):
        self.client.get("/api/assets/1")

    def on_start(self):
        # Login or setup
        pass
locust                       # Web UI at http://localhost:8089
locust --headless -u 100 -r 10 -t 5m --csv=results
locust --master              # Distributed: master
locust --worker --master-host=master-ip  # Workers
介绍

Locust is an open-source load testing tool where you define user behavior in plain Python code. No XML, no DSL — just Python classes and decorators. Features a real-time web UI, distributed testing across multiple machines, and easy CI/CD integration.

What Locust Does

  • Python test scripts — define user behavior as classes
  • Web UI — real-time charts, start/stop, user count control
  • Distributed — master + workers for massive scale
  • Headless mode — for CI/CD
  • CSV/JSON output — export results
  • Custom clients — test gRPC, WebSocket, MQTT, databases
  • Events — hook into test lifecycle
  • Task weighting@task(weight) for traffic distribution

Comparison

Tool Language UI Distributed
Locust Python Web UI Master/worker
k6 JavaScript CLI k6-operator
JMeter Java/GUI Desktop Servers
Vegeta Go CLI No
wrk C CLI No

常见问题 FAQ

Q: 能测多少并发? A: 单机几千用户(gevent 协程),分布式可以轻松到数万。不如 k6 单机效率但 Python 写脚本更灵活。

Q: 和 k6 比? A: Locust 写 Python(更灵活、可以直接调用任何 Python 库),k6 写 JS(Go 运行时更高效)。看团队语言偏好。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产