bench #62
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: bench | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| pr_number: | |
| description: 'pull request number to benchmark' | |
| required: true | |
| type: number | |
| baseline_sha: | |
| description: 'baseline commit (defaults to latest main)' | |
| required: false | |
| type: string | |
| default: '' | |
| # cancel when a new worfkflow is dispatched | |
| concurrency: | |
| group: "${{ github.workflow }}-${{ inputs.pr_number }}" | |
| cancel-in-progress: true | |
| jobs: | |
| baseline: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| baseline_sha: ${{ steps.get-baseline.outputs.baseline }} | |
| steps: | |
| - uses: actions/setup-go@v5 | |
| with: | |
| go-version: "stable" | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: determine baseline commit | |
| id: get-baseline | |
| run: | | |
| if [ -z "${{ inputs.baseline_sha }}" ]; then | |
| git fetch origin main | |
| BASELINE=$(git rev-parse origin/main) | |
| else | |
| BASELINE="${{ inputs.baseline_sha }}" | |
| fi | |
| echo "baseline=${BASELINE::7}" >> $GITHUB_OUTPUT | |
| - name: run baseline benchmarks | |
| run: | | |
| git checkout ${{ steps.get-baseline.outputs.baseline }} | |
| make bench | tee bench-results.txt | |
| cat <<EOF >> $GITHUB_STEP_SUMMARY | |
| ### baseline benchmarks (${{ steps.get-baseline.outputs.baseline }}) | |
| \`\`\` | |
| $(cat bench-results.txt) | |
| \`\`\` | |
| EOF | |
| - name: upload baseline results | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: baseline-results | |
| path: bench-results.txt | |
| retention-days: 1 | |
| head: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| head_sha: ${{ steps.get-head.outputs.head }} | |
| steps: | |
| - uses: actions/setup-go@v5 | |
| with: | |
| go-version: "stable" | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: get pr head commit | |
| id: get-head | |
| run: | | |
| # Use GitHub API to get PR details | |
| PR_DATA=$(curl -s -H "Authorization: token ${{ github.token }}" \ | |
| "https://api.github.com/repos/${{ github.repository }}/pulls/${{ inputs.pr_number }}") | |
| HEAD_SHA=$(echo "$PR_DATA" | jq -r .head.sha) | |
| if [ "$HEAD_SHA" = "null" ]; then | |
| echo "Failed to get PR head commit" | |
| exit 1 | |
| fi | |
| echo "head=${HEAD_SHA::7}" >> $GITHUB_OUTPUT | |
| - name: run head benchmarks | |
| run: | | |
| git checkout ${{ steps.get-head.outputs.head }} | |
| make bench | tee bench-results.txt | |
| cat <<EOF >> $GITHUB_STEP_SUMMARY | |
| ### HEAD benchmarks (${{ steps.get-head.outputs.head }}) | |
| \`\`\` | |
| $(cat bench-results.txt) | |
| \`\`\` | |
| EOF | |
| - name: upload head results | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: head-results | |
| path: bench-results.txt | |
| retention-days: 1 | |
| analysis: | |
| needs: [baseline, head] | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| steps: | |
| - name: download baseline results | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: baseline-results | |
| path: ./baseline | |
| - name: download head results | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: head-results | |
| path: ./head | |
| - name: install go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: "stable" | |
| - name: compare results with benchstat | |
| run: | | |
| set -euo pipefail | |
| BASELINE="${{ needs.baseline.outputs.baseline_sha }}" | |
| HEAD="${{ needs.head.outputs.head_sha }}" | |
| COMPARE_URL="https://github.com/${{ github.repository }}/compare/$BASELINE...$HEAD" | |
| COMPARE_LINK="[$BASELINE...$HEAD]($COMPARE_URL)" | |
| SUMMARY_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
| cat <<EOF > pr_comment | |
| ### benchstats: $COMPARE_LINK | |
| View full benchmark output on the [workflow summary]($SUMMARY_URL). | |
| \`\`\` | |
| $(go run golang.org/x/perf/cmd/benchstat@latest ./baseline/bench-results.txt ./head/bench-results.txt) | |
| \`\`\` | |
| EOF | |
| cat pr_comment >> $GITHUB_STEP_SUMMARY | |
| - name: post benchmark results | |
| uses: marocchino/sticky-pull-request-comment@v2 | |
| with: | |
| header: results | |
| path: pr_comment | |
| number: ${{ inputs.pr_number }} |