#!/usr/bin/env bash
set -euo pipefail
BRANCH="${1:?Usage: bench_compare.sh <branch-or-commit>}"
TS=$(date +%Y%m%d-%H%M%S)
OUTDIR="$HOME/results/${BRANCH//\//-}-${TS}"
mkdir -p "$OUTDIR"

cd ~/typeagent
git fetch origin
git checkout "$BRANCH"

# Rebuild after switching branches
export PATH="$HOME/.local/bin:$PATH"
uv sync

PYTHON=~/typeagent/.venv/bin/python

echo "=== Benchmarking branch: $BRANCH ==="

# Import time (direct venv to avoid uv run overhead)
hyperfine --warmup 5 --min-runs 30 --shell=none \
  --export-json "$OUTDIR/import.json" \
  "$PYTHON -c 'import typeagent'"

# Offline test suite
hyperfine --warmup 2 --min-runs 10 --shell=none \
  --export-json "$OUTDIR/test_suite.json" \
  "$PYTHON -m pytest tests/test_incremental_index.py tests/test_add_messages_with_indexing.py tests/test_podcast_incremental.py tests/test_sqlite_indexes.py tests/test_query.py -q"

echo ""
echo "Results saved to $OUTDIR/"
ls -la "$OUTDIR/"
