Repository navigation
269 lines (255 loc) · 13.6 KB
/
Copy pathperf.yml
File metadata and controls
269 lines (255 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
name: Perf
# Performance regression guards: Duel runs the benchmarks in duel/ on the
# baseline and on this code side by side, and fails if this code is slower.
# See .duel.rb and https://github.com/sferik/duel-ruby
# The Duel job runs this branch's code, so it only reads. Marking a push
# passed is a job of its own, which runs none of it, and commenting on a
# pull request is the Perf comment workflow's.
permissions:
contents: read
pull-requests: read # for the pull request's labels (see c.accept_label)
on:
pull_request:
merge_group: # (a merge queue's)
push:
branches:
- 'main' # (and any other branch to guard, a stable one, say)
schedule:
- cron: "0 4 * * *"
# Allow manually triggering the workflow.
workflow_dispatch:
# A pull request's newer push cancels its run, if that's still going, as
# only its newest commit's result counts. Every other run (a push's, say)
# runs to the end, so that each push is compared.
concurrency:
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.event.pull_request.number || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
duel:
if: "github.event_name != 'push' || (!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]'))"
name: Duel
runs-on: ubuntu-latest
# Whether to comment (post, change or none), the run's status (passed,
# failed or neutral), and whether to mark a push passed (true, or
# nothing), from Duel.
outputs:
comment: ${{ steps.compare.outputs.comment }}
status: ${{ steps.compare.outputs.status }}
passed: ${{ steps.compare.outputs.passed }}
env: # $BUNDLE_GEMFILE must be set at job level, so it is set for all steps
# The perf appraisal: the gemspec's dependencies and Duel (Ruby 3.4 or later)
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/perf.gemfile
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0 # the baseline comes from history, tags included
persist-credentials: false
# The labels of the pull request (or for a push, of the one that
# merged it), for c.accept_label in .duel.rb, as a JSON array. They're
# read as the job runs, and adding the label (or taking it off) runs
# the job again: see the Perf comment workflow.
# On a schedule, the last commit since the last release that merged a
# pull request with the label (ACCEPT_LABEL, as .duel.rb names it):
# what that slowed down was accepted, so it's compared with instead.
- name: Find the pull request's labels
id: labels
if: github.event_name == 'pull_request' || github.event_name == 'merge_group' || github.event_name == 'push' || github.event_name == 'schedule'
continue-on-error: true # (without them, nothing's accepted)
env:
GH_TOKEN: ${{ github.token }}
PR: ${{ github.event.pull_request.number }}
QUEUED: ${{ github.event.merge_group.head_ref }}
ACCEPT_LABEL: 'duel: accept'
RELEASE_TAGS: "v*"
run: |
if [ "$GITHUB_EVENT_NAME" = schedule ]; then
tag=$(git describe --tags --abbrev=0 --match "$RELEASE_TAGS" HEAD^ 2>/dev/null) || exit 0
since=$(TZ=UTC git log -1 --date=format-local:%Y-%m-%dT%H:%M:%SZ --format=%cd "$tag")
merged=$(gh pr list --repo "$GITHUB_REPOSITORY" --state merged --label "$ACCEPT_LABEL" --search "merged:>=$since" --limit 1000 \
--json mergeCommit --jq '.[].mergeCommit.oid')
accepted=$(git rev-list "$tag..HEAD" | grep -Fx -m 1 -f <(echo "$merged")) || accepted=
echo "accepted=$accepted" >> "$GITHUB_OUTPUT"
exit 0
fi
# (In a merge queue, the last pull request queued, which the queue's
# branch names: gh-readonly-queue/main/pr-123-<sha>.)
if [ -n "$QUEUED" ]; then
PR=${QUEUED##*/pr-}
PR=${PR%%-*}
fi
if [ -n "$PR" ]; then
labels=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR/labels" --jq '[.[].name]')
else
labels=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/pulls" --jq '[.[0].labels[]?.name]')
fi
echo "labels=$labels" >> "$GITHUB_OUTPUT"
# .duel.rb sets c.ruby = :version_file, so each version runs on the Ruby
# that its own version file names (.ruby-version, .tool-versions, or
# mise.toml, the first that names one, as Duel reads them). When the
# baseline's names another Ruby than this one's, install that Ruby too,
# and link it where Duel finds it. The baseline is the one the Compare
# step uses: the branch it targets for a pull request, a merge queue's
# base, the commit before a push (or the last commit Duel passed, if
# there is one: see the Mark the commit passed job), the last release
# on a schedule (the newest v* tag before HEAD, as Duel finds it, or
# ACCEPTED, the last change accepted since then), and where HEAD left
# the default branch when run by hand.
- name: Find the baseline's Ruby
id: baseline
env:
BEFORE: ${{ github.event.before }}
QUEUE_BASE: ${{ github.event.merge_group.base_sha }}
PASSED: origin/duel/passed/${{ github.ref_name }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
ACCEPTED: ${{ steps.labels.outputs.accepted }}
run: |
case "$GITHUB_EVENT_NAME" in
pull_request) base="origin/$GITHUB_BASE_REF" ;;
push)
base=$BEFORE
if git merge-base --is-ancestor "$PASSED" HEAD 2>/dev/null; then base=$PASSED; fi ;;
merge_group) base=$QUEUE_BASE ;;
schedule) base=${ACCEPTED:-$(git describe --tags --abbrev=0 --match 'v*' HEAD^ 2>/dev/null)} || base= ;;
*) base=$(git merge-base HEAD "origin/$DEFAULT_BRANCH" 2>/dev/null) || base= ;;
esac
ruby_of() {
for file in .ruby-version .tool-versions mise.toml .mise.toml; do
text=$(git show "$1:$file" 2>/dev/null) || continue
case "$file" in
.ruby-version) version=$(printf '%s\n' "$text" | awk 'NF { print $1; exit }') ;;
.tool-versions) version=$(printf '%s\n' "$text" | awk '$1 == "ruby" { print $2; exit }') ;;
*) version=$(printf '%s\n' "$text" | awk '/^\[/ { tools = ($0 ~ /^\[tools\]/) } tools && match($0, /^[ \t]*"?ruby"?[ \t]*=[ \t]*[[{]?[ \t]*(version[ \t]*=[ \t]*)?"[^"]+"/) { s = substr($0, RSTART, RLENGTH); sub(/"$/, "", s); sub(/.*"/, "", s); print s; exit }') ;;
esac
if [ -n "$version" ]; then
echo "$version"
return
fi
done
}
ruby=
if [ -n "$base" ] && git cat-file -e "$base^{commit}" 2>/dev/null; then
ruby=$(ruby_of "$base")
[ "$ruby" = "$(ruby_of HEAD)" ] && ruby=
fi
echo "ruby=$ruby" >> "$GITHUB_OUTPUT"
- name: Setup the baseline's Ruby
id: baseline-ruby
if: steps.baseline.outputs.ruby != ''
uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0
with:
ruby-version: ${{ steps.baseline.outputs.ruby }}
# The bundle for this Ruby too, which the baseline's workers load.
# (Gems installed for one Ruby version don't load on another.)
bundler-cache: true
- name: Link the baseline's Ruby where Duel finds it
if: steps.baseline.outputs.ruby != ''
env:
PREFIX: ${{ steps.baseline-ruby.outputs.ruby-prefix }}
run: |
mkdir -p ~/.rubies
ln -sfn "$PREFIX" ~/.rubies/ruby-"$("$PREFIX/bin/ruby" -e 'print RUBY_VERSION')"
# The candidate's Ruby, from its version file (the same one Duel reads)
- name: Setup Ruby & RubyGems
uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0
with:
bundler-cache: true
# Against the branch it targets for a pull request, the queue's base
# for a merge queue, what was there before for a push (or as
# c.push_base in .duel.rb says, the last commit Duel passed), and the
# last release on a schedule (slow creep). Duel adds its report to
# the job's summary, writes it to duel.md for the comment, and sets
# the step's outputs to say whether to comment, as c.comment in
# .duel.rb says, whatever the outcome.
- name: Compare
id: compare
env:
DUEL_BASE: ${{ github.event_name == 'schedule' && 'last-release' || github.event_name == 'push' && github.event.before || github.event_name == 'merge_group' && github.event.merge_group.base_sha || '' }}
# (Run by hand, it compares anyway: with nothing changed, the code
# with itself, which checks the setup.)
DUEL_FORCE: ${{ github.event_name == 'workflow_dispatch' && '1' || '' }}
DUEL_LABELS: ${{ steps.labels.outputs.labels }}
DUEL_ACCEPTED: ${{ steps.labels.outputs.accepted }}
run: bundle exec duel --markdown "$RUNNER_TEMP/duel/duel.md"
# For a pull request, what Duel said, for the Perf comment workflow,
# which can't see this job's outputs.
- name: Say whether to comment
if: ${{ !cancelled() && github.event_name == 'pull_request' }}
env:
PR: ${{ github.event.pull_request.number }}
COMMENT: ${{ steps.compare.outputs.comment }}
STATUS: ${{ steps.compare.outputs.status }}
run: |
mkdir -p "$RUNNER_TEMP/duel"
printf 'pr=%s\ncomment=%s\nstatus=%s\n' "$PR" "$COMMENT" "$STATUS" > "$RUNNER_TEMP/duel/comment.env"
# The report, for the comments
- name: Keep the report
if: ${{ !cancelled() && (github.event_name == 'pull_request' || github.event_name == 'push') }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: duel-report
path: ${{ runner.temp }}/duel/
if-no-files-found: ignore # none when Duel didn't get to compare
retention-days: 1
# With c.push_base = :last_passed in .duel.rb, a push is compared with
# the last commit Duel passed, which a branch marks (each branch pushed
# has its own), so slowdowns too small for any one push to fail add up
# until they fail one. This job moves the branch to a push that passed
# (or makes it), but never back: where a newer push's run moved it
# first, it stays. It checks nothing out and runs none of the code. To
# accept a regression, move the branch yourself (for main, git push
# origin main:duel/passed/main).
passed:
name: Mark the commit passed
needs: duel
# One at a time for each branch, so that two runs can't both find no
# branch and race to make it (a push's run doesn't wait for the one
# before it). One that's running is never cancelled, and one that's
# waiting gives way to a newer push's, which moves it further anyway.
concurrency:
group: ${{ github.workflow }}-passed-${{ github.ref_name }}
cancel-in-progress: false
if: ${{ !cancelled() && github.event_name == 'push' && needs.duel.outputs.passed == 'true' }}
runs-on: ubuntu-latest
permissions:
contents: write # to move the branch
steps:
- name: Move the branch to the commit
env:
GH_TOKEN: ${{ github.token }}
BRANCH: duel/passed/${{ github.ref_name }}
run: |
# (Git can't make a branch where another's name is a folder of its
# name: a branch named duel blocks duel/passed/main. If one does,
# this names it.)
blocked() {
prefix=$BRANCH
while [ "${prefix%/*}" != "$prefix" ]; do
prefix=${prefix%/*}
if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$prefix" > /dev/null 2>&1; then
echo "::error::The branch $prefix keeps Duel from making $BRANCH, as Git can't have both. Delete it (git push origin --delete $prefix), or rename it."
fi
done
exit 1
}
# (Forward: GitHub moves it only where it's behind the push. Where
# there's no branch, and making it fails, it looks once more, as
# something else may have made it: a push of it, say.)
for _ in 1 2; do
gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null 2>&1 && exit 0
at=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$BRANCH" --jq .object.sha 2>/dev/null) || at=
[ -n "$at" ] && break
gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null && exit 0
done
[ -n "$at" ] || blocked
compared=$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$at" --jq .status 2>&1) || case "$compared" in
# (GitHub can't compare commits with no history in common.)
*"No common ancestor"*) compared=unrelated ;;
esac
case "$compared" in
# (A newer push's run moved it, or this one's did, run again.)
ahead | identical) ;;
# (History rewritten, a force push's, say, or replaced: moved anyway.)
diverged | behind | unrelated) gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" -F force=true > /dev/null ;;
*) echo "::error::Couldn't compare $BRANCH with this push: $compared" && exit 1 ;;
esac