# Benchmark CI — tracks performance across merges and on-demand for PRs.
#
# Triggers:
#   - Every push to main (historical tracking)
#   - PRs with the "benchmark" label (regression alerts on every push)
#   - Weekly Monday 06:00 UTC (catch dependency regressions)
#   - Manual dispatch
#
# Historical dashboard deployed to GitHub Pages at:
#   https://scylladb.github.io/cqlsh-rs/dev/bench/

name: Benchmarks

on:
  push:
    branches: [main]
  pull_request:
    types: [opened, synchronize, reopened, labeled]
  schedule:
    - cron: '0 6 * * 1'
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

jobs:
  benchmark:
    if: >
      github.event_name == 'push' ||
      github.event_name == 'schedule' ||
      github.event_name == 'workflow_dispatch' ||
      (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'benchmark'))
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry & build artifacts
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-bench-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-bench-

      - name: Run benchmarks
        run: cargo bench --bench benchmarks -- --warm-up-time 1 --measurement-time 1 --sample-size 20 2>&1 | tee bench_output.txt

      - name: Parse benchmark results
        run: python3 scripts/format_bench_summary.py < bench_output.txt >> "$GITHUB_STEP_SUMMARY" || echo "No benchmark results" >> "$GITHUB_STEP_SUMMARY"

      - name: Install hyperfine
        run: sudo apt-get install -y hyperfine

      - name: Install Python cqlsh
        run: pip install cqlsh

      - name: Build cqlsh-rs release
        run: cargo build --release

      - name: Startup comparison (Rust vs Python)
        run: |
          scripts/bench_comparison.sh
          echo "## Rust vs Python: Startup" >> "$GITHUB_STEP_SUMMARY"
          cat startup-comparison.md >> "$GITHUB_STEP_SUMMARY"

      - name: Upload criterion HTML report
        uses: actions/upload-artifact@v6
        with:
          name: criterion-report-${{ github.sha }}
          path: target/criterion/
          retention-days: 90

      - name: Upload raw benchmark output
        uses: actions/upload-artifact@v6
        with:
          name: benchmark-output-${{ github.sha }}
          path: bench_output.txt
          retention-days: 90

      - name: Ensure gh-pages branch exists
        run: |
          if ! git ls-remote --exit-code origin gh-pages >/dev/null 2>&1; then
            CURRENT_REF=$(git rev-parse HEAD)
            echo "Creating gh-pages branch..."
            git config user.name "github-actions[bot]"
            git config user.email "github-actions[bot]@users.noreply.github.com"
            git checkout --orphan gh-pages
            git reset --hard
            git commit --allow-empty -m "chore: initialize gh-pages branch"
            git push origin gh-pages
            git checkout "$CURRENT_REF"
          fi

      - name: Store results & track history
        uses: benchmark-action/github-action-benchmark@v1
        with:
          tool: 'customSmallerIsBetter'
          output-file-path: output.json
          github-token: ${{ secrets.GITHUB_TOKEN }}
          # Only push historical data on main merges (not PRs/schedule)
          auto-push: ${{ github.event_name == 'push' }}
          # Alert if any benchmark regresses >50% from baseline
          alert-threshold: '150%'
          # Always post benchmark results as a PR comment
          comment-on-alert: true
          comment-always: true
          # Don't fail the workflow — just alert
          fail-on-alert: false
          # Historical data location on gh-pages branch
          benchmark-data-dir-path: 'dev/bench'

