Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CodSpeed

on:
push:
branches: [main]
pull_request:
branches: [main]
# Lets CodSpeed replay a run when it needs a baseline.
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read

jobs:
benchmarks:
name: Benchmarks
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
env:
NODE_SHA: 768315cf7165e9ff9b636844b776488e9d1703fc
Comment thread
avivkeller marked this conversation as resolved.
Resolved
steps:
- name: Harden Runner
uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
with:
egress-policy: audit

- name: Setup environment
uses: nodejs/web-team/actions/setup-environment@4c0b18db11a8357664683d70ef1f27af08ecc766
with:
use-version-file: true
pnpm: true

- name: Checkout Node.js sources
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
repository: nodejs/node
ref: ${{ env.NODE_SHA }}
sparse-checkout: |
doc
lib
.
path: node

- name: Run benchmarks
uses: CodSpeedHQ/action@af75f77f17a054769abefaa42fe8dc2975469b5f # v5.6.3
Comment thread
avivkeller marked this conversation as resolved.
Resolved
with:
mode: simulation,memory

@ovflowd ovflowd Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory here is allocations on a warm build, not the peak RSS anyone actually hits. Not sure what we want those numbers to tell us

run: node --run bench
5 changes: 1 addition & 4 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ jobs:

- name: Generate ${{ matrix.target }}
run: |
/usr/bin/time \
--output out/benchmark.json \
--format '{"elapsedSeconds": %e, "userCpuSeconds": %U, "systemCpuSeconds": %S, "maxRssKiB": %M}' \
node packages/cli/bin/cli.mjs generate \
node packages/cli/bin/cli.mjs generate \
Comment thread
avivkeller marked this conversation as resolved.
Resolved
-t ${{ matrix.target }} \
-i "${{ matrix.input }}" \
-o out \
Expand Down
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Thank you for your interest in contributing to the `doc-kit` project! We welcome
- [Best Practices](#best-practices)
- [Example Test File](#example-test-file)
- [Running Tests](#running-tests)
- [Benchmarks](#benchmarks)
- [Code Quality](#code-quality)
- [Linting and Formatting](#linting-and-formatting)
- [Pre-commit Hooks](#pre-commit-hooks)
Expand Down Expand Up @@ -336,6 +337,17 @@ node --run test:coverage
node --test packages/core/src/utils/__tests__/parser.test.mjs
```

## Benchmarks

Generation speed and memory are measured with [CodSpeed](https://codspeed.io)
(`bench/generate.bench.mjs`), on one thread. The workflow checks `nodejs/node`

@ovflowd ovflowd Nov 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: say this is single threaded, so a flat result doesn't read as no change

out at `NODE_SHA` into `./node`, and the suite reads that directory. It runs on
every pull request and reports CPU and memory there.

```bash
node --run bench
```

## Code Quality

### Linting and Formatting
Expand Down
66 changes: 66 additions & 0 deletions bench/generate.bench.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { mkdir, rm } from 'node:fs/promises';
import path from 'node:path';

import { withCodSpeed } from '@codspeed/tinybench-plugin';
import createGenerator from '@doc-kit/core';
import logger from '@doc-kit/core/logger/index.mjs';
import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs';
import { Bench } from 'tinybench';

logger.setLogLevel('warn');

const node = path.join(import.meta.dirname, '../node');
const at = (...parts) => path.join(node, ...parts);

const shared = {
threads: 1,
Comment thread
avivkeller marked this conversation as resolved.
Resolved
index: at('doc/api/index.md'),
typeMap: at('doc/type-map.json'),
changelog: at('CHANGELOG.md'),
};

const docs = at('doc/api/*.md');

const targets = [
Comment thread
avivkeller marked this conversation as resolved.
Resolved
['json', docs],
['json-simple', docs],
['legacy-json', docs],
['legacy-html', docs],
['html', docs],
['llms-txt', docs],
['orama-db', docs],
['man-page', at('doc/api/cli.md')],
['addon-verify', at('doc/api/addons.md')],
['api-links', at('lib/*.js')],
];

const bench = withCodSpeed(
new Bench({ warmup: false, iterations: 1, time: 0, throws: true })
Comment thread
avivkeller marked this conversation as resolved.
Resolved
);

for (const [name, input] of targets) {
const output = path.join(import.meta.dirname, '../out', name);
let runGenerators;
let config;

bench.add(name, () => runGenerators(config), {
beforeAll: async () => {
await rm(output, { recursive: true, force: true });
await mkdir(output, { recursive: true });
({ runGenerators } = createGenerator());
config = await setConfig({
...shared,
target: [name],
input: [input],
output,
});
},
afterAll: () => rm(output, { recursive: true, force: true }),
});
}

await bench.run();

if (process.env.CODSPEED_ENV === undefined) {
console.table(bench.table());
}
42 changes: 9 additions & 33 deletions docs/contributing/comparators.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
A comparator is a small Node.js script that compares one generator's output on
`main` with its output on a pull request and prints a Markdown report. CI
collects these reports into a comment on the pull request, so you and the
reviewers can see what a change does to the generated docs, and to generation
time and memory, before it lands.
reviewers can see what a change does to the generated docs before it lands.

Comparators live in `scripts/comparators/` and run only in this repository's
CI. They are not part of any published package.
Expand All @@ -28,11 +27,6 @@ Two workflows work together:
request (tagged `compared`, so later pushes update it). If every report is
empty, it posts nothing.

Each generation runs under `/usr/bin/time`, which writes `out/benchmark.json`
with the elapsed time, user and system CPU time, and peak resident memory.
Comparators read it for their performance section and leave it out of the
output comparison.

Matrix entries without `compare` (at the moment `man-page`, `addon-verify` and
`json-simple`) are generated and uploaded, but not compared.

Expand All @@ -43,11 +37,6 @@ Matrix entries without `compare` (at the moment `man-page`, `addon-verify` and
| `file-size` | Files added, removed or changed in size, largest change first, with the net change, in a collapsible table | [`html`](../generators/html.md), [`orama-db`](../generators/orama-db.md), [`llms-txt`](../generators/llms-txt.md), [`legacy-html`](../generators/legacy-html.md) |
| `object-assertion` | Files added or removed, and an `assert.deepStrictEqual` diff for each file whose parsed JSON differs, one per file | [`json`](../generators/json.md), [`api-links`](../generators/api-links.md), [`legacy-json`](../generators/legacy-json.md) |

Both end with a **Performance estimate** section: the generation time and peak
memory of each side, read from `benchmark.json`. It comes from a single CI run,
so treat it as a rough signal. It's left out when either side has no
`benchmark.json`.

Keep their limits in mind when you pick one:

- `file-size` compares sizes only. An edit that keeps a file's size the same
Expand All @@ -57,28 +46,25 @@ Keep their limits in mind when you pick one:

## Shared helpers

| Import | From | What it gives you |
| ----------------------------------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `BASE`, `HEAD` | `scripts/constants.mjs` | The two build directories: `base/` and `out/` at the repository root (both gitignored), unless the `BASE` and `HEAD` environment variables point elsewhere. |
| `TITLE` | `scripts/constants.mjs` | The report heading, ``## `<GENERATOR>` Generator``, built from the `GENERATOR` environment variable, which CI sets to the target name. Set the `TITLE` variable to replace it. |
| `BENCHMARK_FILE`, `COMPARISON_FILE` | `scripts/constants.mjs` | The metadata file names, `benchmark.json` and `comparison.txt`. |
| `listOutputFiles(directory)` | `scripts/comparators/files.mjs` | Every file under `directory`, recursively, as sorted relative paths, without the two metadata files. |
| `comparePerformance()` | `scripts/comparators/performance.mjs` | The performance section as Markdown, or an empty string when either side has no `benchmark.json`. It throws if a benchmark value isn't a finite number. |
| Import | From | What it gives you |
| ---------------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `BASE`, `HEAD` | `scripts/constants.mjs` | The two build directories: `base/` and `out/` at the repository root (both gitignored), unless the `BASE` and `HEAD` environment variables point elsewhere. |
| `TITLE` | `scripts/constants.mjs` | The report heading, ``## `<GENERATOR>` Generator``, built from the `GENERATOR` environment variable, which CI sets to the target name. Set the `TITLE` variable to replace it. |
| `COMPARISON_FILE` | `scripts/constants.mjs` | The report's file name, `comparison.txt`. |
| `listOutputFiles(directory)` | `scripts/comparators/files.mjs` | Every file under `directory`, recursively, as sorted relative paths, without `comparison.txt`. |

## Writing a comparator

A comparator is an ES module in `scripts/comparators/`. Its file name without
`.mjs` is the value of the `compare` field in the workflow matrix. It should:

- Read the two builds from `BASE` and `HEAD`, listing their files with
`listOutputFiles`, which skips `benchmark.json` and the `comparison.txt`
that the report is being written to.
`listOutputFiles`, which skips the `comparison.txt` that the report is
being written to.
- Print Markdown to standard output, starting with `TITLE`, and print nothing
when there's nothing to report.
- Report differences instead of failing on them. A comparator that exits with
an error fails the CI job, so throw only when something is actually broken.
- End with `comparePerformance()`, like the built-in comparators, so the
timing stays visible.

### Create the script

Expand All @@ -91,7 +77,6 @@ import { join } from 'node:path';

import { BASE, HEAD, TITLE } from '../constants.mjs';
import { listOutputFiles } from './files.mjs';
import { comparePerformance } from './performance.mjs';

const [baseFiles, headFiles] = await Promise.all(
[BASE, HEAD].map(directory => listOutputFiles(directory))
Expand Down Expand Up @@ -127,12 +112,6 @@ if (changes.length) {
);
}

const performance = await comparePerformance();

if (performance) {
sections.push(performance);
}

if (sections.length) {
console.log(`${TITLE}\n\n${sections.join('\n\n')}\n`);
}
Expand All @@ -155,9 +134,6 @@ node packages/cli/bin/cli.mjs generate -t man-page -i ../node/doc/api/cli.md -o
GENERATOR=man-page node scripts/comparators/changed-files.mjs
```

Local builds have no `benchmark.json`, so the report has no performance
section.

### Add a test

Comparator tests live in `scripts/__tests__/comparators.test.mjs` and run with
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"test:ci": "c8 --reporter=lcov node --test --experimental-test-module-mocks \"packages/*/src/**/*.test.mjs\" \"scripts/**/*.test.mjs\" --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=junit --test-reporter-destination=junit.xml --test-reporter=spec --test-reporter-destination=stdout",
"test:update-snapshots": "node --test --experimental-test-module-mocks --test-update-snapshots \"packages/*/src/**/*.test.mjs\" \"scripts/**/*.test.mjs\"",
"test:watch": "node --test --experimental-test-module-mocks --watch \"packages/*/src/**/*.test.mjs\" \"scripts/**/*.test.mjs\"",
"bench": "node bench/generate.bench.mjs",
"test:e2e": "playwright test",
"prepare": "husky || exit 0",
"run": "node packages/cli/bin/cli.mjs",
Expand All @@ -36,6 +37,8 @@
"devDependencies": {
"@changesets/changelog-github": "^1.0.1",
"@changesets/cli": "^3.0.3",
"@codspeed/tinybench-plugin": "6.1.0",
"@doc-kit/core": "workspace:*",
"@node-core/doc-kit": "workspace:*",
"@playwright/test": "~1.65.1",
"@reporters/github": "~2.1.4",
Expand All @@ -50,6 +53,7 @@
"lint-staged": "17.6.2",
"oxfmt": "0.80.0",
"oxlint": "1.95.0",
"tinybench": "6.2.3",
"wrangler": "4.168.0"
}
}
Loading
Loading