Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/index-list-bullets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@doc-kit/core': patch
---

Read `index` files whose list items start with `-` or `+`, not only `*`
64 changes: 39 additions & 25 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,60 @@ on:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
# Runs on main are the baselines pull requests compare against
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions:
contents: read

jobs:
# A pull request runs the targets its changes can affect, and CodSpeed
# reports the others from the base's run
plan:
name: Plan
runs-on: ubuntu-latest
outputs:
targets: ${{ steps.affected.outputs.targets }}
steps:
- name: Setup environment
uses: nodejs/web-team/actions/setup-environment@4c0b18db11a8357664683d70ef1f27af08ecc766
with:
use-version-file: true
pnpm: true
# A pull request's merge commit and its base
fetch-depth: 2

- name: Find affected targets
id: affected
env:
SINCE: ${{ github.event_name == 'pull_request' && 'HEAD^1' || '' }}
run: echo "targets=$(node bench/affected.mjs $SINCE)" >> "$GITHUB_OUTPUT"

benchmarks:
name: Benchmarks
name: ${{ matrix.target }} (${{ matrix.mode }})
needs: plan
if: needs.plan.outputs.targets != '[]'
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
contents: read
id-token: write
env:
NODE_SHA: 768315cf7165e9ff9b636844b776488e9d1703fc
id-token: write # Uploads results to CodSpeed
strategy:
fail-fast: false
matrix:
target: ${{ fromJSON(needs.plan.outputs.targets) }}
mode: [simulation, memory]
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

# Valgrind can't reserve the address space V8 guards WebAssembly memory
# with, so bounds are checked inline instead
- name: Run benchmarks
uses: CodSpeedHQ/action@af75f77f17a054769abefaa42fe8dc2975469b5f # v5.6.3
uses: CodSpeedHQ/action@ca909ca8b60401c041f627a1d9117aa90dce9524 # v5.8.1
with:
mode: simulation,memory
run: node --run bench
mode: ${{ matrix.mode }}
run: node --disable-wasm-trap-handler bench/generate.bench.mjs ${{ matrix.target }}
6 changes: 6 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@
"es2026": true,
"browser": true
}
},
{
"files": ["docs/showcase/*.js"],
"env": {
"commonjs": true
}
}
]
}
8 changes: 5 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,14 @@ 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`
out at `NODE_SHA` into `./node`, and the suite reads that directory. It runs on
every pull request and reports CPU and memory there.
(`bench/generate.bench.mjs`), on one thread. Each target in
`bench/doc-kit.config.mjs` builds the [showcase](docs/showcase/index.md), which is also on the website. A
pull request measures only the targets its changes can affect
(`bench/affected.mjs`), and CodSpeed reports the others from the base's run.

```bash
node --run bench
node --run bench -- html legacy-html
```

## Code Quality
Expand Down
64 changes: 64 additions & 0 deletions bench/affected.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Prints, as a JSON array, the targets of `doc-kit.config.mjs` that the changes
// since a commit can affect, or all of them without one.
//
// node bench/affected.mjs [commit]

import { execFileSync } from 'node:child_process';
import { readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';

import { loadGenerators } from '@doc-kit/core/generators/loader.mjs';

import config from './doc-kit.config.mjs';

const ROOT = join(import.meta.dirname, '..');

// Changes to these can affect every target
const SHARED =
/^(bench|docs\/showcase)\/|^(package\.json|pnpm-lock\.yaml|pnpm-workspace\.yaml|\.nvmrc|\.github\/workflows\/codspeed\.yml)$/;

// Changes to these affect no target
const INERT = /(^|\/)__tests__\/|\.(md|d\.ts)$/;

const [since] = process.argv.slice(2);
let affected = config.target;

if (since) {
const changed = execFileSync(
'git',
['diff', '--name-only', '--no-renames', since],
{ cwd: ROOT, encoding: 'utf-8' }
)
.split('\n')
.filter(Boolean);

if (!changed.some(file => SHARED.test(file))) {
const relevant = changed.filter(file => !INERT.test(file));

// Each workspace package's directory, by package name
const directories = new Map(
readdirSync(join(ROOT, 'packages')).map(dir => [
JSON.parse(
readFileSync(join(ROOT, 'packages', dir, 'package.json'), 'utf-8')
).name,
`packages/${dir}/`,
])
);

affected = [];

for (const target of config.target) {
// The packages of the target's generator and of those it depends on
const generators = await loadGenerators([target]);
const owners = [...generators.keys()].map(specifier =>
directories.get(specifier.split('/', 2).join('/'))
);

if (relevant.some(file => owners.some(dir => file.startsWith(dir)))) {
affected.push(target);
}
}
}
}

console.log(JSON.stringify(affected));
31 changes: 31 additions & 0 deletions bench/doc-kit.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { join } from 'node:path';

const SHOWCASE = join(import.meta.dirname, '..', 'docs', 'showcase');

export default {
target: [
'json',
'json-simple',
'legacy-json',
'legacy-html',
'html',
'llms-txt',
'orama-db',
'man-page',
'addon-verify',
'api-links',
],

global: {
input: [join(SHOWCASE, '*.md')],
index: join(SHOWCASE, 'index.md'),
},

'ast-js': {
input: [join(SHOWCASE, '*.js')],
},

'man-page': {
envVarsHeaderSlug: 'environment-variables',
},
};
64 changes: 25 additions & 39 deletions bench/generate.bench.mjs
Original file line number Diff line number Diff line change
@@ -1,66 +1,52 @@
import { mkdir, rm } from 'node:fs/promises';
import path from 'node:path';
import { rm } from 'node:fs/promises';
import { join } 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);
import config from './doc-kit.config.mjs';

const shared = {
threads: 1,
index: at('doc/api/index.md'),
typeMap: at('doc/type-map.json'),
changelog: at('CHANGELOG.md'),
};

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

const targets = [
['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 configFile = join(import.meta.dirname, 'doc-kit.config.mjs');
const targets = process.argv.length > 2 ? process.argv.slice(2) : config.target;

const bench = withCodSpeed(
new Bench({ warmup: false, iterations: 1, time: 0, throws: true })
);

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

bench.add(name, () => runGenerators(config), {
// tinybench calls a function that isn't `async` to see if it returns a
// promise, before `beforeAll` has set `runGenerators`
bench.add(name, async () => runGenerators(options), {
beforeAll: async () => {
await rm(output, { recursive: true, force: true });
await mkdir(output, { recursive: true });
({ runGenerators } = createGenerator());
config = await setConfig({
...shared,
// CodSpeed's CPU simulation runs one thread at a time
options = await setConfig({
configFile,
target: [name],
input: [input],
output,
threads: 1,
});
},
beforeEach: () => {
start = performance.now();
},
afterEach: () => {
console.log(
`${name}: ${((performance.now() - start) / 1000).toFixed(2)}s`
);
},
afterAll: () => rm(output, { recursive: true, force: true }),
});
}

await bench.run();

if (process.env.CODSPEED_ENV === undefined) {
console.table(bench.table());
}
Loading
Loading