File size: 4,595 Bytes
a9d7074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
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))