Locust-MCP-Server 是一个用于运行 Locust 负载测试的模型上下文协议(MCP)服务器实现,它能让 Locust 负载测试功能与 AI 驱动开发环境实现无缝集成。
在使用 Locust-MCP-Server 前,请确保你已完成以下操作:
git clone https://github.com/qainsights/locust-mcp-server.git
uv pip install -r requirements.txt
.env 文件:LOCUST_HOST=http://localhost:8089 # 测试默认主机
LOCUST_USERS=3 # 默认用户数
LOCUST_SPAWN_RATE=1 # 用户默认spawn率
LOCUST_RUN_TIME=10s # 默认测试持续时间
完成上述准备工作后,你就可以开始进行 Locust 负载测试了:
hello.py):from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
wait_time = between(1, 5)
@task
def hello_world(self):
self.client.get("/hello")
self.client.get("/world")
@task(3)
def view_items(self):
for item_id in range(10):
self.client.get(f"/item?id={item_id}", name="/item")
time.sleep(1)
def on_start(self):
self.client.post("/login", json={"username":"foo", "password":"bar"})
{
"mcpServers": {
"locust": {
"command": "/Users/naveenkumar/.local/bin/uv",
"args": [
"--directory",
"/Users/naveenkumar/Gits/locust-mcp-server",
"run",
"locust_server.py"
]
}
}
}
运行 Locust 测试以分析 hello.py。Locust MCP 服务器将使用以下工具启动测试:run_locust:带有配置选项的测试运行器(无头模式、主机、持续时间、用户数和 spawn 率)git clone https://github.com/qainsights/locust-mcp-server.git
uv pip install -r requirements.txt
.env 文件:LOCUST_HOST=http://localhost:8089 # 测试默认主机
LOCUST_USERS=3 # 默认用户数
LOCUST_SPAWN_RATE=1 # 用户默认spawn率
LOCUST_RUN_TIME=10s # 默认测试持续时间
创建一个 Locust 测试脚本(例如 hello.py):
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
wait_time = between(1, 5)
@task
def hello_world(self):
self.client.get("/hello")
self.client.get("/world")
@task(3)
def view_items(self):
for item_id in range(10):
self.client.get(f"/item?id={item_id}", name="/item")
time.sleep(1)
def on_start(self):
self.client.post("/login", json={"username":"foo", "password":"bar"})
使用 run_locust API 启动测试:
run_locust(
test_file: str,
headless: bool = True,
host: str = "http://localhost:8089",
runtime: str = "10s",
users: int = 3,
spawn_rate: int = 1
)
run_locust(
test_file: str,
headless: bool = True,
host: str = "http://localhost:8089",
runtime: str = "10s",
users: int = 3,
spawn_rate: int = 1
)
参数说明:
test_file:Locust 测试脚本的路径。headless:无头模式(True)或带 UI(False)。host:目标主机进行负载测试。runtime:测试持续时间(例如,“30s”、“1m”、“5m”)。users:模拟的并发用户数。spawn_rate:用户的 spawn 率。欢迎贡献!请随意提交 Pull Request。
此项目在 MIT 许可证下,详细信息请参阅 LICENSE 文件。