Skip to content

Commit c4494e7

Browse files
authored
Update GitHub Actions templates to cache modules and restore base code coverage and benchmark results (#16)
1 parent 4842a64 commit c4494e7

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

templates/github/workflows/bench.yml

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
name: bench
22
on:
3-
push:
4-
tags:
5-
- v*
6-
branches:
7-
- master
8-
- main
93
pull_request:
4+
workflow_dispatch:
5+
inputs:
6+
old:
7+
description: 'Old Ref'
8+
required: false
9+
default: 'master'
10+
new:
11+
description: 'New Ref'
12+
required: true
13+
1014
env:
1115
GO111MODULE: "on"
16+
CACHE_BENCHMARK: "off" # Enables benchmark result reuse between runs, may skew latency results.
17+
RUN_BASE_BENCHMARK: "on" # Runs benchmark for PR base in case benchmark result is missing.
1218
jobs:
1319
bench:
1420
strategy:
@@ -22,32 +28,52 @@ jobs:
2228
go-version: ${{ matrix.go-version }}
2329
- name: Checkout code
2430
uses: actions/checkout@v2
25-
- name: Restore vendor
31+
with:
32+
ref: ${{ (github.event.inputs.new != '') && github.event.inputs.new || github.event.ref }}
33+
- name: Go cache
2634
uses: actions/cache@v2
2735
with:
36+
# In order:
37+
# * Module download cache
38+
# * Build cache (Linux)
2839
path: |
29-
vendor
30-
key: ${{ runner.os }}-go${{ matrix.go-version }}-vendor-${{ hashFiles('**/go.mod') }}
31-
- name: Populate dependencies
32-
run: |
33-
(test -d vendor && echo vendor found) || (go mod vendor && du -sh vendor && du -sh ~/go/pkg/mod)
40+
~/go/pkg/mod
41+
~/.cache/go-build
42+
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
43+
restore-keys: |
44+
${{ runner.os }}-go-cache
3445
- name: Restore benchstat
3546
uses: actions/cache@v2
3647
with:
3748
path: ~/go/bin/benchstat
3849
key: ${{ runner.os }}-benchstat
3950
- name: Restore base benchmark result
51+
if: env.CACHE_BENCHMARK == 'on'
52+
id: benchmark-base
4053
uses: actions/cache@v2
4154
with:
4255
path: |
4356
bench-master.txt
4457
bench-main.txt
4558
# Use base sha for PR or new commit hash for master/main push in benchmark result key.
4659
key: ${{ runner.os }}-bench-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
60+
- name: Checkout base code
61+
if: env.RUN_BASE_BENCHMARK == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && (github.event.pull_request.base.sha != '' || github.event.inputs.old != '')
62+
uses: actions/checkout@v2
63+
with:
64+
ref: ${{ (github.event.pull_request.base.sha != '' ) && github.event.pull_request.base.sha || github.event.inputs.old }}
65+
path: __base
66+
- name: Run base benchmark
67+
if: env.RUN_BASE_BENCHMARK == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && (github.event.pull_request.base.sha != '' || github.event.inputs.old != '')
68+
run: |
69+
export REF_NAME=master
70+
cd __base
71+
BENCH_COUNT=5 make bench-run bench-stat
72+
cp bench-master.txt ../bench-master.txt
4773
- name: Benchmark
4874
id: bench
4975
run: |
50-
export REF_NAME=${GITHUB_REF##*/}
76+
export REF_NAME=new
5177
BENCH_COUNT=5 make bench-run bench-stat
5278
OUTPUT=$(make bench-stat)
5379
OUTPUT="${OUTPUT//'%'/'%25'}"

templates/github/workflows/test-unit.yml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
pull_request:
88
env:
99
GO111MODULE: "on"
10+
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
1011
jobs:
1112
test:
1213
strategy:
@@ -20,22 +21,37 @@ jobs:
2021
go-version: ${{ matrix.go-version }}
2122
- name: Checkout code
2223
uses: actions/checkout@v2
24+
- name: Go cache
25+
uses: actions/cache@v2
26+
with:
27+
# In order:
28+
# * Module download cache
29+
# * Build cache (Linux)
30+
path: |
31+
~/go/pkg/mod
32+
~/.cache/go-build
33+
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
34+
restore-keys: |
35+
${{ runner.os }}-go-cache
2336
- name: Restore base test coverage
2437
uses: actions/cache@v2
2538
with:
2639
path: |
2740
unit-base.txt
2841
# Use base sha for PR or new commit hash for master/main push in test result key.
2942
key: ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
30-
- name: Restore vendor
31-
uses: actions/cache@v2
43+
- name: Checkout base code
44+
if: env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
45+
uses: actions/checkout@v2
3246
with:
33-
path: |
34-
vendor
35-
key: ${{ runner.os }}-go${{ matrix.go-version }}-vendor-${{ hashFiles('**/go.mod') }}
36-
- name: Populate dependencies
47+
ref: ${{ github.event.pull_request.base.sha }}
48+
path: __base
49+
- name: Run test for base code
50+
if: env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
3751
run: |
38-
(test -d vendor && echo vendor found) || (go mod vendor && du -sh vendor && du -sh ~/go/pkg/mod)
52+
cd __base
53+
make test-unit
54+
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt
3955
- name: Test
4056
id: test
4157
run: |

0 commit comments

Comments
 (0)