name: benchmark on: pull_request: workflow_dispatch: inputs: base: description: The branch, tag or commit to compare against # This file's own branch: `next` has the same file with `next` here. default: main # Without this, two runs racing on the same PR can both find no existing # marker comment and each create their own duplicate (post-benchmark-comment.js # does a check-then-act listComments/createComment with no locking). concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true permissions: contents: read pull-requests: write jobs: benchmark: name: benchmark ${{ matrix.name }} strategy: fail-fast: false matrix: include: - os: ubuntu-latest id: ubuntu name: ubuntu title: Ubuntu - os: macos-latest id: macos name: macOS title: macOS - os: windows-latest id: windows name: windows title: Windows runs-on: ${{ matrix.os }} timeout-minutes: 20 defaults: run: shell: bash env: # Where the build-ts-loader action builds the PR branch - on Windows it # copies the checkout to C:\source\ts-loader first. The base branch is # checked out alongside it at "$ROOT-base". ROOT: ${{ matrix.os == 'windows-latest' && 'C:/source/ts-loader' || github.workspace }} steps: - uses: actions/checkout@v7 with: fetch-depth: 0 - uses: actions/setup-node@v7 with: node-version: 24 cache: npm - uses: ./.github/actions/build-ts-loader # Checked out into a sibling worktree rather than a second checkout step # so both builds can run from disk at the same time. - name: checkout base branch into a worktree working-directory: ${{ env.ROOT }} env: # A manual run has no pull request to take the base from. BASE: ${{ github.event.pull_request.base.sha || inputs.base }} run: | # A branch is only known here as origin's. base_sha=$(git rev-parse --verify --quiet "origin/$BASE^{commit}" || git rev-parse --verify "$BASE^{commit}") git worktree add "$ROOT-base" "$base_sha" - name: install & build (base branch) working-directory: ${{ env.ROOT }}-base run: | npm ci npm run build # --root-a is the PR branch built by the build-ts-loader action; # --root-b is the base branch worktree built in the previous step. # The descriptions go under the table, to say what was compared: the # two paths are the same on every run. - name: run benchmark working-directory: ${{ env.ROOT }} env: HEAD_SHA: ${{ github.event.pull_request.head.sha }} BASE_NAME: ${{ github.event.pull_request.base.ref || inputs.base }} run: | base_sha=$(git -C "$ROOT-base" rev-parse HEAD) if [ -n "$HEAD_SHA" ]; then # The checkout of a pull request is its merge with the base. description_a="${HEAD_SHA:0:7} merged into $BASE_NAME" else description_a="$GITHUB_REF_NAME at ${GITHUB_SHA:0:7}" fi npm run benchmark -- \ --root-a "$ROOT" --label-a "PR branch" \ --description-a "$description_a" \ --root-b "$ROOT-base" --label-b "base branch" \ --description-b "$BASE_NAME at ${base_sha:0:7}" - name: write job summary if: always() run: cat "$ROOT/.benchmark/benchmark-results.md" >> "$GITHUB_STEP_SUMMARY" 2>/dev/null || true - name: upload results if: always() uses: actions/upload-artifact@v7 with: name: benchmark-results-${{ matrix.id }} path: ${{ env.ROOT }}/.benchmark/benchmark-results.* if-no-files-found: ignore # Only runs with a writable token - GitHub automatically issues a # read-only GITHUB_TOKEN for fork PRs, so this step is a silent no-op # there. The step summary above still carries the numbers for everyone. - name: comment on PR if: always() && github.event_name == 'pull_request' uses: actions/github-script@v7 env: MARKER: TITLE: Benchmark (${{ matrix.title }}) with: script: | const path = require('path'); const postComment = require(path.join(process.env.GITHUB_WORKSPACE, '.github/scripts/post-benchmark-comment.js')); await postComment({ github, context, resultsPath: path.join(process.env.ROOT, '.benchmark/benchmark-results.md'), marker: process.env.MARKER, title: process.env.TITLE, });