name: run-benchmarks

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  pick-runner:
    runs-on: ubuntu-latest
    outputs:
      runner: ${{ steps.choose.outputs.use-runner }}
    steps:
      - uses: actions/checkout@v4
      - id: choose
        uses: ./.github/actions/runner-fallback
        with:
          primary-runner: "self-hosted"
          fallback-runner: "ubuntu-latest"
          github-token: ${{ secrets.CUSTOM_GH_TOKEN }}

  run-benchmarks:
    needs: pick-runner
    runs-on: ${{ fromJson(needs.pick-runner.outputs.runner) }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Install Python
        uses: actions/setup-python@v4
      - name: Install uv
        uses: astral-sh/setup-uv@v5
      - name: Setup venv
        run: |
          uv venv
          uv pip install pandas
          uv run .github/scripts/custom_benchmark_report.py --dry-run

      - name: Run benchmarks
        run: cargo bench --features bench

      - name: Generate custom benchmark reports
        run: |
          if [ -d ./target/criterion ]; then
            echo "Found benchmark reports, generating custom report..."
          else
            echo "No benchmark reports found, skipping custom report generation."
            exit 1
          fi
          uv run .github/scripts/custom_benchmark_report.py

      - name: Upload benchmark reports
        uses: actions/upload-artifact@v4
        with:
          name: benchmark-reports-${{ github.sha }}
          path: ./target/criterion/
