From 87630628daad853ff3d6ebbda5fc04d7d2d75984 Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 21 Nov 2026 07:56:23 +0100 Subject: [PATCH] ci: benchmark pull requests with the benchmark label and post the result as a comment --- .github/workflows/benchmark-comment.yml | 59 ++++++++++++++++++++++++ .github/workflows/benchmark.yml | 61 +++++++++++++++++++++++++ benchmark/bench-cmp-branch.js | 8 ++-- 3 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/benchmark-comment.yml create mode 100644 .github/workflows/benchmark.yml diff --git a/.github/workflows/benchmark-comment.yml b/.github/workflows/benchmark-comment.yml new file mode 100644 index 00000000..46524152 --- /dev/null +++ b/.github/workflows/benchmark-comment.yml @@ -0,0 +1,59 @@ +name: Benchmark comment + +# runs in this repository with its own token once a Benchmark run of a pull request is done, +# so the comment can be written also for a pull request from a fork. Nothing from the pull +# request is checked out or run here, only the summary it produced is read +on: + workflow_run: + workflows: [Benchmark] + types: [completed] + +permissions: {} + +jobs: + comment: + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' + timeout-minutes: 5 + runs-on: ubuntu-latest + permissions: + actions: read + pull-requests: write + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: benchmark-summary + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ github.token }} + - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const fs = require('node:fs') + const marker = '' + // the summary comes from the pull request, so it is cut to what a comment can hold + const body = fs.readFileSync('benchmark_summary.md', 'utf8').slice(0, 60000) + const run = context.payload.workflow_run + // the pull request comes from the event, not from the artifact, so a branch cannot + // pick where its table is posted + const { data: pulls } = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + head: `${run.head_repository.owner.login}:${run.head_branch}` + }) + const pull = pulls.find((p) => p.head.sha === run.head_sha) + if (!pull) { + core.info(`no open pull request at ${run.head_sha}, nothing to comment`) + return + } + const issue_number = pull.number + const comments = await github.paginate(github.rest.issues.listComments, { + owner: context.repo.owner, + repo: context.repo.repo, + issue_number + }) + const existing = comments.find((c) => c.user?.type === 'Bot' && c.body?.includes(marker)) + if (existing) { + await github.rest.issues.updateComment({ owner: context.repo.owner, repo: context.repo.repo, comment_id: existing.id, body }) + } else { + await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number, body }) + } diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 00000000..678403a0 --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,61 @@ +name: Benchmark + +# runs on a pull request with the benchmark label, it takes about 15 minutes +on: + pull_request: + types: [opened, reopened, synchronize, labeled] + workflow_dispatch: + inputs: + against: + description: 'Ref to compare this branch against' + default: main + +concurrency: + group: "${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}" + cancel-in-progress: true + +permissions: + contents: read + +jobs: + benchmark: + if: >- + github.event_name == 'workflow_dispatch' || + (github.event.action == 'labeled' && github.event.label.name == 'benchmark') || + (github.event.action != 'labeled' && contains(github.event.pull_request.labels.*.name, 'benchmark')) + timeout-minutes: 40 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + # the base revision has to be in the clone for the worktree of bench:cmp + fetch-depth: 0 + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: lts/* + - name: Install + run: npm install --ignore-scripts + - name: Run benchmark + env: + AGAINST: ${{ github.event.pull_request.base.sha || inputs.against }} + run: | + { + echo '' + echo "### Benchmark against \`${AGAINST:0:12}\`" + echo + echo "Node $(node --version) on $RUNNER_OS, from \`npm run bench:cmp\`." + echo + echo '```text' + node benchmark/bench-cmp-branch.js --against "$AGAINST" + echo '```' + } > benchmark_summary.md + - name: Publish the summary + if: always() + run: cat benchmark_summary.md >> "$GITHUB_STEP_SUMMARY" + # the comment on the pull request is posted by benchmark-comment.yml from this artifact: + # the token of a pull request from a fork cannot write comments + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: benchmark-summary + path: benchmark_summary.md diff --git a/benchmark/bench-cmp-branch.js b/benchmark/bench-cmp-branch.js index 1b5c97a6..f04af24b 100644 --- a/benchmark/bench-cmp-branch.js +++ b/benchmark/bench-cmp-branch.js @@ -37,9 +37,11 @@ const BAND = 4 // of A/B in every round, and out of A/A and B/B every four rounds const ORDERS = [[0, 2, 3, 1], [2, 0, 1, 3], [1, 3, 2, 0], [3, 1, 0, 2]] -const greenColor = '\x1b[32m' -const redColor = '\x1b[31m' -const resetColor = '\x1b[0m' +// no colors when the output goes to a file, as in the benchmark workflow +const tty = process.stdout.isTTY +const greenColor = tty ? '\x1b[32m' : '' +const redColor = tty ? '\x1b[31m' : '' +const resetColor = tty ? '\x1b[0m' : '' if (isMainThread) { main().catch((error) => {