duel 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/docs/ci.md CHANGED
@@ -126,145 +126,6 @@ jobs:
126
126
  path: ${{ runner.temp }}/duel/
127
127
  if-no-files-found: ignore # none when Duel didn't get to compare
128
128
  retention-days: 1
129
-
130
- # The report, as a new comment on a pushed commit, which notifies its
131
- # author, when Duel says to, as for a pull request (see the Perf comment
132
- # workflow). It does only where a regression in a push passes, with
133
- # c.fail_on = :pull_requests in .duel.rb, so the comment says what the
134
- # check doesn't. Its last comment is the last one on any commit. This
135
- # job checks nothing out and runs none of the code.
136
- commit-comment:
137
- name: Comment on the commit
138
- needs: duel
139
- if: ${{ !cancelled() && github.event_name == 'push' && (needs.duel.outputs.comment == 'post' || needs.duel.outputs.comment == 'change') }}
140
- runs-on: ubuntu-latest
141
- permissions:
142
- contents: write # to comment on the commit with the report
143
- steps:
144
- - name: Get the report
145
- id: report
146
- continue-on-error: true # there's none when the Duel job failed before comparing
147
- uses: actions/download-artifact@v8
148
- with:
149
- name: duel-report
150
- path: ${{ runner.temp }}/report
151
- - name: Comment on the commit
152
- if: steps.report.outcome == 'success'
153
- env:
154
- GH_TOKEN: ${{ github.token }}
155
- COMMENT: ${{ needs.duel.outputs.comment }}
156
- STATUS: ${{ needs.duel.outputs.status }}
157
- run: |
158
- marker="<!-- duel"
159
- last=$(gh api --paginate "repos/$GITHUB_REPOSITORY/comments" \
160
- --jq ".[] | select(.user.login == \"github-actions[bot]\") | .body | split(\"\n\")[0] | rtrimstr(\"\r\") | select(startswith(\"$marker status=\"))" | tail -n 1)
161
- if [ "$COMMENT" = post ] || { [ -n "$last" ] && [ "$last" != "$marker status=$STATUS -->" ]; }; then
162
- { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
163
- gh api --method POST "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
164
- fi
165
-
166
- # With c.push_base = :last_passed in .duel.rb, a push is compared with
167
- # the last commit Duel passed, which a branch marks (each branch pushed
168
- # has its own), so slowdowns too small for any one push to fail add up
169
- # until they fail one. This job moves the branch to a push that passed
170
- # (or makes it), but never back: where a newer push's run moved it
171
- # first, it stays. It checks nothing out and runs none of the code. To
172
- # accept a regression, move the branch yourself (for main, git push
173
- # origin main:duel/passed/main).
174
- passed:
175
- name: Mark the commit passed
176
- needs: duel
177
- if: ${{ !cancelled() && github.event_name == 'push' && needs.duel.outputs.passed == 'true' }}
178
- runs-on: ubuntu-latest
179
- permissions:
180
- contents: write # to move the branch
181
- steps:
182
- - name: Move the branch to the commit
183
- env:
184
- GH_TOKEN: ${{ github.token }}
185
- BRANCH: duel/passed/${{ github.ref_name }}
186
- run: |
187
- # (Git can't make a branch where another's name is a folder of its
188
- # name: a branch named duel blocks duel/passed/main. If one does,
189
- # this names it.)
190
- blocked() {
191
- prefix=$BRANCH
192
- while [ "${prefix%/*}" != "$prefix" ]; do
193
- prefix=${prefix%/*}
194
- if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$prefix" > /dev/null 2>&1; then
195
- 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."
196
- fi
197
- done
198
- exit 1
199
- }
200
- # (Forward: GitHub moves it only where it's behind the push.)
201
- gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null 2>&1 && exit 0
202
- at=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$BRANCH" --jq .object.sha 2>/dev/null) || at=
203
- if [ -z "$at" ]; then
204
- gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null || blocked
205
- exit 0
206
- fi
207
- compared=$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$at" --jq .status)
208
- case "$compared" in
209
- # (A newer push's run moved it, or this one's did, run again.)
210
- ahead | identical) ;;
211
- # (History rewritten, a force push's, say: moved anyway.)
212
- diverged | behind) gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" -F force=true > /dev/null ;;
213
- *) echo "::error::Couldn't compare $BRANCH with this push: $compared" && exit 1 ;;
214
- esac
215
-
216
- # Duel's badge, for the README: green (perf guarded) or red (perf
217
- # regressed), as the last push to the default branch that Duel compared
218
- # left it. It's red where the Duel job failed or found a regression,
219
- # even one that passed (with c.fail_on = :pull_requests in .duel.rb). A
220
- # push with nothing to compare leaves it as it was, and so does a run
221
- # for an older push than the badge's. This job writes the badge, alone,
222
- # to a branch of its own, which the README shows:
223
- #
224
- # [![Duel](https://raw.githubusercontent.com/OWNER/REPO/duel/badge/badge.svg)](https://github.com/sferik/duel-ruby)
225
- #
226
- # It checks nothing out and runs none of the code.
227
- badge:
228
- name: Update the badge
229
- needs: duel
230
- if: ${{ !cancelled() && github.event_name == 'push' && github.ref_name == github.event.repository.default_branch && (needs.duel.result == 'failure' || needs.duel.outputs.status == 'passed' || needs.duel.outputs.status == 'failed') }}
231
- runs-on: ubuntu-latest
232
- permissions:
233
- contents: write # to move the badge's branch
234
- steps:
235
- - name: Update the badge
236
- env:
237
- GH_TOKEN: ${{ github.token }}
238
- BRANCH: duel/badge
239
- RESULT: ${{ needs.duel.result }}
240
- STATUS: ${{ needs.duel.outputs.status }}
241
- BADGES: https://raw.githubusercontent.com/sferik/duel-ruby/main/assets
242
- run: |
243
- # (Git can't make a branch where another's name is a folder of its
244
- # name: a branch named duel blocks duel/passed/main. If one does,
245
- # this names it.)
246
- blocked() {
247
- prefix=$BRANCH
248
- while [ "${prefix%/*}" != "$prefix" ]; do
249
- prefix=${prefix%/*}
250
- if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$prefix" > /dev/null 2>&1; then
251
- 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."
252
- fi
253
- done
254
- exit 1
255
- }
256
- # (Where the badge is a newer push's, that push's run has the say.
257
- # Its commit names the push.)
258
- last=$(gh api "repos/$GITHUB_REPOSITORY/commits/$BRANCH" --jq .commit.message 2>/dev/null | sed -n 's/^Duel.s badge, for \([0-9a-f]*\)$/\1/p') || true
259
- if [ -n "$last" ] && [ "$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$last" --jq .status 2>/dev/null)" = ahead ]; then exit 0; fi
260
- badge="badge.svg"
261
- if [ "$RESULT" = failure ] || [ "$STATUS" = failed ]; then badge="badge-regressed.svg"; fi
262
- curl -fsSL "$BADGES/$badge" -o "$RUNNER_TEMP/badge.svg"
263
- tree=$(jq -n --rawfile svg "$RUNNER_TEMP/badge.svg" '{tree: [{path: "badge.svg", mode: "100644", type: "blob", content: $svg}]}' |
264
- gh api --method POST "repos/$GITHUB_REPOSITORY/git/trees" --input - --jq .sha)
265
- commit=$(gh api --method POST "repos/$GITHUB_REPOSITORY/git/commits" -f message="Duel's badge, for $GITHUB_SHA" -f tree="$tree" --jq .sha)
266
- gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$commit" -F force=true > /dev/null 2>&1 ||
267
- gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$commit" > /dev/null || blocked
268
129
  ```
269
130
 
270
131
  A newer push to a pull request cancels the pull request's run, if it's
@@ -352,9 +213,12 @@ the pull request's code. The pull request's code ran in the Duel job,
352
213
  and could have written anything into the artifact, so the workflow
353
214
  trusts only what Duel could have said there: a number that names the
354
215
  pull request whose head the run compared, and a `comment` and a
355
- `status` that Duel uses. The report's Markdown is the comment's body,
356
- which can only say something on the pull request that the pull request
357
- could say itself. When the Duel job fails before it compares, there's
216
+ `status` that Duel uses. For a pull request from a branch of your
217
+ repository, whose author can push to it anyway, the report's Markdown
218
+ is the comment's body. For one from a fork, the report is shown as text,
219
+ in a code block, since the fork's code wrote it: there, posted by
220
+ `github-actions[bot]`, it can't mention anyone, link anywhere or show an
221
+ image. A report too long for a comment is cut short. When the Duel job fails before it compares, there's
358
222
  no report, and no comment. GitHub runs a `workflow_run` workflow only
359
223
  from your default branch, so the pull request that adds the workflow
360
224
  gets no comment.
@@ -372,7 +236,8 @@ name: Perf comment
372
236
  # the pull request's code, and of what the Duel job kept (where that code
373
237
  # ran), it trusts only what that job could have said: the pull request's
374
238
  # number, if it names the pull request the run compared, and the comment
375
- # and the status, if they're ones Duel says. (The workflow must be on the
239
+ # and the status, if they're ones Duel says. A fork's report, which the
240
+ # fork's code wrote, is shown as text. (The workflow must be on the
376
241
  # default branch before it runs at all.)
377
242
  on:
378
243
  workflow_run:
@@ -410,14 +275,29 @@ jobs:
410
275
  COMMENT=$(said comment 'post|change')
411
276
  STATUS=$(said status 'passed|failed|neutral')
412
277
  [ -n "$PR" ] && [ -n "$COMMENT" ] && [ -n "$STATUS" ] || exit 0
413
- [ "$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq .head.sha)" = "$HEAD_SHA" ] || exit 0
278
+ # (The pull request's head commit, and the repository it's from.)
279
+ head=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq '"\(.head.sha) \(.head.repo.full_name)"')
280
+ [ "${head%% *}" = "$HEAD_SHA" ] || exit 0
281
+ # The report, as Markdown for a branch of this repository (whose
282
+ # author can push here anyway), but for a fork's, as text: its run
283
+ # ran the fork's code, which could have written anything in it. In
284
+ # a code block, fenced with more backticks than any run of them in
285
+ # the report, it can't mention anyone, link anywhere or show an
286
+ # image. (And it's cut short where a comment couldn't hold it.)
287
+ report() {
288
+ if [ "${head#* }" = "$GITHUB_REPOSITORY" ]; then cat "$RUNNER_TEMP/report/duel.md" && return; fi
289
+ fence=$(grep -o '``*' "$RUNNER_TEMP/report/duel.md" | awk '{ if (length > n) n = length } END { n = (n < 3) ? 3 : n + 1; while (n-- > 0) printf "`" }')
290
+ printf 'From a fork, so shown as text:\n\n%s\n' "$fence"
291
+ head -c 60000 "$RUNNER_TEMP/report/duel.md"
292
+ printf '\n%s\n' "$fence"
293
+ }
414
294
  marker="<!-- duel"
415
295
  # The first line of the last comment Duel posted here: its marker,
416
296
  # with its status (none in Duel 0.1.0's, which counts as another).
417
297
  last=$(gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$PR/comments" \
418
298
  --jq ".[] | select(.user.login == \"github-actions[bot]\") | .body | split(\"\n\")[0] | rtrimstr(\"\r\") | select(startswith(\"$marker status=\") or . == \"$marker -->\")" | tail -n 1)
419
299
  if [ "$COMMENT" = post ] || { [ -n "$last" ] && [ "$last" != "$marker status=$STATUS -->" ]; }; then
420
- { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
300
+ { echo "$marker status=$STATUS -->"; report; } > "$RUNNER_TEMP/comment.md"
421
301
  gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$PR/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
422
302
  fi
423
303
  ```
@@ -438,6 +318,52 @@ By default, a push gets no comment: Duel sets the `comment` output to
438
318
  the commit job checks nothing out either, and it alone gets the
439
319
  `contents: write` permission that a commit comment takes.
440
320
 
321
+ `duel init` writes the Comment on the commit job only where `.duel.rb`
322
+ sets `c.fail_on = :pull_requests`, as it's the only time Duel says to
323
+ comment on a push, and the job's `contents: write` is otherwise unused.
324
+ To add it later, set `c.fail_on`, remove the workflow, and run
325
+ `duel init` again (it keeps `.duel.rb`), or add the job yourself:
326
+
327
+ ```yaml
328
+ # .github/workflows/perf.yml, in jobs: (with c.fail_on = :pull_requests)
329
+ # The report, as a new comment on a pushed commit, which notifies its
330
+ # author, when Duel says to, as for a pull request (see the Perf comment
331
+ # workflow). It does only where a regression in a push passes, with
332
+ # c.fail_on = :pull_requests in .duel.rb, so the comment says what the
333
+ # check doesn't. Its last comment is the last one on any commit. This
334
+ # job checks nothing out and runs none of the code.
335
+ commit-comment:
336
+ name: Comment on the commit
337
+ needs: duel
338
+ if: ${{ !cancelled() && github.event_name == 'push' && (needs.duel.outputs.comment == 'post' || needs.duel.outputs.comment == 'change') }}
339
+ runs-on: ubuntu-latest
340
+ permissions:
341
+ contents: write # to comment on the commit with the report
342
+ steps:
343
+ - name: Get the report
344
+ id: report
345
+ continue-on-error: true # there's none when the Duel job failed before comparing
346
+ uses: actions/download-artifact@v8
347
+ with:
348
+ name: duel-report
349
+ path: ${{ runner.temp }}/report
350
+ - name: Comment on the commit
351
+ if: steps.report.outcome == 'success'
352
+ env:
353
+ GH_TOKEN: ${{ github.token }}
354
+ COMMENT: ${{ needs.duel.outputs.comment }}
355
+ STATUS: ${{ needs.duel.outputs.status }}
356
+ run: |
357
+ marker="<!-- duel"
358
+ last=$(gh api --paginate "repos/$GITHUB_REPOSITORY/comments" \
359
+ --jq ".[] | select(.user.login == \"github-actions[bot]\") | .body | split(\"\n\")[0] | rtrimstr(\"\r\") | select(startswith(\"$marker status=\"))" | tail -n 1)
360
+ if [ "$COMMENT" = post ] || { [ -n "$last" ] && [ "$last" != "$marker status=$STATUS -->" ]; }; then
361
+ { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
362
+ gh api --method POST "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
363
+ fi
364
+
365
+ ```
366
+
441
367
  ## Accept a regression with a label
442
368
 
443
369
  With [`c.accept_label`](configuration.md#accept-a-regression-with-a-label),
@@ -493,11 +419,75 @@ If you set `c.push_base` back to `:before`, delete the branches too
493
419
  (`git push origin --delete duel/passed/main`), because the step that
494
420
  finds the baseline's Ruby uses one whenever it exists.
495
421
 
422
+ `duel init` writes the Mark the commit passed job only where `.duel.rb`
423
+ sets `c.push_base = :last_passed`, as the job's `contents: write` is
424
+ otherwise unused. To add it later, set `c.push_base`, remove the
425
+ workflow, and run `duel init` again (it keeps `.duel.rb`), or add the job
426
+ yourself:
427
+
428
+ ```yaml
429
+ # .github/workflows/perf.yml, in jobs: (with c.push_base = :last_passed)
430
+ # With c.push_base = :last_passed in .duel.rb, a push is compared with
431
+ # the last commit Duel passed, which a branch marks (each branch pushed
432
+ # has its own), so slowdowns too small for any one push to fail add up
433
+ # until they fail one. This job moves the branch to a push that passed
434
+ # (or makes it), but never back: where a newer push's run moved it
435
+ # first, it stays. It checks nothing out and runs none of the code. To
436
+ # accept a regression, move the branch yourself (for main, git push
437
+ # origin main:duel/passed/main).
438
+ passed:
439
+ name: Mark the commit passed
440
+ needs: duel
441
+ if: ${{ !cancelled() && github.event_name == 'push' && needs.duel.outputs.passed == 'true' }}
442
+ runs-on: ubuntu-latest
443
+ permissions:
444
+ contents: write # to move the branch
445
+ steps:
446
+ - name: Move the branch to the commit
447
+ env:
448
+ GH_TOKEN: ${{ github.token }}
449
+ BRANCH: duel/passed/${{ github.ref_name }}
450
+ run: |
451
+ # (Git can't make a branch where another's name is a folder of its
452
+ # name: a branch named duel blocks duel/passed/main. If one does,
453
+ # this names it.)
454
+ blocked() {
455
+ prefix=$BRANCH
456
+ while [ "${prefix%/*}" != "$prefix" ]; do
457
+ prefix=${prefix%/*}
458
+ if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$prefix" > /dev/null 2>&1; then
459
+ 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."
460
+ fi
461
+ done
462
+ exit 1
463
+ }
464
+ # (Forward: GitHub moves it only where it's behind the push.)
465
+ gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null 2>&1 && exit 0
466
+ at=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$BRANCH" --jq .object.sha 2>/dev/null) || at=
467
+ if [ -z "$at" ]; then
468
+ gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null || blocked
469
+ exit 0
470
+ fi
471
+ compared=$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$at" --jq .status 2>&1) || case "$compared" in
472
+ # (GitHub can't compare commits with no history in common.)
473
+ *"No common ancestor"*) compared=unrelated ;;
474
+ esac
475
+ case "$compared" in
476
+ # (A newer push's run moved it, or this one's did, run again.)
477
+ ahead | identical) ;;
478
+ # (History rewritten, a force push's, say, or replaced: moved anyway.)
479
+ diverged | behind | unrelated) gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" -F force=true > /dev/null ;;
480
+ *) echo "::error::Couldn't compare $BRANCH with this push: $compared" && exit 1 ;;
481
+ esac
482
+
483
+ ```
484
+
496
485
  ## Show whether the last push passed
497
486
 
498
- The Update the badge job keeps a badge that turns red when Duel fails.
499
- After each push to your default branch, it writes `badge.svg` to a
500
- branch of its own, `duel/badge`, so your README can show it:
487
+ With `--badge`, `duel init` adds the Update the badge job, which keeps a
488
+ badge that turns red when Duel fails. After each push to your default
489
+ branch, it writes `badge.svg` to a branch of its own, `duel/badge`, so
490
+ your README can show it:
501
491
 
502
492
  ```markdown
503
493
  [![Duel](https://raw.githubusercontent.com/OWNER/REPO/duel/badge/badge.svg)](https://github.com/sferik/duel-ruby)
@@ -516,8 +506,124 @@ that moving a branch takes. For a project in a subdirectory of its
516
506
  repository, the branch names the directory, such as
517
507
  `duel/badge-gems-parser`.
518
508
 
519
- Until the first push to your default branch after you add the
520
- workflow, the branch doesn't exist, and the badge doesn't show.
509
+ Until the first push to your default branch after you add the job, the
510
+ branch doesn't exist, and the badge doesn't show.
511
+
512
+ `duel init` writes the job only with `--badge` (for a Rails app,
513
+ `bin/rails generate duel:install --badge`), as without the jobs for
514
+ pushes, it's the workflow's only job that writes to the repository. To
515
+ add it later, remove the workflow, and run `duel init --badge` again (it
516
+ keeps `.duel.rb`), or add the job yourself:
517
+
518
+ ```yaml
519
+ # .github/workflows/perf.yml, in jobs: (with duel init --badge)
520
+ # Duel's badge, for the README: green (perf guarded) or red (perf
521
+ # regressed), as the last push to the default branch that Duel compared
522
+ # left it. It's red where the Duel job failed or found a regression,
523
+ # even one that passed (with c.fail_on = :pull_requests in .duel.rb). A
524
+ # push with nothing to compare leaves it as it was, and so does a run
525
+ # for an older push than the badge's. This job writes the badge, alone,
526
+ # to a branch of its own, which the README shows:
527
+ #
528
+ # [![Duel](https://raw.githubusercontent.com/OWNER/REPO/duel/badge/badge.svg)](https://github.com/sferik/duel-ruby)
529
+ #
530
+ # It checks nothing out, runs none of the code, and fetches nothing.
531
+ badge:
532
+ name: Update the badge
533
+ needs: duel
534
+ # One at a time, so that one run's check of which push the badge
535
+ # shows can't interleave with another run's write (a push's run
536
+ # doesn't wait for the one before it). A badge job that's running is
537
+ # never cancelled, and one that's waiting gives way to a newer push's,
538
+ # whose result the badge should show anyway.
539
+ concurrency:
540
+ group: ${{ github.workflow }}-badge
541
+ cancel-in-progress: false
542
+ if: ${{ !cancelled() && github.event_name == 'push' && github.ref_name == github.event.repository.default_branch && (needs.duel.result == 'failure' || needs.duel.outputs.status == 'passed' || needs.duel.outputs.status == 'failed') }}
543
+ runs-on: ubuntu-latest
544
+ permissions:
545
+ contents: write # to move the badge's branch
546
+ steps:
547
+ - name: Update the badge
548
+ env:
549
+ GH_TOKEN: ${{ github.token }}
550
+ BRANCH: duel/badge
551
+ RESULT: ${{ needs.duel.result }}
552
+ STATUS: ${{ needs.duel.outputs.status }}
553
+ run: |
554
+ # (Git can't make a branch where another's name is a folder of its
555
+ # name: a branch named duel blocks duel/passed/main. If one does,
556
+ # this names it.)
557
+ blocked() {
558
+ prefix=$BRANCH
559
+ while [ "${prefix%/*}" != "$prefix" ]; do
560
+ prefix=${prefix%/*}
561
+ if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$prefix" > /dev/null 2>&1; then
562
+ 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."
563
+ fi
564
+ done
565
+ exit 1
566
+ }
567
+ # (Where the badge is a newer push's, that push's run has the say.
568
+ # Its commit names the push.)
569
+ last=$(gh api "repos/$GITHUB_REPOSITORY/commits/$BRANCH" --jq .commit.message 2>/dev/null | sed -n 's/^Duel.s badge, for \([0-9a-f]*\)$/\1/p') || true
570
+ if [ -n "$last" ] && [ "$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$last" --jq .status 2>/dev/null)" = ahead ]; then exit 0; fi
571
+ # (Its image, written here rather than fetched from anywhere: green,
572
+ # or red where the Duel job failed or found a regression.)
573
+ state="perf guarded" width=133 top="#34D058" bottom="#28A745" right="76.939C78.629 0 80 1.343 80"
574
+ if [ "$RESULT" = failure ] || [ "$STATUS" = failed ]; then
575
+ state="perf regressed" width=142 top="#EA4A5A" bottom="#D73A49" right="85.939C87.629 0 89 1.343 89"
576
+ fi
577
+ cat > "$RUNNER_TEMP/badge.svg" <<SVG
578
+ <svg xmlns="http://www.w3.org/2000/svg" width="$width" height="20">
579
+ <title>Duel - $state</title>
580
+ <defs>
581
+ <linearGradient id="duel-fill" x1="50%" y1="0%" x2="50%" y2="100%">
582
+ <stop stop-color="#444D56" offset="0%"></stop>
583
+ <stop stop-color="#24292E" offset="100%"></stop>
584
+ </linearGradient>
585
+ <linearGradient id="state-fill" x1="50%" y1="0%" x2="50%" y2="100%">
586
+ <stop stop-color="$top" offset="0%"></stop>
587
+ <stop stop-color="$bottom" offset="100%"></stop>
588
+ </linearGradient>
589
+ </defs>
590
+ <g fill="none" fill-rule="evenodd">
591
+ <g font-family="'DejaVu Sans',Verdana,Geneva,sans-serif" font-size="11">
592
+ <path d="M0,3 C0,1.3431 1.3552,0 3.02702703,0 L53,0 L53,20 L3.02702703,20 C1.3552,20 0,18.6569 0,17 L0,3 Z" fill="url(#duel-fill)" fill-rule="nonzero"></path>
593
+ <text fill="#010101" fill-opacity=".3">
594
+ <tspan x="22.1981982" y="15" aria-hidden="true">Duel</tspan>
595
+ </text>
596
+ <text fill="#FFFFFF">
597
+ <tspan x="22.1981982" y="14">Duel</tspan>
598
+ </text>
599
+ </g>
600
+ <g transform="translate(53)" font-family="'DejaVu Sans',Verdana,Geneva,sans-serif" font-size="11">
601
+ <path d="M0 0h$right 3v14c0 1.657-1.37 3-3.061 3H0V0z" fill="url(#state-fill)" fill-rule="nonzero"></path>
602
+ <text fill="#010101" fill-opacity=".3" aria-hidden="true">
603
+ <tspan x="4" y="15">$state</tspan>
604
+ </text>
605
+ <text fill="#FFFFFF">
606
+ <tspan x="4" y="14">$state</tspan>
607
+ </text>
608
+ </g>
609
+ <g fill="#959DA5">
610
+ <path d="M4.6 3.6L6.9 4.5L14.3 11.9L12.9 13.3L5.5 5.9Z M17.4 3.6L15.1 4.5L7.7 11.9L9.1 13.3L16.5 5.9Z"></path>
611
+ <circle cx="16.6" cy="15.6" r="1.1"></circle>
612
+ <circle cx="5.4" cy="15.6" r="1.1"></circle>
613
+ </g>
614
+ <g stroke="#959DA5" stroke-linecap="round">
615
+ <path d="M12.1 14.5L15.5 11.1 M9.9 14.5L6.5 11.1" stroke-width="1.6"></path>
616
+ <path d="M13.8 12.8L16 15 M8.2 12.8L6 15" stroke-width="1.8"></path>
617
+ </g>
618
+ </g>
619
+ </svg>
620
+ SVG
621
+ tree=$(jq -n --rawfile svg "$RUNNER_TEMP/badge.svg" '{tree: [{path: "badge.svg", mode: "100644", type: "blob", content: $svg}]}' |
622
+ gh api --method POST "repos/$GITHUB_REPOSITORY/git/trees" --input - --jq .sha)
623
+ commit=$(gh api --method POST "repos/$GITHUB_REPOSITORY/git/commits" -f message="Duel's badge, for $GITHUB_SHA" -f tree="$tree" --jq .sha)
624
+ gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$commit" -F force=true > /dev/null 2>&1 ||
625
+ gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$commit" > /dev/null || blocked
626
+ ```
521
627
 
522
628
  ## Guard other branches
523
629
 
@@ -145,7 +145,10 @@ pull request's Checks tab). For a push, Duel reads the labels of the
145
145
  pull request that merged it, so with
146
146
  [`c.push_base = :last_passed`](#compare-a-push-with-the-last-commit-that-passed),
147
147
  the merged regression becomes the commit that later pushes are compared
148
- with. By default, there's no such label.
148
+ with. Duel's default is no such label, but the `.duel.rb` that
149
+ `duel init` writes (and duel-rails's generator) names `duel: accept`.
150
+ Create the label in your repository to use it, or set
151
+ `c.accept_label = nil` so that nothing can let a regression through.
149
152
 
150
153
  ## Compare a push with the last commit that passed
151
154
 
data/docs/how-it-works.md CHANGED
@@ -234,5 +234,7 @@ Neither has an allocation count, an instruction count, or profiles.
234
234
  TruffleRuby has peak memory only on Linux. JRuby measures peak memory in
235
235
  the JVM's heap in use, on any operating system, because the JVM takes
236
236
  memory for its heap ahead of time, so resident memory barely moves as
237
- a benchmark's objects fill it. StackProf runs on CRuby only: Duel
237
+ a benchmark's objects fill it. The peak starts from the heap in use
238
+ after a collection that Duel asks the JVM for, as JRuby's `GC.start`
239
+ collects nothing. StackProf runs on CRuby only: Duel
238
240
  doesn't use it on JRuby or TruffleRuby, even where it's installed.
@@ -72,6 +72,20 @@ happens, Duel stops and tells you to set `c.build` to a command that
72
72
  builds each version, such as `c.build = "make"`. See
73
73
  [Prepare each version](configuration.md#prepare-each-version).
74
74
 
75
+ ## Duel says a version loaded an extension twice
76
+
77
+ A worker loads `.duel.rb` and your plugins before it loads its version.
78
+ If one of them requires your library, or a gem that requires it, the
79
+ worker finds the library wherever the load path leads, such as the
80
+ candidate's `lib/` or an installed release. Duel then loads the
81
+ version's own build, but a native extension can't be unloaded, so both
82
+ stay loaded, and the comparison wouldn't be of the version's code alone.
83
+ Duel stops and names the extension and both of its files.
84
+
85
+ To fix it, require what loads your library in a block that runs once the
86
+ version has loaded, such as `c.in_worker`, not at the top of `.duel.rb`
87
+ or as a plugin loads.
88
+
75
89
  ## Duel says that the versions' paths differ in length
76
90
 
77
91
  The baseline's path is as long and as deep as your project's, because
data/lib/duel/context.rb CHANGED
@@ -508,7 +508,7 @@ module Duel
508
508
 
509
509
  def start
510
510
  @started = true
511
- GC.start(full_mark: true, immediate_sweep: true)
511
+ Native::Memory.collect
512
512
  @base = resident
513
513
  @peak = 0
514
514
  @watcher = Thread.new do
data/lib/duel/init.rb CHANGED
@@ -11,7 +11,8 @@ module Duel
11
11
  # `duel init`: sets Duel up in a gem or an app that isn't a Rails app
12
12
  # (duel-rails's generator sets a Rails app up). It writes .duel.rb, a
13
13
  # benchmark to start from, and GitHub Actions workflows (Duel's, and
14
- # one that comments on a pull request with its report), or with
14
+ # one that comments on a pull request with its report, and with --badge
15
+ # a job that keeps a badge), or with
15
16
  # --gitlab, GitLab CI jobs to include, adds a Rake
16
17
  # task to the Rakefile, and adds StackProf to the Gemfile (for profiles
17
18
  # of regressions, on CRuby). It overwrites nothing without --force, and
@@ -79,6 +80,7 @@ module Duel
79
80
  o.banner = USAGE
80
81
  o.on("--force", "Overwrite files that exist") { @force = true }
81
82
  o.on("--dry-run", "Say what it would do, and write nothing") { @dry_run = true }
83
+ o.on("--badge", "Write a job that keeps a badge for the README (it needs contents: write)") { @badge = true }
82
84
  o.on("--gitlab", "Write GitLab CI jobs (to include from .gitlab-ci.yml), not GitHub Actions workflows") { @gitlab = true }
83
85
  o.on("-h", "--help", "Print this help") do
84
86
  @out.puts o
@@ -179,6 +181,42 @@ module Duel
179
181
  # The project's configuration, as the conventions have it.
180
182
  def config = Config.new(@root)
181
183
 
184
+ # The project's settings, which decide the workflow's jobs: its
185
+ # .duel.rb's, or the defaults, where there's none yet or --force
186
+ # replaces it with the one it writes. Nil where it doesn't load (one
187
+ # of its plugins isn't installed, say), and then the workflow has
188
+ # both jobs for pushes, as if .duel.rb asked for them, and says so.
189
+ def settings = instance_variable_defined?(:@settings) ? @settings : (@settings = load_settings)
190
+
191
+ def load_settings
192
+ path = File.join(@root, ".duel.rb")
193
+ return Config.new if @force || !File.exist?(path)
194
+
195
+ Duel.load_config(path)
196
+ rescue ScriptError, StandardError => e
197
+ @err.puts "duel init: .duel.rb didn't load (#{e.class}: #{e}), so the workflow has both jobs for pushes, as if it asked for them"
198
+ end
199
+
200
+ # Whether the workflow comments on a pushed commit: where a regression
201
+ # in a push passes (c.fail_on = :pull_requests), the only time Duel
202
+ # says to.
203
+ def comment_on_pushes?
204
+ found = settings
205
+ found.nil? || found.fail_on == :pull_requests
206
+ end
207
+
208
+ # Whether the workflow marks a push passed: where a push is compared
209
+ # with the last commit Duel passed (c.push_base = :last_passed).
210
+ def mark_passed?
211
+ found = settings
212
+ found.nil? || found.push_base == :last_passed
213
+ end
214
+
215
+ # Whether the workflow keeps a badge for the README (--badge): its job
216
+ # moves a branch, so it's the workflow's only job, without the jobs
217
+ # for pushes, that writes to the repository.
218
+ def badge? = @badge
219
+
182
220
  # The library the gemspec names (see Config#library), or nil.
183
221
  def library = config.library
184
222
 
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Duel
4
+ # What the runner and a worker say to each other, over the worker's
5
+ # socket: plain Hashes, Arrays, Strings, numbers, true, false and nil,
6
+ # marshaled. Not JSON: the worker would load json before it loads the
7
+ # version it measures, and in json's own repository, every baseline
8
+ # worker would load the candidate's json first, and the baseline's
9
+ # over it. Marshal is Ruby's own, with nothing to load (so a version's
10
+ # code is the first of it the worker loads), and it keeps a String's
11
+ # encoding. (Both ends are Duel's own processes, and the socket is in
12
+ # the run's own directory.)
13
+ # @api private
14
+ module Message
15
+ # Sends +message+ over +io+.
16
+ def self.write(io, message) = io.write(Marshal.dump(message))
17
+
18
+ # The next message over +io+, or nil once the other end has closed it.
19
+ def self.read(io)
20
+ Marshal.load(io)
21
+ rescue EOFError
22
+ nil
23
+ end
24
+ end
25
+ end