11name : Benchmark
22
33on :
4+ workflow_dispatch :
45 push :
56 branches :
67 - " master"
78 - " main"
89 paths :
9- - " **.go"
10+ - " **/* .go"
1011 - " **/go.mod"
1112 - " .github/workflows/benchmark.yml"
1213 pull_request :
13- branches :
14- - " *"
1514 paths :
16- - " **.go"
15+ - " **/* .go"
1716 - " **/go.mod"
1817 - " .github/workflows/benchmark.yml"
19- workflow_dispatch :
2018
2119permissions :
2220 deployments : write
2321 contents : write
22+ pull-requests : write
2423
2524jobs :
26- Compare :
25+ compare :
2726 runs-on : ubuntu-latest
2827 steps :
2928 - name : Fetch Repository
3029 uses : actions/checkout@v6
30+ with :
31+ fetch-depth : 0
3132
3233 - name : Install Go
3334 uses : actions/setup-go@v6
@@ -37,37 +38,104 @@ jobs:
3738 cache : false
3839
3940 - name : Run Benchmarks
41+ shell : bash
4042 run : |
41- set -o pipefail
43+ set -euo pipefail
44+ : > output.txt
4245 for d in */ ; do
43- cd "$d"
44- go test ./... -benchmem -run=^$ -bench . | tee -a ../output.txt
45- cd ..
46+ if [[ ! -f "${d}go.mod" ]]; then
47+ continue
48+ fi
49+ (
50+ cd "$d"
51+ go test ./... -benchmem -run=^$ -bench .
52+ ) | tee -a output.txt
4653 done
54+
55+ - name : Get GitHub Runner System Information
56+ uses : kenchan0130/actions-system-info@v1.4.0
57+ id : system-info
58+
59+ - name : Get Default branch SHA
60+ id : get-default-branch-sha
61+ env :
62+ DEFAULT_BRANCH : ${{ github.event.repository.default_branch }}
4763 shell : bash
64+ run : |
65+ set -euo pipefail
66+ SHA="$(git ls-remote origin "${DEFAULT_BRANCH}" | awk 'NR==1 {print $1}')"
67+ if [[ -z "${SHA}" ]]; then
68+ echo "Failed to resolve SHA for default branch '${DEFAULT_BRANCH}'" >&2
69+ exit 1
70+ fi
71+ echo "sha=${SHA}" >> "$GITHUB_OUTPUT"
72+
73+ - name : Build normalized cache key
74+ id : cache-key
75+ env :
76+ CPU_MODEL : ${{ steps.system-info.outputs.cpu-model }}
77+ BASE_SHA : ${{ steps.get-default-branch-sha.outputs.sha }}
78+ shell : bash
79+ run : |
80+ set -euo pipefail
81+ SAFE_CPU_MODEL="$(printf '%s' "$CPU_MODEL" | tr -cs '[:alnum:]._-' '-')"
82+ SAFE_CPU_MODEL="${SAFE_CPU_MODEL#-}"
83+ SAFE_CPU_MODEL="${SAFE_CPU_MODEL%-}"
84+ echo "key=${BASE_SHA}-${{ runner.os }}-${SAFE_CPU_MODEL}-benchmark" >> "$GITHUB_OUTPUT"
4885
49- - name : Get Previous Benchmark Results
50- uses : actions/cache@v5
86+ - name : Get Benchmark Results from default branch
87+ id : cache
88+ uses : actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
5189 with :
5290 path : ./cache
53- key : ${{ runner.os }}-benchmark
91+ key : ${{ steps.cache-key.outputs.key }}
5492
55- - name : Save Benchmark Results
93+ - name : Compare PR Benchmark Results with default branch
94+ if : ${{ github.event_name == 'pull_request' && steps.cache.outputs.cache-hit == 'true' }}
5695 uses : benchmark-action/github-action-benchmark@v1.20.7
5796 with :
58- # What benchmark tool the output.txt came from
59- tool : ' go'
60- # Where the output from the benchmark tool is stored
97+ tool : " go"
6198 output-file-path : output.txt
62- # Where the previous data file is stored
6399 external-data-json-path : ./cache/benchmark-data.json
64- # Secret for Github
65- github-token : ${{ secrets.BENCHMARK_TOKEN }}
66- # Directory that contains benchmark files on the GitHub pages branch
67- benchmark-data-dir-path : " benchmarks"
68- # Workflow will fail when an alert happens
100+ save-data-file : false
69101 fail-on-alert : true
70- comment-on-alert : ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
71- # summary-always: ${{ github.event_name != 'push' && github.event_name != 'workflow_dispatch' }}
102+ comment-on-alert : ${{ github.event.pull_request.head.repo.fork == false }}
103+ github-token : ${{ secrets.GITHUB_TOKEN }}
104+ summary-always : true
105+ alert-threshold : " 150%"
106+
107+ - name : Store Benchmark Results for default branch
108+ if : ${{ github.ref_name == github.event.repository.default_branch }}
109+ uses : benchmark-action/github-action-benchmark@v1.20.7
110+ with :
111+ tool : " go"
112+ output-file-path : output.txt
113+ external-data-json-path : ./cache/benchmark-data.json
114+ save-data-file : true
115+ fail-on-alert : false
116+ github-token : ${{ secrets.GITHUB_TOKEN }}
117+ summary-always : true
118+ alert-threshold : " 150%"
119+ auto-push : false
120+
121+ - name : Publish Benchmark Results to GitHub Pages
122+ if : ${{ github.ref_name == github.event.repository.default_branch }}
123+ uses : benchmark-action/github-action-benchmark@v1.20.7
124+ with :
125+ tool : " go"
126+ output-file-path : output.txt
127+ benchmark-data-dir-path : " benchmarks"
128+ fail-on-alert : false
129+ github-token : ${{ secrets.GITHUB_TOKEN }}
130+ comment-on-alert : false
131+ summary-always : true
132+ save-data-file : true
133+ alert-threshold : " 150%"
72134 auto-push : ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
73- save-data-file : ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
135+
136+ - name : Update Benchmark Results cache
137+ if : ${{ github.ref_name == github.event.repository.default_branch }}
138+ uses : actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
139+ with :
140+ path : ./cache
141+ key : ${{ steps.cache-key.outputs.key }}
0 commit comments