Skip to content

Commit 94d87a0

Browse files
BipulLamsalsylvestre
authored andcommitted
ci: fix benchmarks workflow to only run on changed crates
Detects which crates to benchmark from the PR's git diff. A change to uucore/* or uucore_procs/* benchmarks every crate that has a benches/ directory and a change scoped to src/uu/<package> benchmarks only that package.
1 parent 1a6fb19 commit 94d87a0

1 file changed

Lines changed: 48 additions & 38 deletions

File tree

.github/workflows/benchmarks.yml

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ name: Benchmarks
55

66
on:
77
pull_request:
8+
paths: &bench_paths
9+
- 'src/uu/**'
10+
- 'src/uucore/**'
11+
- 'src/uucore_procs/**'
12+
- 'Cargo.toml'
13+
- 'Cargo.lock'
814
push:
915
branches:
1016
- main
17+
paths: *bench_paths
1118

1219
permissions:
1320
contents: read # to fetch code (actions/checkout)
@@ -18,7 +25,47 @@ concurrency:
1825
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
1926

2027
jobs:
28+
detects-benchmarks:
29+
name: Detect changes on crates
30+
runs-on: ubuntu-latest
31+
outputs:
32+
package: ${{ steps.detect.outputs.package }}
33+
steps:
34+
- uses: actions/checkout@v7.0.1
35+
with:
36+
persist-credentials: false
37+
fetch-depth: 0
38+
39+
- id: detect
40+
shell: bash
41+
run: |
42+
if [ "${{ github.event_name }}" = "pull_request" ]; then
43+
base="${{ github.event.pull_request.base.sha }}"
44+
else
45+
# for push diff of the previous
46+
base="${{ github.event.before }}"
47+
fi
48+
49+
changed=$(git diff --name-only "$base" "${{ github.sha }}")
50+
51+
if echo "$changed" | grep -qE '^(Cargo\.(toml|lock)|src/(uucore|uucore_procs)/)'; then
52+
candidates=$(ls src/uu)
53+
else
54+
candidates=$(echo "$changed" | awk -F'/' '/^src\/uu\// {print $3}' | sort -u)
55+
fi
56+
57+
package=$(for pkg in $candidates;
58+
do
59+
if [ -d "src/uu/$pkg/benches" ]; then
60+
echo "uu_$pkg"
61+
fi
62+
done | sort -u | jq -R . | jq -sc .)
63+
64+
echo "package=$package" >> "$GITHUB_OUTPUT"
65+
2166
benchmarks:
67+
needs: detects-benchmarks
68+
if: needs.detects-benchmarks.outputs.package != '[]'
2269
name: Run ${{ matrix.type }} benchmarks for ${{ matrix.package }} (CodSpeed)
2370
runs-on: ubuntu-latest
2471
timeout-minutes: 90
@@ -30,44 +77,7 @@ jobs:
3077
max-parallel: 12
3178
matrix:
3279
type: [simulation, memory]
33-
package: [
34-
uu_base64,
35-
uu_cksum,
36-
uu_cp,
37-
uu_cat,
38-
uu_cut,
39-
uu_dd,
40-
uu_df,
41-
uu_du,
42-
uu_echo,
43-
uu_expand,
44-
uu_expr,
45-
uu_fold,
46-
uu_hostname,
47-
uu_join,
48-
uu_ls,
49-
uu_mv,
50-
uu_nl,
51-
uu_numfmt,
52-
uu_rm,
53-
uu_seq,
54-
uu_shuf,
55-
uu_sort,
56-
uu_split,
57-
uu_tee,
58-
uu_timeout,
59-
uu_tr,
60-
uu_tsort,
61-
uu_unexpand,
62-
uu_uniq,
63-
uu_wc,
64-
uu_factor,
65-
uu_date,
66-
uu_csplit,
67-
uu_true,
68-
uu_false,
69-
uu_ptx
70-
]
80+
package: ${{ fromJson(needs.detects-benchmarks.outputs.package) }}
7181
steps:
7282
- uses: actions/checkout@v7.0.1
7383
with:

0 commit comments

Comments
 (0)