import json, time, urllib.request

def call(messages, max_tokens=200):
    req = urllib.request.Request("http://localhost:8080/v1/chat/completions",
        data=json.dumps({"model":"Qwen3.8-27B-Uncensored-HauhauCS-Aggressive-Q4_K_P.gguf",
            "messages":messages,"max_tokens":max_tokens,"stream":True}).encode(),
        headers={"Content-Type":"application/json"})
    t0=time.time(); ttf=None; n_gen=0; n_reason=0
    with urllib.request.urlopen(req) as r:
        for line in r:
            line=line.decode().strip()
            if not line.startswith("data: ") or line[6:]=="[DONE]": continue
            d=json.loads(line[6:])
            delta=d["choices"][0].get("delta",{})
            c=delta.get("content"); rc=delta.get("reasoning_content")
            if rc:
                n_reason+=1
                if ttf is None: ttf=time.time()-t0
            if c:
                n_gen+=1
                if ttf is None: ttf=time.time()-t0
    t1=time.time()
    return ttf, t1-t0, n_gen, n_reason

# 1) typical short question, ~200 tokens out
ttf, tot, n, nr = call([{"role":"user","content":"用三句话介绍一下量子计算。"}])
print(f"[short q]  ttft={ttf:.2f}s  total={tot:.2f}s  out_tokens={n}  reason_tokens={nr}  speed={n/max(tot-ttf,0.01):.1f} t/s")

# 2) long prompt (~8k tokens) to simulate Hermes system prompt
long_ctx = "以下是背景资料。" + ("人工智能是计算机科学的一个重要分支。" * 700)
ttf, tot, n, nr = call([{"role":"system","content":long_ctx},{"role":"user","content":"请总结上面的资料，50字以内。"}], max_tokens=150)
print(f"[long ctx] ttft={ttf:.2f}s  total={tot:.2f}s  out_tokens={n}  speed={n/max(tot-ttf,0.01):.1f} t/s")
