// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 // Time limits that mean the same thing on every machine. A test that says "this takes under 2 seconds" is there to // catch work that grows with the square of its input (seconds become minutes), and its limit was written on one // machine. On CI's runner, a small virtual machine that runs eight test files at once, the same correct code took 4 // to 12 times the limits, and six tests failed with nothing wrong. So a limit here is: // // - measured in the processor time this process used (process.cpuUsage), not time on the clock, so the other test // files that share the processors do not count against it; // - multiplied by this machine's pace: how many times longer a fixed piece of work (`work` below) takes here than // the REFERENCE_MS it took where the limits were written. The pace is taken before and after what is timed, and // the larger counts. It is never below 1, so no limit gets tighter on a faster machine, and on the machine the // limits were written on they are what they were. // // A failure says the time, the limit and the pace. PPLUS_PACE_LOG=1 prints every measurement that passed too. import assert from 'node:assert/strict'; /** Milliseconds `work()` takes, at its best, on the machine the tests' limits were written on. */ export const REFERENCE_MS = 2.3; /** A little of what the engines do: cut names out of text, make small objects, count in a Map, sort. */ function work() { let sum = 0; const names = new Map(); for (let i = 0; i < 40000; i++) { const word = `v${i % 997}`; const node = { kind: 'name', text: word, line: i, at: [i, i + word.length] }; names.set(word, (names.get(word) ?? 0) + node.at[1] - node.at[0]); sum += word.charCodeAt(word.length - 1); } const list = [...names].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1)); return sum + list.length; } /** Processor time this process has used, in milliseconds (every thread of it: the collector and the compiler too). */ const cpuMs = () => { const u = process.cpuUsage(); return (u.user + u.system) / 1000; }; let warm = false; /** How many times longer the fixed work takes here, now, than on the reference machine; at least 1. */ export function pace() { if (!warm) { for (let i = 0; i < 20; i++) work(); warm = true; } let best = Infinity; for (let i = 0; i < 5; i++) { const t0 = cpuMs(); work(); best = Math.min(best, cpuMs() - t0); } return Math.max(1, best / REFERENCE_MS); } /** The processor time `fn` takes, in milliseconds, and the pace around it. */ export function measure(fn) { const before = pace(); const t0 = cpuMs(); fn(); const ms = cpuMs() - t0; return { ms, pace: Math.max(before, pace()) }; } /** Whether a measurement is inside a limit written for the reference machine, as a message when it is not. */ export function over(m, limitMs, what) { const line = `${what}: ${m.ms.toFixed(1)} ms of processor time; the limit is ${limitMs} ms${m.pace > 1 ? ` x ${m.pace.toFixed(1)}, this machine's pace (tests/helpers/pace.mjs)` : ''}`; if (process.env.PPLUS_PACE_LOG) console.log(`pace: ${line}`); return m.ms < limitMs * m.pace ? null : line; } /** Run `fn` and fail unless it took under `limitMs` of processor time at this machine's pace. */ export function within(limitMs, what, fn) { const failed = over(measure(fn), limitMs, what); assert.ok(!failed, failed ?? ''); } /** * For the prover in a test: no time on the clock for a routine. Its counted budgets then decide alone (docs/limits.md), * so what a test finds proved, open or broken is the same on every machine. With the Prove tab's 3 seconds, a slow * runner left open what the tests expect proved. */ export const NO_CLOCK = Object.freeze({ budgetMs: Infinity });