/** * Benchmarks for CodSpeed. * * In CI, the CodSpeed action runs this script under CPU simulation: it counts * the instructions each benchmark executes instead of timing it, so the result * barely depends on the runner and a pull request is compared with the base * branch without the noise of shared runners. Outside the CodSpeed action the * plugin falls back to plain tinybench timing, which is enough to check the * script locally. * * Usage: * pnpm run build * pnpm run codspeed * * Patching uses the fixtures of benchmark/patch-benchmark.mjs. Parse and * stringify use the two spec examples of benchmark/fixture-benchmark.mjs, from * the smol-toml and iarna-toml submodules; the 5 MB document stays in the * timing harness, as a single call of it takes too long under simulation. * Other libraries are not benchmarked here: CodSpeed tracks this package's own * code over time, and the comparisons with them stay in the timing harnesses. */ import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { isDeepStrictEqual } from 'node:util'; import { Bench } from 'tinybench'; import { withCodSpeed } from '@codspeed/tinybench-plugin'; import { parse as parseSmol } from 'smol-toml'; import { patchFixtures } from './patch-fixtures.mjs'; const rootDir = join(fileURLToPath(new URL('.', import.meta.url)), '..'); const importDist = (file) => import(pathToFileURL(join(rootDir, 'dist', file)).href); const full = await importDist('toml-patch.js'); const lite = await importDist('patch-lite.js'); const implementations = [ { name: 'full', patch: full.patch }, { name: 'lite', patch: lite.patch }, ]; // The same documents and options as the fixture benchmark: parse with Date // values, and stringify the object smol-toml parses, as in its README. const documents = [ { name: 'toml-spec-example', file: 'submodules/smol-toml/bench/testfiles/toml-spec-example.toml' }, { name: '0A-spec-01-example-v0.4.0', file: 'submodules/iarna-toml/benchmark/0A-spec-01-example-v0.4.0.toml' }, ].map(({ name, file }) => { const source = readFileSync(join(rootDir, file), 'utf8'); return { name, source, value: parseSmol(source, { useLegacyDate: true, maxDepth: 1010 }) }; }); // Local runs only: CodSpeed runs each benchmark under its own instrument. const bench = withCodSpeed(new Bench({ time: 200, iterations: 5, warmupTime: 50 })); for (const { name, source, value } of documents) { bench.add(`parse: ${name}`, () => { full.parse(source, { temporal: false }); }); bench.add(`stringify: ${name}`, () => { full.stringify(value); }); } for (const [scenario, fixtures] of Object.entries(patchFixtures)) { for (const fixture of fixtures) { const updated = full.parse(fixture.source); fixture.update(updated); for (const { name, patch } of implementations) { // Check the output once, outside the measured function. if (!isDeepStrictEqual(full.parse(patch(fixture.source, updated)), updated)) { throw new Error(`patch (${name}) gave the wrong result for ${scenario} ${fixture.name}`); } bench.add(`patch ${name}: ${scenario} ${fixture.name}`, () => { patch(fixture.source, updated); }); } } } await bench.run(); // Under CodSpeed the plugin measures outside tinybench, which then has no // results of its own and would print a table of `not-started` rows. if (bench.tasks.some((task) => task.result.state === 'completed')) { console.table(bench.table()); }