| 1 |
name: Benchmarks |
| 2 |
|
| 3 |
on: |
| 4 |
workflow_dispatch: |
| 5 |
inputs: |
| 6 |
pr_number: |
| 7 |
description: 'Pull Request number' |
| 8 |
required: true |
| 9 |
type: number |
| 10 |
|
| 11 |
permissions: {} |
| 12 |
|
| 13 |
env: |
| 14 |
BENCH_COUNT: 6 |
| 15 |
BENCH_FILTER: 'BenchmarkCompare/(mem|os)fs' |
| 16 |
BENCH_PATH: ./test/... |
| 17 |
|
| 18 |
jobs: |
| 19 |
setup: |
| 20 |
name: Get PR Info |
| 21 |
runs-on: ubuntu-latest |
| 22 |
permissions: |
| 23 |
contents: read |
| 24 |
outputs: |
| 25 |
base_ref: ${{ steps.pr-info.outputs.base_ref }} |
| 26 |
steps: |
| 27 |
- name: Get PR base ref |
| 28 |
id: pr-info |
| 29 |
env: |
| 30 |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 31 |
run: | |
| 32 |
base_ref=$(gh pr view ${{ inputs.pr_number }} --repo ${{ github.repository }} --json baseRefName --jq .baseRefName) |
| 33 |
echo "base_ref=$base_ref" >> $GITHUB_OUTPUT |
| 34 |
|
| 35 |
bench-base: |
| 36 |
name: Benchmark base branch |
| 37 |
runs-on: ubuntu-latest |
| 38 |
needs: setup |
| 39 |
permissions: {} |
| 40 |
outputs: |
| 41 |
outcome: ${{ steps.bench.outcome }} |
| 42 |
steps: |
| 43 |
- name: Checkout base branch |
| 44 |
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd |
| 45 |
with: |
| 46 |
ref: ${{ needs.setup.outputs.base_ref }} |
| 47 |
persist-credentials: false |
| 48 |
|
| 49 |
- name: Set up Go |
| 50 |
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 |
| 51 |
with: |
| 52 |
go-version: stable |
| 53 |
cache-dependency-path: go.sum |
| 54 |
|
| 55 |
- name: Run benchmarks |
| 56 |
id: bench |
| 57 |
run: go test -run='^$' -bench="$BENCH_FILTER" -benchmem -count=$BENCH_COUNT $BENCH_PATH > base.txt 2>&1 |
| 58 |
continue-on-error: true |
| 59 |
|
| 60 |
- name: Upload results |
| 61 |
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f |
| 62 |
with: |
| 63 |
name: base-bench |
| 64 |
path: base.txt |
| 65 |
|
| 66 |
bench-pr: |
| 67 |
name: Benchmark PR branch |
| 68 |
runs-on: ubuntu-latest |
| 69 |
needs: setup |
| 70 |
permissions: {} |
| 71 |
outputs: |
| 72 |
outcome: ${{ steps.bench.outcome }} |
| 73 |
steps: |
| 74 |
- name: Checkout PR branch |
| 75 |
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd |
| 76 |
with: |
| 77 |
ref: refs/pull/${{ inputs.pr_number }}/head |
| 78 |
persist-credentials: false |
| 79 |
|
| 80 |
- name: Set up Go |
| 81 |
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 |
| 82 |
with: |
| 83 |
go-version: stable |
| 84 |
cache-dependency-path: go.sum |
| 85 |
|
| 86 |
- name: Run benchmarks |
| 87 |
id: bench |
| 88 |
run: go test -run='^$' -bench="$BENCH_FILTER" -benchmem -count=$BENCH_COUNT $BENCH_PATH > pr.txt 2>&1 |
| 89 |
continue-on-error: true |
| 90 |
|
| 91 |
- name: Upload results |
| 92 |
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f |
| 93 |
with: |
| 94 |
name: pr-bench |
| 95 |
path: pr.txt |
| 96 |
|
| 97 |
compare: |
| 98 |
name: Compare and report |
| 99 |
runs-on: ubuntu-latest |
| 100 |
needs: [bench-base, bench-pr] |
| 101 |
if: always() |
| 102 |
permissions: |
| 103 |
pull-requests: write |
| 104 |
steps: |
| 105 |
- name: Set up Go |
| 106 |
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 |
| 107 |
with: |
| 108 |
go-version: stable |
| 109 |
|
| 110 |
- name: Install benchstat |
| 111 |
|
| 112 |
run: go install golang.org/x/perf/cmd/[email protected] |
| 113 |
|
| 114 |
- name: Download base results |
| 115 |
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 |
| 116 |
with: |
| 117 |
name: base-bench |
| 118 |
|
| 119 |
- name: Download PR results |
| 120 |
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 |
| 121 |
with: |
| 122 |
name: pr-bench |
| 123 |
|
| 124 |
- name: Compare and report |
| 125 |
env: |
| 126 |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 127 |
PR_NUMBER: ${{ inputs.pr_number }} |
| 128 |
REPO: ${{ github.repository }} |
| 129 |
BASE_OUTCOME: ${{ needs.bench-base.outputs.outcome }} |
| 130 |
PR_OUTCOME: ${{ needs.bench-pr.outputs.outcome }} |
| 131 |
run: | |
| 132 |
if [ "$BASE_OUTCOME" != "success" ] || [ "$PR_OUTCOME" != "success" ]; then |
| 133 |
{ |
| 134 |
echo "## ⚠️ Benchmark Run Failed" |
| 135 |
echo "" |
| 136 |
echo "One or more benchmark runs did not complete successfully." |
| 137 |
echo "Please check the workflow logs for details." |
| 138 |
echo "" |
| 139 |
echo "| Step | Outcome |" |
| 140 |
echo "| --- | --- |" |
| 141 |
echo "| Base branch benchmarks | \`${BASE_OUTCOME}\` |" |
| 142 |
echo "| PR branch benchmarks | \`${PR_OUTCOME}\` |" |
| 143 |
} > comment.md |
| 144 |
else |
| 145 |
benchstat base.txt pr.txt > stat.txt 2>&1 |
| 146 |
|
| 147 |
# Extract lines with regressions greater than 5% on any metric. |
| 148 |
# Also capture infinite regressions (+∞ / +Inf) that appear when the |
| 149 |
# baseline measurement was zero. |
| 150 |
awk ' |
| 151 |
/\+∞/ || /\+Inf/ { print; next } |
| 152 |
match($0, /\+[0-9]+\.?[0-9]*%/) { |
| 153 |
pct = substr($0, RSTART + 1, RLENGTH - 2) |
| 154 |
if (pct + 0 > 5) print |
| 155 |
} |
| 156 |
' stat.txt > regressions.txt |
| 157 |
|
| 158 |
if [ -s regressions.txt ]; then |
| 159 |
{ |
| 160 |
echo "## ⚠️ Benchmark Regressions Detected (>5%)" |
| 161 |
echo "" |
| 162 |
echo "The following benchmarks have degraded by more than 5% in time, memory, or allocations:" |
| 163 |
echo "" |
| 164 |
echo "\`\`\`" |
| 165 |
cat regressions.txt |
| 166 |
echo "\`\`\`" |
| 167 |
echo "" |
| 168 |
echo "<details>" |
| 169 |
echo "<summary>Full benchmark comparison (<code>benchstat</code>)</summary>" |
| 170 |
echo "" |
| 171 |
echo "\`\`\`" |
| 172 |
cat stat.txt |
| 173 |
echo "\`\`\`" |
| 174 |
echo "</details>" |
| 175 |
} > comment.md |
| 176 |
else |
| 177 |
{ |
| 178 |
echo "## ✅ No Benchmark Regressions" |
| 179 |
echo "" |
| 180 |
echo "No benchmark regressions greater than 5% were detected." |
| 181 |
echo "" |
| 182 |
echo "<details>" |
| 183 |
echo "<summary>Full benchmark comparison (<code>benchstat</code>)</summary>" |
| 184 |
echo "" |
| 185 |
echo "\`\`\`" |
| 186 |
cat stat.txt |
| 187 |
echo "\`\`\`" |
| 188 |
echo "</details>" |
| 189 |
} > comment.md |
| 190 |
fi |
| 191 |
fi |
| 192 |
|
| 193 |
gh pr comment "$PR_NUMBER" --edit-last --create-if-none --body-file comment.md --repo "$REPO" |