name: Benchmark Go (PR)

on:
  pull_request:
    branches:
      - main

permissions:
  contents: read

jobs:
  benchmark:
    name: Performance regression check
    runs-on: ubuntu-latest
    permissions:
      # allow posting comments to pull request
      pull-requests: write

    steps:
      - name: Fetch Repo
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false

      - name: Install Go
        uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
        with:
          go-version-file: go.mod

      - name: Run benchmark
        run: set -o pipefail; go test ./... -benchmem -run=^$ -bench . | tee output.txt

      - name: Store benchmark result
        uses: benchmark-action/github-action-benchmark@4bdcce38c94cec68da58d012ac24b7b1155efe8b # v1.20.7
        with:
          tool: 'go'
          output-file-path: output.txt
          github-token: ${{ secrets.GITHUB_TOKEN }}
          auto-push: false
          alert-threshold: "150%"
          fail-on-alert: false # Don't make red crosses on the PR, it's almost certainly a false positive currently
          comment-on-alert: true # notify on PR if alert triggers
          summary-always: true # always comment on PRs to leave job summary
