"""Benchmark a local Strata server with the same protocol as moe-expert-streaming's scripts/bench.py.

Four 200-token decode prompts once each (story first), a 4,059-token prompt (prefill), then 200 tokens after a
6,062-token document. Greedy, thinking off. Streams each answer and times it on the client; also keeps the server's own
`timings` object when it sends one.

    python strata_bench.py prompts.json out.json [--rounds N] [--only decode,prefill_4k,long_doc_6k]
"""

import argparse
import json
import time
import urllib.request

URL = "http://127.0.0.1:8080/v1/chat/completions"


def run(prompt, max_tokens):
    body = {
        "model": "strata",
        "messages": [{"role": "user", "content": prompt}],
        "max_tokens": max_tokens,
        "temperature": 0,
        "stream": True,
        "stream_options": {"include_usage": True},
        "reasoning_effort": "none",
        "chat_template_kwargs": {"enable_thinking": False},
        "ignore_eos": True,
    }
    req = urllib.request.Request(URL, json.dumps(body).encode(), {"Content-Type": "application/json"})
    start = time.perf_counter()
    first = None
    pieces, chunks, usage, timings = [], 0, None, None
    with urllib.request.urlopen(req, timeout=3600) as resp:
        for raw in resp:
            line = raw.decode().strip()
            if not line.startswith("data:"):
                continue
            data = line[5:].strip()
            if data == "[DONE]":
                break
            event = json.loads(data)
            usage = event.get("usage") or usage
            timings = event.get("timings") or timings
            for choice in event.get("choices", []):
                delta = choice.get("delta", {})
                text = (delta.get("content") or "") + (delta.get("reasoning_content") or "")
                if text:
                    if first is None:
                        first = time.perf_counter()
                    chunks += 1
                    pieces.append(text)
    end = time.perf_counter()
    new_tokens = (usage or {}).get("completion_tokens") or chunks
    prompt_tokens = (usage or {}).get("prompt_tokens")
    ttft = (first or end) - start
    return {
        "prompt_tokens": prompt_tokens,
        "new_tokens": new_tokens,
        "stream_chunks": chunks,
        "time_to_first_token_s": ttft,
        "prefill_tokens_per_s": prompt_tokens / ttft if prompt_tokens else None,
        "decode_tokens_per_s": (new_tokens - 1) / (end - first) if first and new_tokens > 1 else None,
        "total_s": end - start,
        "server_timings": timings,
        "text": "".join(pieces),
    }


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("prompts")
    parser.add_argument("output")
    parser.add_argument("--rounds", type=int, default=1)
    parser.add_argument("--only", default="decode,prefill_4k,long_doc_6k")
    args = parser.parse_args()
    prompts = json.load(open(args.prompts))
    only = args.only.split(",")
    report = {"runs": {"story": [], "prefill_4k": [], "long_doc_6k": []}}
    for _ in range(args.rounds):
        if "decode" in only:
            for prompt in prompts["decode"]:
                report["runs"]["story"].append({"prompt": prompt, **run(prompt, 200)})
                print(json.dumps({k: v for k, v in report["runs"]["story"][-1].items() if k != "text"}), flush=True)
        if "prefill_4k" in only:
            report["runs"]["prefill_4k"].append(run(prompts["prefill_4k"], 32))
            print(json.dumps({k: v for k, v in report["runs"]["prefill_4k"][-1].items() if k != "text"}), flush=True)
        if "long_doc_6k" in only:
            report["runs"]["long_doc_6k"].append(run(prompts["long_doc_6k"], 200))
            print(json.dumps({k: v for k, v in report["runs"]["long_doc_6k"][-1].items() if k != "text"}), flush=True)
    rates = sorted(r["decode_tokens_per_s"] for r in report["runs"]["story"] if r["decode_tokens_per_s"])
    if rates:
        report["decode_median"] = (rates[(len(rates) - 1) // 2] + rates[len(rates) // 2]) / 2
        print("decode median", report["decode_median"])
    json.dump(report, open(args.output, "w"), indent=2)


if __name__ == "__main__":
    main()
