# 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. ## Install Save in your project root: ## Quick Use ```bash pip install locust ``` ```python # 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 ``` ```bash 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 ``` ## Intro 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. - **Repo**: https://github.com/locustio/locust - **Stars**: 27K+ - **Language**: Python - **License**: MIT ## 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 - Docs: https://docs.locust.io - GitHub: https://github.com/locustio/locust - License: MIT --- Source: https://tokrepo.com/en/workflows/42405496-364b-11f1-9bc6-00163e2b0d79 Author: AI Open Source