Skip to content

Commit 8763062

Browse files
committed
ci: benchmark pull requests with the benchmark label and post the result as a comment
1 parent 212f847 commit 8763062

3 files changed

Lines changed: 125 additions & 3 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Benchmark comment
2+
3+
# runs in this repository with its own token once a Benchmark run of a pull request is done,
4+
# so the comment can be written also for a pull request from a fork. Nothing from the pull
5+
# request is checked out or run here, only the summary it produced is read
6+
on:
7+
workflow_run:
8+
workflows: [Benchmark]
9+
types: [completed]
10+
11+
permissions: {}
12+
13+
jobs:
14+
comment:
15+
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
16+
timeout-minutes: 5
17+
runs-on: ubuntu-latest
18+
permissions:
19+
actions: read
20+
pull-requests: write
21+
steps:
22+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
23+
with:
24+
name: benchmark-summary
25+
run-id: ${{ github.event.workflow_run.id }}
26+
github-token: ${{ github.token }}
27+
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
28+
with:
29+
script: |
30+
const fs = require('node:fs')
31+
const marker = '<!-- benchmark-comment -->'
32+
// the summary comes from the pull request, so it is cut to what a comment can hold
33+
const body = fs.readFileSync('benchmark_summary.md', 'utf8').slice(0, 60000)
34+
const run = context.payload.workflow_run
35+
// the pull request comes from the event, not from the artifact, so a branch cannot
36+
// pick where its table is posted
37+
const { data: pulls } = await github.rest.pulls.list({
38+
owner: context.repo.owner,
39+
repo: context.repo.repo,
40+
state: 'open',
41+
head: `${run.head_repository.owner.login}:${run.head_branch}`
42+
})
43+
const pull = pulls.find((p) => p.head.sha === run.head_sha)
44+
if (!pull) {
45+
core.info(`no open pull request at ${run.head_sha}, nothing to comment`)
46+
return
47+
}
48+
const issue_number = pull.number
49+
const comments = await github.paginate(github.rest.issues.listComments, {
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
issue_number
53+
})
54+
const existing = comments.find((c) => c.user?.type === 'Bot' && c.body?.includes(marker))
55+
if (existing) {
56+
await github.rest.issues.updateComment({ owner: context.repo.owner, repo: context.repo.repo, comment_id: existing.id, body })
57+
} else {
58+
await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number, body })
59+
}

‎.github/workflows/benchmark.yml‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Benchmark
2+
3+
# runs on a pull request with the benchmark label, it takes about 15 minutes
4+
on:
5+
pull_request:
6+
types: [opened, reopened, synchronize, labeled]
7+
workflow_dispatch:
8+
inputs:
9+
against:
10+
description: 'Ref to compare this branch against'
11+
default: main
12+
13+
concurrency:
14+
group: "${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}"
15+
cancel-in-progress: true
16+
17+
permissions:
18+
contents: read
19+
20+
jobs:
21+
benchmark:
22+
if: >-
23+
github.event_name == 'workflow_dispatch' ||
24+
(github.event.action == 'labeled' && github.event.label.name == 'benchmark') ||
25+
(github.event.action != 'labeled' && contains(github.event.pull_request.labels.*.name, 'benchmark'))
26+
timeout-minutes: 40
27+
runs-on: ubuntu-latest
28+
steps:
29+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
30+
with:
31+
# the base revision has to be in the clone for the worktree of bench:cmp
32+
fetch-depth: 0
33+
persist-credentials: false
34+
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
35+
with:
36+
node-version: lts/*
37+
- name: Install
38+
run: npm install --ignore-scripts
39+
- name: Run benchmark
40+
env:
41+
AGAINST: ${{ github.event.pull_request.base.sha || inputs.against }}
42+
run: |
43+
{
44+
echo '<!-- benchmark-comment -->'
45+
echo "### Benchmark against \`${AGAINST:0:12}\`"
46+
echo
47+
echo "Node $(node --version) on $RUNNER_OS, from \`npm run bench:cmp\`."
48+
echo
49+
echo '```text'
50+
node benchmark/bench-cmp-branch.js --against "$AGAINST"
51+
echo '```'
52+
} > benchmark_summary.md
53+
- name: Publish the summary
54+
if: always()
55+
run: cat benchmark_summary.md >> "$GITHUB_STEP_SUMMARY"
56+
# the comment on the pull request is posted by benchmark-comment.yml from this artifact:
57+
# the token of a pull request from a fork cannot write comments
58+
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
59+
with:
60+
name: benchmark-summary
61+
path: benchmark_summary.md

‎benchmark/bench-cmp-branch.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ const BAND = 4
3737
// of A/B in every round, and out of A/A and B/B every four rounds
3838
const ORDERS = [[0, 2, 3, 1], [2, 0, 1, 3], [1, 3, 2, 0], [3, 1, 0, 2]]
3939

40-
const greenColor = '\x1b[32m'
41-
const redColor = '\x1b[31m'
42-
const resetColor = '\x1b[0m'
40+
// no colors when the output goes to a file, as in the benchmark workflow
41+
const tty = process.stdout.isTTY
42+
const greenColor = tty ? '\x1b[32m' : ''
43+
const redColor = tty ? '\x1b[31m' : ''
44+
const resetColor = tty ? '\x1b[0m' : ''
4345

4446
if (isMainThread) {
4547
main().catch((error) => {

0 commit comments

Comments
 (0)