#!/usr/bin/env python3
"""Measure latency of an OpenAI-compatible chat endpoint (lesson AUT-203-L06). Standard library only.

Sends the same short classification request N times and reports median and 95th percentile latency,
and the token usage the endpoint reports. Use ONLY synthetic text.

  LLM_BASE_URL (default http://127.0.0.1:8787/v1 — the mock model), LLM_MODEL, LLM_API_KEY

    python measure_latency.py 20
"""
import json
import os
import statistics
import sys
import time
import urllib.request

BASE = os.environ.get("LLM_BASE_URL", "http://127.0.0.1:8787/v1").rstrip("/")
MODEL = os.environ.get("LLM_MODEL", "mock-model")
KEY = os.environ.get("LLM_API_KEY", "local-demo-token" if "127.0.0.1:8787" in BASE else "")
BODY = {"model": MODEL, "messages": [
    {"role": "system", "content": "Classify the bookstore message. Return only JSON with category, order_id, urgent, summary, needs_human."},
    {"role": "user", "content": "<message>سفارش ۴۸۲۱۵ هنوز نرسیده</message>"}]}


def once():
    headers = {"Content-Type": "application/json"}
    if KEY:
        headers["Authorization"] = f"Bearer {KEY}"
    req = urllib.request.Request(BASE + "/chat/completions", method="POST", headers=headers,
                                 data=json.dumps(BODY, ensure_ascii=False).encode("utf-8"))
    t0 = time.perf_counter()
    with urllib.request.urlopen(req, timeout=120) as r:
        data = json.load(r)
    return (time.perf_counter() - t0) * 1000, data.get("usage") or {}


def main(n):
    times, usage = [], {}
    for _ in range(n):
        ms, usage = once()
        times.append(ms)
    times.sort()
    p95 = times[max(0, int(round(0.95 * len(times))) - 1)]
    print(f"endpoint {BASE}  model {MODEL}  runs {n}")
    print(f"median {statistics.median(times):.0f} ms   p95 {p95:.0f} ms   max {times[-1]:.0f} ms")
    print(f"reported usage per call: {usage}")


if __name__ == "__main__":
    main(int(sys.argv[1]) if len(sys.argv) > 1 else 10)
