Skip to content

Commit 784f676

Browse files
authored
chore(workflows): Fix benchmark workflow (#119)
1 parent 30521da commit 784f676

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

.github/workflows/bench.yml

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,65 +42,70 @@ jobs:
4242
${{ steps.gopaths.outputs.path }}/include
4343
key: ${{ runner.os }}-gobin-${{ hashfiles('Makefile') }}
4444

45-
- name: Generate code
46-
run: make gen
47-
48-
- name: Get benchmark filename for base branch
49-
id: base-benchfile
50-
if: github.event_name == 'pull_request'
45+
- name: Get benchmark filenames and cache keys
46+
id: benchfiles
5147
run: |
52-
BASE_BENCHFILE="${{ github.event.pull_request.base.ref }}"
53-
echo "name=bench-${BASE_BENCHFILE////-}.txt" >> "$GITHUB_OUTPUT"
48+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
49+
BASE_REF="${{ github.event.pull_request.base.ref }}"
50+
else
51+
BASE_REF="main"
52+
fi
53+
SAFE_BASE_REF="${BASE_REF////-}"
54+
echo "base-filename=bench-${SAFE_BASE_REF}.txt" >> "$GITHUB_OUTPUT"
55+
echo "base-cache-key=${{ runner.os }}-benchmark-${SAFE_BASE_REF}" >> "$GITHUB_OUTPUT"
56+
57+
HEAD_REF="${{ github.head_ref || github.ref_name }}"
58+
SAFE_HEAD_REF="${HEAD_REF////-}"
59+
echo "head-filename=bench-${SAFE_HEAD_REF}.txt" >> "$GITHUB_OUTPUT"
60+
echo "head-cache-key=${{ runner.os }}-benchmark-${SAFE_HEAD_REF}" >> "$GITHUB_OUTPUT"
5461
55-
- name: Get benchmark filename for head branch
56-
id: head-benchfile
57-
run: |
58-
HEAD_BENCHFILE="${{ github.head_ref || github.ref_name }}"
59-
echo "name=bench-${HEAD_BENCHFILE////-}.txt" >> "$GITHUB_OUTPUT"
62+
- name: Generate code
63+
run: make gen
6064

6165
- name: Restore PR base branch benchmarks
6266
uses: actions/cache/restore@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
63-
id: cache-base-bench
67+
id: restore-base-bench
6468
if: github.event_name == 'pull_request'
6569
with:
66-
path: ${{ steps.base-benchfile.outputs.name }}
67-
key: ${{ runner.os }}-bench-${{ steps.base-benchfile.outputs.name }}
70+
path: ${{ steps.benchfiles.outputs.base-filename }}
71+
key: ${{ steps.benchfiles.outputs.base-cache-key }}
6872

6973
- name: Run benchmarks
70-
env:
71-
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
7274
run: |
73-
make bench > ${{ steps.head-benchfile.outputs.name }}
74-
cat ${{ steps.head-benchfile.outputs.name }}
75+
make bench > ${{ steps.benchfiles.outputs.head-filename }}
76+
cat ${{ steps.benchfiles.outputs.head-filename }}
7577
7678
- name: Delete old benchmark caches
7779
if: github.event_name != 'pull_request'
7880
continue-on-error: true
7981
run: |
80-
gh cache delete ${{ runner.os }}-bench-main.txt
82+
gh cache delete "${{ steps.benchfiles.outputs.head-cache-key }}"
8183
env:
8284
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8385

8486
- name: Cache benchmark results
8587
uses: actions/cache/save@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
8688
if: github.event_name != 'pull_request'
8789
with:
88-
path: ${{ steps.head-benchfile.outputs.name }}
89-
key: ${{ runner.os }}-bench-${{ steps.head-benchfile.outputs.name }}
90+
path: ${{ steps.benchfiles.outputs.head-filename }}
91+
key: ${{ steps.benchfiles.outputs.head-cache-key }}
9092

9193
- name: Compare benchmarks
9294
id: bench-diff
9395
if: github.event_name == 'pull_request' && steps.cache-base-bench.outputs.cache-hit == 'true'
9496
run: |
95-
go run golang.org/x/perf/cmd/benchstat@latest ${{ steps.base-benchfile.outputs.name }} ${{ steps.head-benchfile.outputs.name }} > bench-diff.txt
97+
go run golang.org/x/perf/cmd/benchstat@latest \
98+
${{ steps.benchfiles.outputs.base-filename }} \
99+
${{ steps.benchfiles.outputs.head-filename }} > bench-diff.txt
96100
cat bench-diff.txt
97101
echo 'summary<<EOF' >> $GITHUB_OUTPUT
98102
cat bench-diff.txt >> $GITHUB_OUTPUT
99103
echo 'EOF' >> $GITHUB_OUTPUT
100104
101105
- name: Write results to markdown file
102106
env:
103-
FILENAME: ${{ steps.head-benchfile.outputs.name }}
107+
FILENAME: ${{ steps.benchfiles.outputs.head-filename }}
108+
BENCH_DIFF: ${{ steps.bench-diff.outputs.summary }}
104109
run: |
105110
{
106111
echo "### Benchmark Results"
@@ -111,21 +116,17 @@ jobs:
111116
cat "$FILENAME"
112117
echo '```'
113118
echo "</details>"
114-
} > bench-comment.md
115119
116-
- name: Write benchmark diff to markdown file
117-
if: github.event_name == 'pull_request' && steps.cache-base-bench.outputs.cache-hit == 'true'
118-
run: |
119-
BENCH_DIFF="${{ steps.bench-diff.outputs.summary }}"
120-
cat <<EOF >> bench-comment.md
121-
122-
<details><summary>Benchmark vs base branch</summary>
123-
124-
\`\`\`
125-
$BENCH_DIFF
126-
\`\`\`
127-
</details>
128-
EOF
120+
if [ -n "$BENCH_DIFF" ]; then
121+
echo ""
122+
echo "<details open><summary>Benchmark vs base branch</summary>"
123+
echo ""
124+
echo '```'
125+
echo "$BENCH_DIFF"
126+
echo '```'
127+
echo "</details>"
128+
fi
129+
} > bench-comment.md
129130
130131
- name: Upload benchmark results
131132
if: github.event_name == 'pull_request'

0 commit comments

Comments
 (0)