name: Benchmarks

on:
  pull_request:

jobs:
  bench:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          fetch-depth: 0

      - uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8 # stable
      - uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2
      - uses: cargo-bins/cargo-binstall@f8ce4d55b131f4a1e373b8747ca6b6a54133ae5a # v1.18.0

      - run: cargo binstall --no-confirm critcmp

      - name: Bench PR
        run: cargo bench -p swink-agent --bench context -- --save-baseline pr

      - name: Export PR baseline
        run: critcmp --export pr > "$RUNNER_TEMP/pr.json"

      - name: Add base worktree
        run: git worktree add ../bench-base ${{ github.event.pull_request.base.sha }}

      - name: Bench base
        working-directory: ../bench-base
        run: cargo bench -p swink-agent --bench context -- --save-baseline base

      - name: Export base baseline
        working-directory: ../bench-base
        run: critcmp --export base > "$RUNNER_TEMP/base.json"

      - name: Compare
        run: critcmp "$RUNNER_TEMP/base.json" "$RUNNER_TEMP/pr.json" | tee bench-diff.txt

      - name: Post comment
        uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
        with:
          script: |
            const fs = require('fs');
            const diff = fs.readFileSync('bench-diff.txt', 'utf8');
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '## Benchmark comparison\n```\n' + diff + '\n```'
            });
