"""
Gemma 4 26B-A4B Q4_K_M - 76.4 t/s 재현 테스트
이전 최적값: ngl=999 t=6 ub=512 b=2048 ctk=f16 ctv=f16
"""
import subprocess, time, json, urllib.request, sys, os

try: sys.stdout.reconfigure(encoding='utf-8')
except: pass

LLAMA = os.path.join(os.getcwd(), "llama_bin_run", "llama-server.exe")
MODEL = os.path.join(os.getcwd(), "models", "gemma-4-26B-A4B-it-Q4_K_M.gguf")

subprocess.run(["taskkill", "/F", "/IM", "llama-server.exe"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
time.sleep(3)

cmd = [
    LLAMA, "--model", MODEL,
    "-ngl", "999", "-c", "262144", "-np", "1", "-fa", "on",
    "--cache-type-k", "f16", "--cache-type-v", "f16",
    "-ub", "512", "-b", "2048", "-t", "6", "-tb", "6",
    "--prio", "3", "--mlock", "--poll", "50",
    "--port", "8000", "--host", "0.0.0.0",
]

print("[1/4] Starting Gemma4 26B Q4_K_M (76.4 t/s config)...")
server = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

print("[2/4] Waiting for boot...")
healthy = False
for sec in range(180):
    time.sleep(1)
    if server.poll() is not None:
        print(f"  !! CRASHED (exit code {server.returncode})")
        sys.exit(1)
    try:
        with urllib.request.urlopen("http://127.0.0.1:8000/health", timeout=1) as r:
            if json.loads(r.read()).get("status") == "ok":
                healthy = True; break
    except: pass
    if sec % 10 == 9: print(f"  ... {sec+1}s")

if not healthy:
    print("  FAIL: boot timeout"); server.kill(); sys.exit(1)

print(f"  OK!")
try:
    v = subprocess.run(["nvidia-smi", "--query-gpu=index,memory.used,memory.total", "--format=csv,noheader,nounits"], capture_output=True, text=True, timeout=5)
    print(f"  VRAM: {v.stdout.strip()}")
except: pass

def bench(n):
    payload = json.dumps({"messages": [{"role": "user", "content": "Count from 1 to 50, each number on a new line."}], "max_tokens": n, "temperature": 0}).encode()
    req = urllib.request.Request("http://127.0.0.1:8000/v1/chat/completions", data=payload, headers={"Content-Type": "application/json"})
    t0 = time.time()
    with urllib.request.urlopen(req, timeout=120) as r:
        res = json.loads(r.read())
    el = time.time() - t0
    ct = res["usage"]["completion_tokens"]
    return ct / el, ct, el

try: bench(10)
except: pass

print("[3/4] Running 5x benchmark (200 tokens)...")
results = []
for i in range(5):
    tps, tok, el = bench(200)
    results.append(tps)
    print(f"  Run {i+1}: {tps:.2f} t/s  ({tok} tok / {el:.2f}s)")

avg = sum(results) / len(results)
best = max(results)
worst = min(results)
summary = f"""
==================================================
 Gemma4 26B Q4_K_M  5-Run Results:
   AVG:  {avg:.2f} t/s
   BEST: {best:.2f} t/s
   MIN:  {worst:.2f} t/s
   Runs: {[f'{r:.2f}' for r in results]}
==================================================
"""
print(summary)
with open("scripts/gemma4_test_result.txt", "w") as f:
    f.write(summary)

server.kill()
subprocess.run(["taskkill", "/F", "/IM", "llama-server.exe"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
