#!/usr/bin/env bash
# h3-benchmark.sh — benchmark protocol v1, one row (see docs/h3-economics.md).
#
# Fixed test: barista prompt, seed 42, 8 steps euler/beta, 864x480, 15s,
# SageAttention2 ON, Spectrum OFF. One cold run + 5 warm runs; report the
# MEDIAN warm time, not the best. Before trusting the row, confirm in the
# ComfyUI boot log that sage attention is actually active and note the
# torch/CUDA build — numbers without them are meaningless.
#
# Usage: h3-benchmark.sh [endpoint]   (default http://127.0.0.1:8188 — tunnel first)
set -euo pipefail
HERE=$(cd "$(dirname "$0")" && pwd)
ENDPOINT="${1:-http://127.0.0.1:8188}"
PROMPT="A barista pours latte art in a sunlit cafe, steam rising, camera slowly pushes in, ambient cafe sounds"

run() { python3 "$HERE/h3-run.py" --endpoint "$ENDPOINT" --prompt "$PROMPT" \
        --seconds 15 --width 864 --height 480 --steps 8 --seed 42 \
        --prefix "h3bench/barista_864x480_s8" --label "$1"; }

echo "== cold run (includes model load into VRAM) =="
run bench-cold
echo "== 5 warm runs =="
for i in 1 2 3 4 5; do run "bench-warm-$i"; done

echo "== median warm total =="
grep bench-warm "$HERE/timings.csv" | awk -F, '{print $11}' | sort -n | awk '{a[NR]=$1} END{print a[int((NR+1)/2)] "s (median of " NR ")"}'
echo "fill the row in docs/h3-economics.md: \$/finished-min = (median_s/3600 × \$_hr) × 4; log checkpoint, ComfyUI version, torch/CUDA in Notes"
