Spaces:
Sleeping
Sleeping
Download tests/load_test.py from Qurrah/replyflow-rag: direct link, hf CLI and curl.
- Browser
- Download file 4.6 kB
-
https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/tests/load_test.py
- Command line
-
hf download hf://spaces/Qurrah/replyflow-rag/tests/load_test.py
-
curl -L -o load_test.py https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/tests/load_test.py
4.6 kB
| """ | |
| Load test for the /query endpoint. | |
| Fires N requests all at once (concurrently) and reports how long each one | |
| took, so you can see how badly requests queue up / delay each other under | |
| a simulated burst. | |
| Usage: | |
| python load_test.py --url http://localhost:7860/query --n 100 --api-key YOUR_KEY | |
| Adjust --payload-* args to match a real query for one of your test workspaces | |
| (channel/channel_identifier must resolve to a real workspace + Shopify creds | |
| in your DB, otherwise every request will just fail fast at the workspace | |
| lookup step and you won't be testing the real inventory/LLM path). | |
| """ | |
| import argparse | |
| import asyncio | |
| import statistics | |
| import time | |
| import httpx | |
| async def fire_one(client: httpx.AsyncClient, url: str, headers: dict, payload: dict, idx: int) -> dict: | |
| start = time.perf_counter() | |
| try: | |
| resp = await client.post(url, json=payload, headers=headers, timeout=60.0) | |
| elapsed = time.perf_counter() - start | |
| return { | |
| "idx": idx, | |
| "status": resp.status_code, | |
| "elapsed": elapsed, | |
| "error": None, | |
| } | |
| except Exception as e: | |
| elapsed = time.perf_counter() - start | |
| return { | |
| "idx": idx, | |
| "status": None, | |
| "elapsed": elapsed, | |
| "error": str(e), | |
| } | |
| async def main(args): | |
| headers = {"Content-Type": "application/json"} | |
| if args.api_key: | |
| headers["X-API-Key"] = args.api_key | |
| payload = { | |
| "query": args.query, | |
| "channel": args.channel, | |
| "channel_identifier": args.channel_identifier, | |
| } | |
| if args.post_caption: | |
| payload["post_caption"] = args.post_caption | |
| print(f"Firing {args.n} concurrent requests at {args.url} ...") | |
| overall_start = time.perf_counter() | |
| limits = httpx.Limits(max_connections=args.n + 10, max_keepalive_connections=args.n + 10) | |
| async with httpx.AsyncClient(limits=limits) as client: | |
| tasks = [ | |
| fire_one(client, args.url, headers, payload, i) | |
| for i in range(args.n) | |
| ] | |
| results = await asyncio.gather(*tasks) | |
| overall_elapsed = time.perf_counter() - overall_start | |
| # --- Report --- | |
| successes = [r for r in results if r["error"] is None and r["status"] and r["status"] < 400] | |
| failures = [r for r in results if r not in successes] | |
| print("\n" + "=" * 60) | |
| print(f"Total wall-clock time for all {args.n} requests: {overall_elapsed:.2f}s") | |
| print(f"Successful responses: {len(successes)} / {args.n}") | |
| print(f"Failed/errored responses: {len(failures)} / {args.n}") | |
| if successes: | |
| elapsed_times = [r["elapsed"] for r in successes] | |
| elapsed_times.sort() | |
| print("\n--- Latency stats (successful requests only) ---") | |
| print(f" min: {min(elapsed_times):.2f}s") | |
| print(f" max: {max(elapsed_times):.2f}s") | |
| print(f" mean: {statistics.mean(elapsed_times):.2f}s") | |
| print(f" median: {statistics.median(elapsed_times):.2f}s") | |
| if len(elapsed_times) >= 20: | |
| p90 = elapsed_times[int(len(elapsed_times) * 0.90)] | |
| p99 = elapsed_times[int(len(elapsed_times) * 0.99)] | |
| print(f" p90: {p90:.2f}s") | |
| print(f" p99: {p99:.2f}s") | |
| if failures: | |
| print("\n--- Failures (first 10 shown) ---") | |
| for r in failures[:10]: | |
| print(f" idx={r['idx']} status={r['status']} error={r['error']}") | |
| print("\n--- Per-request timeline (sorted by completion time) ---") | |
| for r in sorted(results, key=lambda r: r["elapsed"]): | |
| status_str = r["status"] if r["status"] else f"ERROR: {r['error']}" | |
| print(f" idx={r['idx']:>3} | {r['elapsed']:.2f}s | status={status_str}") | |
| print("=" * 60) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Load test the /query endpoint") | |
| parser.add_argument("--url", default="http://localhost:7860/query", help="Full URL to /query endpoint") | |
| parser.add_argument("--n", type=int, default=100, help="Number of concurrent requests to fire") | |
| parser.add_argument("--api-key", default=None, help="API key for auth header") | |
| parser.add_argument("--query", default="do you have this in stock?", help="Query text to send") | |
| parser.add_argument("--channel", default="instagram", help="Channel value") | |
| parser.add_argument("--channel-identifier", default="123", help="Channel identifier (must exist in your DB)") | |
| parser.add_argument("--post-caption", default=None, help="Post caption text (optional)") | |
| args = parser.parse_args() | |
| asyncio.run(main(args)) |