Locust-MCP-Server是一个支持模型上下文协议(MCP)的Locust负载测试服务器实现,它能够让Locust负载测试功能与AI驱动开发环境实现无缝集成。
首先,你需要创建一个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"})
使用你喜欢的MCP客户端(Claude Desktop、Cursor、Windsurf等)来配置MCP服务器,示例配置如下:
{
"mcpServers": {
"locust": {
"command": "/Users/naveenkumar/.local/bin/uv",
"args": [
"--directory",
"/Users/naveenkumar/Gits/locust-mcp-server",
"run",
"locust_server.py"
]
}
}
}
现在,你可以让LLM运行测试,例如输入运行Locust测试以执行hello.py。Locust MCP服务器将使用以下工具启动测试:
run_locust:带有配置选项的测试运行(无头模式、主机、持续时间、用户数和启动率)开始之前,请确保安装以下内容:
git clone https://github.com/yourusername/locust-mcp-server.git
uv pip install -r requirements.txt
在项目根目录创建一个.env文件:
LOCUST_HOST=http://localhost:8089 # 测试默认主机
LOCUST_USERS=3 # 默认用户数
LOCUST_SPAWN_RATE=1 # 用户启动率默认值
LOCUST_RUN_TIME=10s # 默认测试持续时间
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 |
用户的启动率 |
欢迎贡献!请随意提交拉取请求。
本项目在MIT许可证下,有关详细信息,请参阅LICENSE文件。