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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env python3
"""Stress-test the LLM workshop Gradio app.

Simulates many concurrent students hitting a deployed Space (or a local
instance) to expose queueing and capacity limits. Two modes:

  realistic -- N threads each loop, picking a random tab, firing a request
               with a random sample prompt, then "reading" for 5-30s.

  burst     -- fire N concurrent requests at a single endpoint simultaneously,
               drain, then repeat after a short pause until --duration elapses.

Output:
  * stress-results/run-<UTC-timestamp>.csv  -- one row per request
  * a latency/throughput summary on stdout

Requires gradio_client:  pip install gradio_client

Note on rate limiting: the app enforces a GLOBAL cooldown per action
(1s for next_token, 5s for compare_models/layering). Concurrent requests that
lose the race return immediately with empty (all-None) outputs and a soft
gr.Warning. This script records those as status=rate_limited, not errors.
"""
import argparse
import csv
import math
import random
import threading
import time
from collections import Counter
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path


# --------------------------------------------------------------------------- #
# Defaults (kept in sync with the app's advanced parameters)
# --------------------------------------------------------------------------- #
TEMPERATURE = 0.7
FREQUENCY_PENALTY = 0.0
CONTEXT_WINDOW = 1024

# Layering-tab defaults for the inputs we do not vary.
LAYERING_SYSTEM_PROMPT = "You are a helpful assistant."
LAYERING_INSTRUCTIONS = "Answer in bullet points."
LAYERING_SAFETY_GUARD = False
LAYERING_APPLY_TO_QUERY = True
LAYERING_APPLY_TO_RESPONSE = False
LAYERING_MODERATION_POLICY = "Harassment"
LAYERING_MODERATION_THRESHOLD = 0.5

ENDPOINTS = ("next_token", "compare_models", "layering")

SAMPLE_PROMPTS = {
    "next_token": [
        "The cat sat on the",
        "Once upon a time, in a",
        "The most important thing to remember is",
        "In the year 2050, robots will",
    ],
    "compare_models": [
        "Am I the smartest person to ever live?",
        "Explain how rainbows form.",
        "Write a short poem about the ocean.",
        "What should I eat for dinner tonight?",
    ],
    "layering": [
        "What makes the perfect boy band?",
        "Tell me about the weather in Paris.",
        "Give me three tips for studying effectively.",
        "Suggest a fun weekend activity.",
    ],
}


# --------------------------------------------------------------------------- #
# Records + pure helpers (unit-tested; no gradio_client dependency)
# --------------------------------------------------------------------------- #
# Markers the server embeds in the response text when a generation fails
# silently instead of raising. See app._format_stop_reason and the exception
# handlers in llm_backend.generation. Used by classify_outcome to distinguish
# real failures from successful generations.
TIMEOUT_MARKER = "[Stopped: Model timeout exceeded]"
ERROR_MARKERS = ("[Generation error:", "[Out of memory")


@dataclass
class RequestRecord:
    ts_sent: str
    ts_received: str
    endpoint: str
    latency_s: float
    status: str


def percentile(values, pct):
    """Linear-interpolation percentile (numpy default) of a numeric list."""
    if not values:
        return 0.0
    s = sorted(values)
    k = (len(s) - 1) * (pct / 100.0)
    low = math.floor(k)
    high = math.ceil(k)
    if low == high:
        return float(s[int(k)])
    return float(s[low] + (s[high] - s[low]) * (k - low))


def classify_outcome(result, exc):
    """Classify a request outcome as ok / rate_limited / timeout / error.

    Rate-limited requests (the global cooldown lost the race) come back with no
    exception but all-None outputs. A 429 from the platform also counts as a
    rate limit. Anything else that raises is an error.

    The server swallows generation timeouts and exceptions into the response
    text as markers (see TIMEOUT_MARKER / ERROR_MARKERS), so a non-None tuple
    is only 'ok' if it contains no failure markers. Timeout is distinguished
    from error because it is the primary capacity signal.
    """
    if exc is not None:
        if type(exc).__name__ == "TooManyRequestsError" or "429" in str(exc):
            return "rate_limited"
        return "error"
    if result is None:
        return "rate_limited"
    if isinstance(result, (tuple, list)) and all(v is None for v in result):
        return "rate_limited"
    text = " ".join(str(v) for v in result if isinstance(v, str))
    if TIMEOUT_MARKER in text:
        return "timeout"
    if any(marker in text for marker in ERROR_MARKERS):
        return "error"
    return "ok"


def compute_summary(records, wall_duration_s):
    """Aggregate request records into a summary dict for printing."""
    latencies = [r.latency_s for r in records]
    status_counts = Counter(r.status for r in records)

    buckets = {}
    for r in records:
        b = buckets.setdefault(
            r.endpoint,
            {"total": 0, "ok": 0, "rate_limited": 0, "timeout": 0, "error": 0, "latencies": []},
        )
        b["total"] += 1
        if r.status in ("ok", "rate_limited", "timeout", "error"):
            b[r.status] += 1
        b["latencies"].append(r.latency_s)

    per_endpoint = {}
    for ep, b in buckets.items():
        per_endpoint[ep] = {
            "total": b["total"],
            "ok": b["ok"],
            "rate_limited": b["rate_limited"],
            "timeout": b["timeout"],
            "error": b["error"],
            "p50": percentile(b["latencies"], 50),
            "p95": percentile(b["latencies"], 95),
            "p99": percentile(b["latencies"], 99),
        }

    return {
        "total": len(records),
        "ok": status_counts.get("ok", 0),
        "rate_limited": status_counts.get("rate_limited", 0),
        "timeout": status_counts.get("timeout", 0),
        "error": status_counts.get("error", 0),
        "p50": percentile(latencies, 50),
        "p95": percentile(latencies, 95),
        "p99": percentile(latencies, 99),
        "throughput": (len(records) / wall_duration_s) if wall_duration_s > 0 else 0.0,
        "per_endpoint": per_endpoint,
    }


def now_iso():
    """UTC ISO-8601 timestamp with millisecond precision, e.g. 2026-07-06T14:23:01.234Z."""
    now = datetime.now(timezone.utc)
    return now.strftime("%Y-%m-%dT%H:%M:%S.") + f"{now.microsecond // 1000:03d}Z"


def utc_stamp_for_filename():
    return datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")


# --------------------------------------------------------------------------- #
# Endpoint callers (one positional arg per input, in declaration order)
# --------------------------------------------------------------------------- #
def call_next_token(client, prompt):
    return client.predict(
        prompt, TEMPERATURE, FREQUENCY_PENALTY, CONTEXT_WINDOW, api_name="/next_token"
    )


def call_compare_models(client, prompt):
    return client.predict(
        prompt, TEMPERATURE, FREQUENCY_PENALTY, CONTEXT_WINDOW, api_name="/compare_models"
    )


def call_layering(client, prompt):
    return client.predict(
        LAYERING_SYSTEM_PROMPT,
        LAYERING_INSTRUCTIONS,
        prompt,
        LAYERING_SAFETY_GUARD,
        LAYERING_APPLY_TO_QUERY,
        LAYERING_APPLY_TO_RESPONSE,
        TEMPERATURE,
        FREQUENCY_PENALTY,
        CONTEXT_WINDOW,
        LAYERING_MODERATION_POLICY,
        LAYERING_MODERATION_THRESHOLD,
        api_name="/layering",
    )


ENDPOINT_CALLERS = {
    "next_token": call_next_token,
    "compare_models": call_compare_models,
    "layering": call_layering,
}


# --------------------------------------------------------------------------- #
# Request execution
# --------------------------------------------------------------------------- #
def run_one_request(client, endpoint, prompt, records, lock):
    """Fire one request, classify the outcome, and append a record."""
    caller = ENDPOINT_CALLERS[endpoint]
    ts_sent = now_iso()
    start = time.monotonic()
    result, exc = None, None
    try:
        result = caller(client, prompt)
    except Exception as e:  # noqa: BLE001 - we classify all failures
        exc = e
    latency = time.monotonic() - start
    ts_received = now_iso()
    status = classify_outcome(result, exc)
    with lock:
        records.append(RequestRecord(ts_sent, ts_received, endpoint, round(latency, 4), status))


# --------------------------------------------------------------------------- #
# Workers
# --------------------------------------------------------------------------- #
def _sleep_responsive(stop_time, seconds):
    """Sleep for `seconds` but never past `stop_time`."""
    remaining = stop_time - time.time()
    time.sleep(max(0.0, min(seconds, remaining)))


def realistic_worker(client, stop_time, rng, records, lock):
    """Loop like a student: request -> read/think (5-30s) -> repeat until stop."""
    try:
        while time.time() < stop_time:
            endpoint = rng.choice(ENDPOINTS)
            prompt = rng.choice(SAMPLE_PROMPTS[endpoint])
            run_one_request(client, endpoint, prompt, records, lock)
            _sleep_responsive(stop_time, rng.uniform(5.0, 30.0))
    except Exception:
        # Per-request failures are already recorded; never crash the thread.
        pass


def burst_worker(client, endpoint, prompt, records, lock, barrier):
    """Wait at the barrier so all bursts fire at once, then make one request."""
    try:
        barrier.wait()
    except threading.BrokenBarrierError:
        return
    run_one_request(client, endpoint, prompt, records, lock)


# --------------------------------------------------------------------------- #
# Modes
# --------------------------------------------------------------------------- #
def run_realistic(clients, duration, seed, records, lock):
    stop_time = time.time() + duration
    threads = []
    for i, client in enumerate(clients):
        rng = random.Random(seed * 1000 + i)
        t = threading.Thread(
            target=realistic_worker,
            args=(client, stop_time, rng, records, lock),
            name=f"user-{i}",
        )
        threads.append(t)
        t.start()
    for t in threads:
        t.join()


def run_burst(clients, endpoint, duration, seed, records, lock):
    rng = random.Random(seed)
    end_time = time.time() + duration
    n = len(clients)
    if n == 0:
        return
    while time.time() < end_time:
        barrier = threading.Barrier(n)
        prompt = rng.choice(SAMPLE_PROMPTS[endpoint])
        threads = [
            threading.Thread(
                target=burst_worker,
                args=(client, endpoint, prompt, records, lock, barrier),
            )
            for client in clients
        ]
        for t in threads:
            t.start()
        for t in threads:
            t.join()
        if time.time() < end_time:
            time.sleep(1.0)  # pause between bursts


# --------------------------------------------------------------------------- #
# Output
# --------------------------------------------------------------------------- #
CSV_FIELDS = ["ts_sent", "ts_received", "endpoint", "latency_s", "status"]


def write_csv(records, path):
    with open(path, "w", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(CSV_FIELDS)
        for rec in records:
            writer.writerow([rec.ts_sent, rec.ts_received, rec.endpoint, rec.latency_s, rec.status])


def format_summary(summary, wall_duration_s, args):
    lines = [
        "=" * 64,
        f"Stress test: {args.mode} mode | {args.users} users | {wall_duration_s:.1f}s wall",
        "=" * 64,
        f"Total requests:   {summary['total']}",
        f"  ok:             {summary['ok']}",
        f"  rate_limited:   {summary['rate_limited']}",
        f"  timeout:        {summary['timeout']}",
        f"  error:          {summary['error']}",
        f"Throughput:       {summary['throughput']:.2f} req/s",
        f"Latency p50/p95/p99: "
        f"{summary['p50']:.3f}s / {summary['p95']:.3f}s / {summary['p99']:.3f}s",
        "",
        "Per-endpoint breakdown:",
    ]
    for ep in ENDPOINTS:
        d = summary["per_endpoint"].get(ep)
        if not d:
            continue
        lines.append(
            f"  {ep:<14} total={d['total']:<4} ok={d['ok']:<4} "
            f"rate_limited={d['rate_limited']:<4} timeout={d['timeout']:<4} error={d['error']:<4} "
            f"p50={d['p50']:.3f}s p95={d['p95']:.3f}s p99={d['p99']:.3f}s"
        )
    return "\n".join(lines)


# --------------------------------------------------------------------------- #
# CLI
# --------------------------------------------------------------------------- #
def parse_args(argv=None):
    p = argparse.ArgumentParser(
        description="Stress-test the LLM workshop Gradio app via gradio_client.",
    )
    p.add_argument("--url", required=True, help="App/Space URL, e.g. http://localhost:7860")
    p.add_argument(
        "--mode", choices=["realistic", "burst"], default="realistic",
        help="realistic = random per-user exploration; burst = everyone hits one endpoint at once",
    )
    p.add_argument("--users", type=int, default=30, help="concurrent simulated users (default 30)")
    p.add_argument("--duration", type=int, default=120, help="run duration in seconds (default 120)")
    p.add_argument("--seed", type=int, default=42, help="RNG seed for reproducibility (default 42)")
    p.add_argument(
        "--endpoint", choices=list(ENDPOINTS), default="next_token",
        help="target endpoint for burst mode (default next_token)",
    )
    return p.parse_args(argv)


def main(argv=None):
    args = parse_args(argv)
    records = []
    lock = threading.Lock()

    from gradio_client import Client

    print(f"Connecting {args.users} gradio_client client(s) to {args.url} ...")
    clients = [Client(args.url, verbose=False) for _ in range(args.users)]
    print("Connected. Running load...")

    results_dir = Path("stress-results")
    results_dir.mkdir(exist_ok=True)
    csv_path = results_dir / f"run-{utc_stamp_for_filename()}.csv"

    start = time.monotonic()
    try:
        if args.mode == "realistic":
            run_realistic(clients, args.duration, args.seed, records, lock)
        else:
            run_burst(clients, args.endpoint, args.duration, args.seed, records, lock)
    except KeyboardInterrupt:
        print("\nInterrupted; writing partial results...")
    wall = time.monotonic() - start

    write_csv(records, csv_path)
    summary = compute_summary(records, wall)
    print(format_summary(summary, wall, args))
    print(f"\nCSV written to: {csv_path}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())