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

cd ~/rich
git checkout "$BRANCH"
export PATH="$HOME/.local/bin:$PATH"
uv pip install -e .

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

hyperfine --warmup 3 --min-runs 30 --shell=none \
  --export-json "$OUTDIR/import.json" \
  "$VENV_PYTHON -c 'import rich'"

hyperfine --warmup 3 --min-runs 20 --shell=none \
  --export-json "$OUTDIR/modules.json" \
  -n 'console' "$VENV_PYTHON -c 'from rich.console import Console'" \
  -n 'logging' "$VENV_PYTHON -c 'from rich.logging import RichHandler'" \
  -n 'traceback' "$VENV_PYTHON -c 'from rich.traceback import Traceback'" \
  -n 'syntax' "$VENV_PYTHON -c 'from rich.syntax import Syntax'" \
  -n 'markdown' "$VENV_PYTHON -c 'from rich.markdown import Markdown'"

python3 ~/bench/bench_importtime.py "import rich" "$OUTDIR/importtime.tsv"

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