#!/usr/bin/env bash
set -euo pipefail
export PATH="$HOME/.local/bin:$PATH"

cd ~/rich
TS=$(date +%Y%m%d-%H%M%S)
OUTDIR="$HOME/results/e2e-${TS}"
mkdir -p "$OUTDIR"

VENV_PY="$HOME/rich/.venv/bin/python"
SETUP='cd ~/rich && git checkout {branch} -q && PATH=$HOME/.local/bin:$PATH uv pip install --python '"$VENV_PY"' -e . -q 2>/dev/null'

echo "=== E2E Benchmark: master vs codeflash/optimize ==="
echo "Python: $($VENV_PY --version)"
echo "Output: $OUTDIR"
echo ""

echo "--- Console import ---"
hyperfine --warmup 5 --min-runs 30 --shell=bash \
    --export-json "$OUTDIR/console.json" \
    -L branch master,codeflash/optimize \
    --setup "$SETUP" \
    -n '{branch}' \
    "$VENV_PY -c 'from rich.console import Console'"

echo ""
echo "--- RichHandler import ---"
hyperfine --warmup 5 --min-runs 30 --shell=bash \
    --export-json "$OUTDIR/richhandler.json" \
    -L branch master,codeflash/optimize \
    --setup "$SETUP" \
    -n '{branch}' \
    "$VENV_PY -c 'from rich.logging import RichHandler'"

echo ""
echo "--- import rich ---"
hyperfine --warmup 5 --min-runs 30 --shell=bash \
    --export-json "$OUTDIR/rich.json" \
    -L branch master,codeflash/optimize \
    --setup "$SETUP" \
    -n '{branch}' \
    "$VENV_PY -c 'import rich'"

echo ""
echo "--- Per-module breakdown (codeflash/optimize) ---"
git checkout codeflash/optimize -q
uv pip install --python $VENV_PY -e . -q 2>/dev/null
hyperfine --warmup 3 --min-runs 20 --shell=none \
    --export-json "$OUTDIR/modules.json" \
    -n 'import rich' "$VENV_PY -c 'import rich'" \
    -n 'Console' "$VENV_PY -c 'from rich.console import Console'" \
    -n 'RichHandler' "$VENV_PY -c 'from rich.logging import RichHandler'" \
    -n 'Traceback' "$VENV_PY -c 'from rich.traceback import Traceback'" \
    -n 'Syntax' "$VENV_PY -c 'from rich.syntax import Syntax'" \
    -n 'Markdown' "$VENV_PY -c 'from rich.markdown import Markdown'"

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