127 lines
5.1 KiB
Python
127 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Run the IfixKart customer swarm.
|
|
|
|
Example:
|
|
cd Backend
|
|
./venv/bin/python -m swarm.agents.runner \
|
|
--agents-file sim-artifacts/agents-smoke.json \
|
|
--count 20 --concurrency 20 --think-time-scale 0.02
|
|
|
|
For 10k:
|
|
./venv/bin/python -m swarm.agents.runner \
|
|
--agents-file sim-artifacts/agents-<run>.json \
|
|
--count 10000 --concurrency 250 --ramp-seconds 120 --think-time-scale 0.01
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import asyncio
|
|
import json
|
|
import sys
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
BACKEND_DIR = Path(__file__).resolve().parents[2]
|
|
if str(BACKEND_DIR) not in sys.path:
|
|
sys.path.insert(0, str(BACKEND_DIR))
|
|
|
|
from swarm.agents.orchestrator import run_fleet, write_report # noqa: E402
|
|
from swarm.agents.personas import Persona, build_personas # noqa: E402
|
|
|
|
DEFAULT_AGENTS = BACKEND_DIR / "sim-artifacts" / "agents-smoke.json"
|
|
DEFAULT_OUT = BACKEND_DIR / "sim-artifacts"
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description="Run the IfixKart simulated customer swarm")
|
|
parser.add_argument("--base-url", default="http://127.0.0.1:8001")
|
|
parser.add_argument("--agents-file", default=str(DEFAULT_AGENTS))
|
|
parser.add_argument("--count", type=int, default=20)
|
|
parser.add_argument("--concurrency", type=int, default=50)
|
|
parser.add_argument("--think-time-scale", type=float, default=0.02)
|
|
parser.add_argument("--ramp-seconds", type=float, default=5.0)
|
|
parser.add_argument("--seed", type=int, default=42)
|
|
parser.add_argument("--request-timeout", type=float, default=45.0)
|
|
parser.add_argument("--llm-model", default="qwen3.5:4b", help="Ollama model for persona bank; empty disables")
|
|
parser.add_argument("--ollama-url", default="http://127.0.0.1:11434")
|
|
parser.add_argument("--persona-count", type=int, default=24)
|
|
parser.add_argument("--persona-file", default="", help="Reuse an existing persona bank JSON")
|
|
parser.add_argument("--output", default="")
|
|
args = parser.parse_args()
|
|
|
|
agents_path = Path(args.agents_file)
|
|
if not agents_path.exists():
|
|
raise SystemExit(f"Agents file not found: {agents_path}; run seed_customers.py first")
|
|
data = json.loads(agents_path.read_text())
|
|
accounts = data.get("agents") or []
|
|
run_id = data.get("run_id") or datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S")
|
|
|
|
if args.persona_file:
|
|
persona_data = json.loads(Path(args.persona_file).read_text())
|
|
base_personas = [Persona.from_dict(p) for p in persona_data.get("personas", persona_data)]
|
|
print(f"[runner] loaded {len(base_personas)} personas from {args.persona_file}")
|
|
else:
|
|
base_personas = build_personas(
|
|
args.persona_count,
|
|
model=args.llm_model or None,
|
|
ollama_url=args.ollama_url,
|
|
seed=args.seed,
|
|
)
|
|
persona_out = DEFAULT_OUT / f"personas-{run_id}.json"
|
|
persona_out.parent.mkdir(parents=True, exist_ok=True)
|
|
persona_out.write_text(
|
|
json.dumps(
|
|
{
|
|
"run_id": run_id,
|
|
"model": args.llm_model or "deterministic",
|
|
"personas": [p.to_dict() for p in base_personas],
|
|
},
|
|
indent=2,
|
|
)
|
|
)
|
|
print(f"[runner] persona bank saved to {persona_out}")
|
|
|
|
print(
|
|
f"[runner] base_url={args.base_url} agents={min(args.count, len(accounts))} "
|
|
f"concurrency={args.concurrency} think_scale={args.think_time_scale}"
|
|
)
|
|
report = asyncio.run(
|
|
run_fleet(
|
|
base_url=args.base_url,
|
|
accounts=accounts,
|
|
base_personas=base_personas,
|
|
count=args.count,
|
|
concurrency=args.concurrency,
|
|
think_time_scale=args.think_time_scale,
|
|
ramp_seconds=args.ramp_seconds,
|
|
seed=args.seed,
|
|
request_timeout=args.request_timeout,
|
|
progress_every=10 if args.count <= 100 else max(50, args.count // 20),
|
|
)
|
|
)
|
|
report["run_id"] = run_id
|
|
report["base_url"] = args.base_url
|
|
report["persona_model"] = args.llm_model or "deterministic"
|
|
report["concurrency"] = args.concurrency
|
|
output = Path(args.output) if args.output else DEFAULT_OUT / f"report-{run_id}-{int(report['agents_requested'])}.json"
|
|
write_report(report, output)
|
|
|
|
print("\n=== SWARM SUMMARY ===")
|
|
print(f"agents completed : {report['agents_completed']}/{report['agents_requested']}")
|
|
print(f"duration : {report['duration_seconds']}s")
|
|
print(f"outcomes : {report['outcomes']}")
|
|
print(f"orders confirmed : {report['orders_confirmed']}")
|
|
print(f"revenue : ₹{report['revenue']:,}")
|
|
print(f"AOV : ₹{report['aov']:,}")
|
|
print(f"HTTP requests : {report['total_http_requests']}")
|
|
print(f"HTTP statuses : {report['http_statuses']}")
|
|
if report["top_errors"]:
|
|
print("top errors :")
|
|
for err in report["top_errors"][:8]:
|
|
print(f" {err['count']}x {err['error']}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|