# shellcheck shell=bash

bench_http_endpoint() {
  local label="$1" method="$2" path="$3" base="$4"
  local body="${5:-}" requests="${6:-${BENCH_REQUESTS:-100}}" warmup="${7:-${BENCH_WARMUP:-10}}"
  python3 - "$label" "$method" "$base$path" "$requests" "$warmup" "$body" <<'PY'
import statistics
import sys
import urllib.request

label, method, url, n_req, n_warm, body = sys.argv[1:7]
n_req = int(n_req)
n_warm = int(n_warm)
data = body.encode() if body else None
headers = {"Content-Type": "application/json"} if data else {}

def once():
    req = urllib.request.Request(url, data=data, headers=headers, method=method)
    t0 = __import__("time").perf_counter()
    with urllib.request.urlopen(req, timeout=10) as resp:
        resp.read()
    return (__import__("time").perf_counter() - t0) * 1000.0

for _ in range(n_warm):
    try:
        once()
    except Exception:
        pass

samples = []
errors = 0
for _ in range(n_req):
    try:
        samples.append(once())
    except Exception:
        errors += 1

if not samples:
    print(f"{label}: FAIL errors={errors}")
else:
    samples.sort()
    def pct(p):
        i = max(0, min(len(samples) - 1, int(len(samples) * p / 100.0) - 1))
        return samples[i]
    print(
        f"{label}: ok={len(samples)} err={errors} "
        f"p50={pct(50):.2f}ms p95={pct(95):.2f}ms avg={statistics.mean(samples):.2f}ms max={samples[-1]:.2f}ms"
    )
PY
}

bench_http_suite() {
  local base="${1:-$LM_URL}"
  if ! curl -sf "${base}/api/health" >/dev/null; then
    echo "Server không phản hồi tại $base" >&2
    return 1
  fi
  bench_http_endpoint "GET /api/health" GET "/api/health" "$base"
  bench_http_endpoint "GET /api/state" GET "/api/state" "$base"
  bench_http_endpoint "GET /api/missions" GET "/api/missions" "$base"
  bench_http_endpoint "GET /api/mission_queue" GET "/api/mission_queue" "$base"
  bench_http_endpoint "GET /api/v2.0.0/mission_queue" GET "/api/v2.0.0/mission_queue" "$base"
  bench_http_endpoint "GET /" GET "/" "$base"
  bench_http_endpoint "GET /missions.js" GET "/missions.js" "$base"
  local mid
  mid="$(curl -sf "${base}/api/missions" | python3 -c "import json,sys; m=json.load(sys.stdin).get('missions',[]); print(m[0]['id'] if m else '')" 2>/dev/null || true)"
  if [[ -n "$mid" ]]; then
    bench_http_endpoint "POST /api/v2.0.0/mission_queue" POST "/api/v2.0.0/mission_queue" "$base" \
      "{\"mission_id\":\"${mid}\",\"priority\":0}"
  fi
}
