name: Simple benchmarks
on:
  push:
    branches:
      - main
  pull_request:


permissions:
  # deployments permission to deploy GitHub pages website
  deployments: write
  # contents permission to update benchmark contents in gh-pages branch
  contents: write

jobs:
  benchmark:
    name: Performance regression check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2
      - name: Install Rust
        uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8  # stable
        with:
          components: rustfmt, clippy

      - name: Install Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405  # v6
        with:
          python-version: "3.12"
          architecture: "x64"

      - name: Install
        working-directory: ./bindings/python
        run: |
          pip install -U pip uv
          uv sync --frozen --extra dev

      - name: Run tests
        working-directory: ./bindings/python
        run: |
          cargo test
          uv run --frozen pytest --benchmark-json output.json benches/
      # Download previous benchmark result from cache (if exists)
      - name: Download previous benchmark data
        uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7  # v5
        with:
          path: ./cache
          key: ${{ runner.os }}-benchmark
      # Run `github-action-benchmark` action
      - name: Store benchmark result
        uses: benchmark-action/github-action-benchmark@a60cea5bc7b49e15c1f58f411161f99e0df48372  # v1
        with:
          # What benchmark tool the output.txt came from
          tool: 'pytest'
          # Where the output from the benchmark tool is stored
          output-file-path: ./bindings/python/output.json
          github-token: ${{ secrets.GITHUB_TOKEN }}
          # Push and deploy GitHub pages branch automatically
          auto-push: ${{ github.event.pull_request.head.repo.fork == false }}
          comment-on-alert: true
          # Mention @rhysd in the commit comment
          alert-comment-cc-users: '@danieldk,@McPatate'
