#!/usr/bin/env python3
"""Benchmark just qwen2.5-coder-14b at 65K."""
import json
import time
import urllib.request

BASE_URL = "http://127.0.0.1:9292/v1"

def api(model, msgs, max_tokens, temp=0.0, timeout=300):
    data = json.dumps({"model": model, "messages": msgs, "max_tokens": max_tokens, "temperature": temp}).encode()
    req = urllib.request.Request(f"{BASE_URL}/chat/completions", data=data, headers={"Content-Type": "application/json"}, method="POST")
    start = time.time()
    resp = urllib.request.urlopen(req, timeout=timeout)
    elapsed = time.time() - start
    r = json.loads(resp.read())
    u = r.get("usage", {})
    return {"time": elapsed, "prompt_tokens": u.get("prompt_tokens",0), "completion_tokens": u.get("completion_tokens",0)}

LONG = "Explain in detail " * 100  # ~500-700 tok

print("=== qwen2.5-coder-14b (65K, Q4_K_M, dense 14B) ===")

print("Warming up...", flush=True)
w = api("qwen2.5-coder-14b", [{"role":"user","content":"Say ready"}], 5)
print(f"  Warm-up: {w['time']:.1f}s")

time.sleep(3)

print("Bench 1 - Prompt eval...", flush=True)
b1 = api("qwen2.5-coder-14b", [{"role":"user","content":LONG}], 5)
pe = b1["prompt_tokens"] / b1["time"]
print(f"  Prompt: {b1['prompt_tokens']} tok, Time: {b1['time']:.1f}s → {pe:.1f} tok/s")

time.sleep(3)

print("Bench 2 - Generation...", flush=True)
b2 = api("qwen2.5-coder-14b", [{"role":"user","content":"Write a 500-word essay on AGI's impact on society."}], 500, temp=0.7)
gen = b2["completion_tokens"] / b2["time"]
print(f"  Gen: {b2['completion_tokens']} tok, Time: {b2['time']:.1f}s → {gen:.1f} tok/s")

print(f"\nResults: Prompt eval {pe:.1f} t/s | Gen {gen:.1f} t/s | 65K context")
