#cloud-config
# Benchmark VM for attrs import time optimization
# Minimal setup: Python 3.13, uv, hyperfine, git

package_update: true

packages:
  - git
  - build-essential
  - curl
  - wget

runcmd:
  # Install hyperfine
  - wget -q https://github.com/sharkdp/hyperfine/releases/download/v1.19.0/hyperfine_1.19.0_amd64.deb -O /tmp/hyperfine.deb
  - dpkg -i /tmp/hyperfine.deb
  - rm /tmp/hyperfine.deb

  # Install uv for azureuser
  - su - azureuser -c 'curl -LsSf https://astral.sh/uv/install.sh | sh'

  # Clone attrs fork and set up both branches
  - su - azureuser -c 'export PATH=$HOME/.local/bin:$PATH && git clone https://github.com/KRRT7/attrs.git ~/attrs'
  - su - azureuser -c 'export PATH=$HOME/.local/bin:$PATH && cd ~/attrs && uv venv --python 3.13 && uv pip install -e "."'
  - su - azureuser -c 'export PATH=$HOME/.local/bin:$PATH && cd ~/attrs && uv pip install pytest hypothesis cloudpickle'

  # Write benchmark script
  - |
    cat > /home/azureuser/bench.sh << 'BENCHEOF'
    #!/usr/bin/env bash
    set -euo pipefail
    export PATH=$HOME/.local/bin:$PATH
    cd ~/attrs

    PYTHON=.venv/bin/python
    RUNS=100

    echo "=== attrs import time benchmark ==="
    echo "Python: $($PYTHON --version)"
    echo "CPU: $(lscpu | grep 'Model name' | sed 's/.*: *//')"
    echo "Runs: $RUNS per branch"
    echo ""

    # Optimized branch
    git checkout perf/defer-inspect-import -q
    uv pip install -e . -q
    echo "--- perf/defer-inspect-import ---"
    hyperfine --warmup 10 --runs $RUNS "$PYTHON -c 'import attrs'" --export-json /tmp/optimized.json
    echo ""
    echo "Without bytecode:"
    hyperfine --warmup 10 --runs $RUNS "PYTHONDONTWRITEBYTECODE=1 $PYTHON -c 'import attrs'" --export-json /tmp/optimized-nobc.json
    echo ""

    # Baseline
    git checkout main -q
    uv pip install -e . -q
    echo "--- main (baseline) ---"
    hyperfine --warmup 10 --runs $RUNS "$PYTHON -c 'import attrs'" --export-json /tmp/baseline.json
    echo ""
    echo "Without bytecode:"
    hyperfine --warmup 10 --runs $RUNS "PYTHONDONTWRITEBYTECODE=1 $PYTHON -c 'import attrs'" --export-json /tmp/baseline-nobc.json
    echo ""

    # importtime check
    echo "=== inspect deferred? ==="
    git checkout perf/defer-inspect-import -q
    uv pip install -e . -q
    echo -n "Optimized: "
    if $PYTHON -X importtime -c "import attr" 2>&1 | grep -q inspect; then
      echo "NO (inspect still eagerly imported)"
    else
      echo "YES (inspect deferred)"
    fi
    git checkout main -q
    uv pip install -e . -q
    echo -n "Baseline:  "
    if $PYTHON -X importtime -c "import attr" 2>&1 | grep -q inspect; then
      echo "inspect eagerly imported (expected)"
    else
      echo "inspect NOT imported (unexpected)"
    fi

    echo ""
    echo "=== Test suite (optimized branch) ==="
    git checkout perf/defer-inspect-import -q
    uv pip install -e . -q
    $PYTHON -m pytest tests/ -x -q 2>&1 | tail -5

    echo ""
    echo "Done. JSON results in /tmp/{baseline,optimized,baseline-nobc,optimized-nobc}.json"
    BENCHEOF
    chown azureuser:azureuser /home/azureuser/bench.sh
    chmod +x /home/azureuser/bench.sh
