-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocust_server.py
55 lines (47 loc) · 1.84 KB
/
locust_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from typing import Any
import subprocess
from pathlib import Path
import os
from mcp.server.fastmcp import FastMCP
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
mcp = FastMCP("locust")
@mcp.tool(name="run_locust", description="Run Locust with the given configuration.")
async def run_locust(test_file: str, host: str = os.getenv("LOCUST_HOST", "http://localhost:8089"),
users: int = int(os.getenv("LOCUST_USERS", "100")),
spawn_rate: int = int(os.getenv("LOCUST_SPAWN_RATE", "10")),
runtime: str = os.getenv("LOCUST_RUNTIME", "30s"),
headless: bool = os.getenv("LOCUST_HEADLESS", "true").lower() == "true") -> Any:
"""
Run Locust with the given configuration.
Args:
test_file: Path to the Locust test file
host: Target host URL to load test
users: Number of concurrent users to simulate
spawn_rate: Rate at which users are spawned per second
runtime: Duration of the test (e.g., "30s", "1m", "5m")
headless: Whether to run in headless mode (no web UI)
"""
locust_bin = os.getenv("LOCUST_BIN", "locust")
cmd = [locust_bin, "-f", test_file, "--host", host]
if headless:
cmd.extend(["--headless"])
cmd.extend(["-u", str(users)])
cmd.extend(["-r", str(spawn_rate)])
cmd.extend(["-t", runtime])
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return {
"status": "success",
"output": result.stdout,
"error": result.stderr
}
except subprocess.CalledProcessError as e:
return {
"status": "error",
"output": e.stdout,
"error": e.stderr
}
if __name__ == "__main__":
mcp.run(transport="stdio")