| name: Performance Comparison for Pull Requests |
| |
| on: |
| pull_request: |
| branches: [master] |
| |
| jobs: |
| benchmark-pr: |
| name: Performance benchmark comparison |
| runs-on: ubuntu-latest |
| steps: |
| - name: Checkout PR branch |
| uses: actions/checkout@v4 |
| with: |
| ref: ${{ github.event.pull_request.head.sha }} |
| |
| - name: Set up Go |
| uses: actions/setup-go@v5 |
| with: |
| go-version: 'stable' |
| |
| # Save commit SHAs for display |
| - name: Save commit info |
| id: commits |
| run: | |
| BASE_SHA="${{ github.event.pull_request.base.sha }}" |
| HEAD_SHA="${{ github.event.pull_request.head.sha }}" |
| echo "base_short=${BASE_SHA:0:7}" >> $GITHUB_OUTPUT |
| echo "head_short=${HEAD_SHA:0:7}" >> $GITHUB_OUTPUT |
| |
| # Run benchmark on PR branch |
| - name: Run benchmark on PR branch |
| run: | |
| go test -bench '.' -benchtime=2s -benchmem ./... | tee pr-bench.txt |
| |
| # Checkout base branch and run benchmark |
| - name: Checkout base branch |
| uses: actions/checkout@v4 |
| with: |
| ref: ${{ github.event.pull_request.base.sha }} |
| clean: false |
| path: base |
| |
| - name: Run benchmark on base branch |
| working-directory: base |
| run: | |
| go test -bench '.' -benchtime=2s -benchmem ./... | \ |
| tee ../base-bench.txt |
| |
| # Install benchstat for comparison |
| - name: Install benchstat |
| run: go install golang.org/x/perf/cmd/benchstat@latest |
| |
| # Compare benchmarks using benchstat |
| - name: Compare benchmarks with benchstat |
| id: benchstat |
| run: | |
| cat > comparison.md << 'EOF' |
| ## Benchmark Comparison |
| |
| Comparing base branch (`${{ steps.commits.outputs.base_short }}`) |
| vs PR branch (`${{ steps.commits.outputs.head_short }}`) |
| |
| ``` |
| EOF |
| benchstat base-bench.txt pr-bench.txt >> comparison.md || true |
| echo '```' >> comparison.md |
| |
| # Post-process to append percentage + emoji column (🚀 faster < -10%, 🐌 slower > +10%, otherwise ➡️) |
| if [ ! -f comparison.md ]; then |
| echo "comparison.md not found after benchstat." >&2 |
| exit 1 |
| fi |
| python3 .github/scripts/benchmark_formatter.py |
| |
| # Save PR number |
| - name: Save PR number |
| run: | |
| PR_NUMBER="${{ github.event.pull_request.number }}" |
| if [ -z "$PR_NUMBER" ]; then |
| echo "Error: Pull request number is not available in event payload." >&2 |
| exit 1 |
| fi |
| echo "$PR_NUMBER" > pr_number.txt |
| |
| # Upload benchmark results |
| - name: Upload benchmark results |
| uses: actions/upload-artifact@v4 |
| with: |
| name: benchmark-results |
| path: | |
| comparison.md |
| pr_number.txt |