chore(workflows): Fix benchmark workflow (#119) #189
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
| # Workflow to run benchmarking. | |
| # * Runs on new semantic tags, pushes to main, and pull requests to main. | |
| name: Benchmark | |
| on: | |
| push: | |
| tags: ['v*'] | |
| branches: ['main'] | |
| paths-ignore: ['README.md', 'examples/**', '.github/workflows/test.yml', '.github/workflows/build.yml'] | |
| pull_request: | |
| branches: ['main'] | |
| paths-ignore: ['README.md', 'examples/**', '.github/workflows/test.yml', '.github/workflows/build.yml'] | |
| env: | |
| PROTOC_VERSION: '3.15.8' | |
| jobs: | |
| bench: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
| - name: Set up Go | |
| uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 | |
| with: | |
| go-version-file: go.mod | |
| cache: true | |
| cache-dependency-path: go.sum | |
| - name: Get go paths | |
| id: gopaths | |
| run: echo "path=$(go env GOPATH)" >> "$GITHUB_OUTPUT" | |
| - name: Restore gobin cache | |
| uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 | |
| with: | |
| path: | | |
| ${{ steps.gopaths.outputs.path }}/bin | |
| ${{ steps.gopaths.outputs.path }}/include | |
| key: ${{ runner.os }}-gobin-${{ hashfiles('Makefile') }} | |
| - name: Get benchmark filenames and cache keys | |
| id: benchfiles | |
| run: | | |
| if [[ "${{ github.event_name }}" == "pull_request" ]]; then | |
| BASE_REF="${{ github.event.pull_request.base.ref }}" | |
| else | |
| BASE_REF="main" | |
| fi | |
| SAFE_BASE_REF="${BASE_REF////-}" | |
| echo "base-filename=bench-${SAFE_BASE_REF}.txt" >> "$GITHUB_OUTPUT" | |
| echo "base-cache-key=${{ runner.os }}-benchmark-${SAFE_BASE_REF}" >> "$GITHUB_OUTPUT" | |
| HEAD_REF="${{ github.head_ref || github.ref_name }}" | |
| SAFE_HEAD_REF="${HEAD_REF////-}" | |
| echo "head-filename=bench-${SAFE_HEAD_REF}.txt" >> "$GITHUB_OUTPUT" | |
| echo "head-cache-key=${{ runner.os }}-benchmark-${SAFE_HEAD_REF}" >> "$GITHUB_OUTPUT" | |
| - name: Generate code | |
| run: make gen | |
| - name: Restore PR base branch benchmarks | |
| uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 | |
| id: restore-base-bench | |
| if: github.event_name == 'pull_request' | |
| with: | |
| path: ${{ steps.benchfiles.outputs.base-filename }} | |
| key: ${{ steps.benchfiles.outputs.base-cache-key }} | |
| - name: Run benchmarks | |
| run: | | |
| make bench > ${{ steps.benchfiles.outputs.head-filename }} | |
| cat ${{ steps.benchfiles.outputs.head-filename }} | |
| - name: Delete old benchmark caches | |
| if: github.event_name != 'pull_request' | |
| continue-on-error: true | |
| run: | | |
| gh cache delete "${{ steps.benchfiles.outputs.head-cache-key }}" | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Cache benchmark results | |
| uses: actions/cache/save@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 | |
| if: github.event_name != 'pull_request' | |
| with: | |
| path: ${{ steps.benchfiles.outputs.head-filename }} | |
| key: ${{ steps.benchfiles.outputs.head-cache-key }} | |
| - name: Compare benchmarks | |
| id: bench-diff | |
| if: github.event_name == 'pull_request' && steps.cache-base-bench.outputs.cache-hit == 'true' | |
| run: | | |
| go run golang.org/x/perf/cmd/benchstat@latest \ | |
| ${{ steps.benchfiles.outputs.base-filename }} \ | |
| ${{ steps.benchfiles.outputs.head-filename }} > bench-diff.txt | |
| cat bench-diff.txt | |
| echo 'summary<<EOF' >> $GITHUB_OUTPUT | |
| cat bench-diff.txt >> $GITHUB_OUTPUT | |
| echo 'EOF' >> $GITHUB_OUTPUT | |
| - name: Write results to markdown file | |
| env: | |
| FILENAME: ${{ steps.benchfiles.outputs.head-filename }} | |
| BENCH_DIFF: ${{ steps.bench-diff.outputs.summary }} | |
| run: | | |
| { | |
| echo "### Benchmark Results" | |
| echo "" | |
| echo "<details><summary>Benchmark results</summary>" | |
| echo "" | |
| echo '```' | |
| cat "$FILENAME" | |
| echo '```' | |
| echo "</details>" | |
| if [ -n "$BENCH_DIFF" ]; then | |
| echo "" | |
| echo "<details open><summary>Benchmark vs base branch</summary>" | |
| echo "" | |
| echo '```' | |
| echo "$BENCH_DIFF" | |
| echo '```' | |
| echo "</details>" | |
| fi | |
| } > bench-comment.md | |
| - name: Upload benchmark results | |
| if: github.event_name == 'pull_request' | |
| uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 | |
| with: | |
| name: bench-comment-${{ github.event.number }} | |
| path: bench-comment.md | |
| if-no-files-found: error | |
| retention-days: 1 | |
| overwrite: true | |
| - name: Save gobin cache | |
| uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 | |
| with: | |
| path: | | |
| ${{ steps.gopaths.outputs.path }}/bin | |
| ${{ steps.gopaths.outputs.path }}/include | |
| key: ${{ runner.os }}-gobin-${{ hashfiles('Makefile') }} |