|
| 1 | +name: Perf |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + merge_group: # (a merge queue's) |
| 6 | + push: |
| 7 | + branches: [master] # (and any other branch to guard, a stable one, say) |
| 8 | + schedule: |
| 9 | + - cron: "0 4 * * *" |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +# A pull request's newer push cancels its run, if that's still going, as |
| 13 | +# only its newest commit's result counts. Every other run (a push's, say) |
| 14 | +# runs to the end, so that each push is compared. |
| 15 | +concurrency: |
| 16 | + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.event.pull_request.number || github.run_id }} |
| 17 | + cancel-in-progress: ${{ github.event_name == 'pull_request' }} |
| 18 | + |
| 19 | +# The Duel job runs the branch's code (its benchmarks too), so it only |
| 20 | +# reads. Commenting on a commit is the Comment on the commit job's, which |
| 21 | +# runs none of it, and commenting on a pull request is the Perf comment |
| 22 | +# workflow's. |
| 23 | +permissions: |
| 24 | + contents: read |
| 25 | + pull-requests: read # for the pull request's labels (see c.accept_label) |
| 26 | + |
| 27 | +jobs: |
| 28 | + duel: |
| 29 | + name: Duel |
| 30 | + runs-on: ubuntu-latest |
| 31 | + # Whether to comment (post, change or none), the run's status (passed, |
| 32 | + # failed or neutral), and whether to mark a push passed (true, or |
| 33 | + # nothing), from Duel. |
| 34 | + outputs: |
| 35 | + comment: ${{ steps.compare.outputs.comment }} |
| 36 | + status: ${{ steps.compare.outputs.status }} |
| 37 | + passed: ${{ steps.compare.outputs.passed }} |
| 38 | + steps: |
| 39 | + - uses: actions/checkout@v7 |
| 40 | + with: |
| 41 | + fetch-depth: 0 # the baseline comes from history, tags included |
| 42 | + persist-credentials: false |
| 43 | + # The labels of the pull request (or for a push, of the one that |
| 44 | + # merged it), for c.accept_label in .duel.rb, as a JSON array. They're |
| 45 | + # read as the job runs, and adding the label (or taking it off) runs |
| 46 | + # the job again: see the Perf comment workflow. |
| 47 | + # On a schedule, the last commit since the last release that merged a |
| 48 | + # pull request with the label (ACCEPT_LABEL, as .duel.rb names it): |
| 49 | + # what that slowed down was accepted, so it's compared with instead. |
| 50 | + - name: Find the pull request's labels |
| 51 | + id: labels |
| 52 | + if: github.event_name == 'pull_request' || github.event_name == 'merge_group' || github.event_name == 'push' || github.event_name == 'schedule' |
| 53 | + continue-on-error: true # (without them, nothing's accepted) |
| 54 | + env: |
| 55 | + GH_TOKEN: ${{ github.token }} |
| 56 | + PR: ${{ github.event.pull_request.number }} |
| 57 | + QUEUED: ${{ github.event.merge_group.head_ref }} |
| 58 | + ACCEPT_LABEL: 'duel: accept' |
| 59 | + RELEASE_TAGS: "v*" |
| 60 | + run: | |
| 61 | + if [ "$GITHUB_EVENT_NAME" = schedule ]; then |
| 62 | + tag=$(git describe --tags --abbrev=0 --match "$RELEASE_TAGS" HEAD^ 2>/dev/null) || exit 0 |
| 63 | + since=$(TZ=UTC git log -1 --date=format-local:%Y-%m-%dT%H:%M:%SZ --format=%cd "$tag") |
| 64 | + merged=$(gh pr list --repo "$GITHUB_REPOSITORY" --state merged --label "$ACCEPT_LABEL" --search "merged:>=$since" --limit 1000 \ |
| 65 | + --json mergeCommit --jq '.[].mergeCommit.oid') |
| 66 | + accepted=$(git rev-list "$tag..HEAD" | grep -Fx -m 1 -f <(echo "$merged")) || accepted= |
| 67 | + echo "accepted=$accepted" >> "$GITHUB_OUTPUT" |
| 68 | + exit 0 |
| 69 | + fi |
| 70 | + # (In a merge queue, the last pull request queued, which the queue's |
| 71 | + # branch names: gh-readonly-queue/master/pr-123-<sha>.) |
| 72 | + if [ -n "$QUEUED" ]; then |
| 73 | + PR=${QUEUED##*/pr-} |
| 74 | + PR=${PR%%-*} |
| 75 | + fi |
| 76 | + if [ -n "$PR" ]; then |
| 77 | + labels=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR/labels" --jq '[.[].name]') |
| 78 | + else |
| 79 | + labels=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/pulls" --jq '[.[0].labels[]?.name]') |
| 80 | + fi |
| 81 | + echo "labels=$labels" >> "$GITHUB_OUTPUT" |
| 82 | + - uses: ruby/setup-ruby@v1 |
| 83 | + with: |
| 84 | + ruby-version: "4.0" # the newest Ruby, which those who care most about speed run |
| 85 | + bundler-cache: true |
| 86 | + # Against the branch it targets for a pull request, the queue's base |
| 87 | + # for a merge queue, what was there before for a push (or as |
| 88 | + # c.push_base in .duel.rb says, the last commit Duel passed), and the |
| 89 | + # last release on a schedule (slow creep). Duel adds its report to |
| 90 | + # the job's summary, writes it to duel.md for the comment, and sets |
| 91 | + # the step's outputs to say whether to comment, as c.comment in |
| 92 | + # .duel.rb says, whatever the outcome. |
| 93 | + - name: Compare |
| 94 | + id: compare |
| 95 | + env: |
| 96 | + 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 || '' }} |
| 97 | + # (Run by hand, it compares anyway: with nothing changed, the code |
| 98 | + # with itself, which checks the setup.) |
| 99 | + DUEL_FORCE: ${{ github.event_name == 'workflow_dispatch' && '1' || '' }} |
| 100 | + DUEL_LABELS: ${{ steps.labels.outputs.labels }} |
| 101 | + DUEL_ACCEPTED: ${{ steps.labels.outputs.accepted }} |
| 102 | + run: bundle exec duel --markdown "$RUNNER_TEMP/duel/duel.md" |
| 103 | + # For a pull request, what Duel said, for the Perf comment workflow, |
| 104 | + # which can't see this job's outputs. |
| 105 | + - name: Say whether to comment |
| 106 | + if: ${{ !cancelled() && github.event_name == 'pull_request' }} |
| 107 | + env: |
| 108 | + PR: ${{ github.event.pull_request.number }} |
| 109 | + COMMENT: ${{ steps.compare.outputs.comment }} |
| 110 | + STATUS: ${{ steps.compare.outputs.status }} |
| 111 | + run: | |
| 112 | + mkdir -p "$RUNNER_TEMP/duel" |
| 113 | + printf 'pr=%s\ncomment=%s\nstatus=%s\n' "$PR" "$COMMENT" "$STATUS" > "$RUNNER_TEMP/duel/comment.env" |
| 114 | + # The report, for the comments. |
| 115 | + - name: Keep the report |
| 116 | + if: ${{ !cancelled() && (github.event_name == 'pull_request' || github.event_name == 'push') }} |
| 117 | + uses: actions/upload-artifact@v7 |
| 118 | + with: |
| 119 | + name: duel-report |
| 120 | + path: ${{ runner.temp }}/duel/ |
| 121 | + if-no-files-found: ignore # none when Duel didn't get to compare |
| 122 | + retention-days: 1 |
0 commit comments