Skip to content

Commit 822c20f

Browse files
committed
test(oc-path): measure synchronous worker CPU budgets
1 parent 332b1a5 commit 822c20f

1 file changed

Lines changed: 24 additions & 22 deletions

File tree

‎extensions/oc-path/src/oc-path/tests/scenarios/perf-determinism.test.ts‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
// OC Path tests cover perf determinism plugin behavior.
21
import { describe, expect, it } from "vitest";
32
import { emitMd } from "../../emit.js";
43
import { parseMd } from "../../parse.js";
54
import { resolveMdOcPath as resolveOcPath } from "../../resolve.js";
65

7-
const perfBudgetMultiplier = process.env.CI ? 4 : 1;
8-
const perfSampleCount = 3;
6+
const cpuBudgetMultiplier = process.env.CI ? 4 : 1;
97

10-
function expectWithinPerfBudget(run: () => void, localBudgetMs: number) {
11-
// Loaded shared-vCPU CI can pause any single sample, so use the best of a few runs.
12-
// The minimum still catches consistently slow regressions without treating contention as one.
13-
let bestElapsedMs = Number.POSITIVE_INFINITY;
14-
for (let sample = 0; sample < perfSampleCount; sample++) {
15-
const start = performance.now();
16-
run();
17-
bestElapsedMs = Math.min(bestElapsedMs, performance.now() - start);
18-
}
19-
expect(bestElapsedMs).toBeLessThan(localBudgetMs * perfBudgetMultiplier);
8+
function expectWithinCpuBudget(run: () => void, localCpuBudgetMs: number) {
9+
// Budget this worker's synchronous compute, excluding off-CPU waits and CPU
10+
// consumed by other Vitest workers in the same process.
11+
const start = process.threadCpuUsage();
12+
run();
13+
const { user, system } = process.threadCpuUsage(start);
14+
expect((user + system) / 1000).toBeLessThan(localCpuBudgetMs * cpuBudgetMultiplier);
2015
}
2116

22-
describe("perf + determinism", () => {
23-
it("parses 100 KB file within the parser budget", () => {
17+
describe("CPU budgets + determinism", () => {
18+
it("does not charge off-CPU waits to the synchronous work budget", () => {
19+
const waitState = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));
20+
expectWithinCpuBudget(() => {
21+
Atomics.wait(waitState, 0, 0, 250);
22+
}, 50);
23+
});
24+
25+
it("parses 100 KB file within the parser CPU budget", () => {
2426
const lines: string[] = [];
2527
for (let i = 0; i < 1000; i++) {
2628
lines.push("## H" + i);
@@ -29,23 +31,23 @@ describe("perf + determinism", () => {
2931
}
3032
}
3133
const raw = lines.join("\n");
32-
expectWithinPerfBudget(() => parseMd(raw), 200);
34+
expectWithinCpuBudget(() => parseMd(raw), 200);
3335
});
3436

35-
it("parses 1000 small files in under 500 ms", () => {
37+
it("parses 1000 small files within a 500 ms CPU budget", () => {
3638
const raw = `## H\n- a\n- b: c\n## I\n- d\n`;
37-
expectWithinPerfBudget(() => {
39+
expectWithinCpuBudget(() => {
3840
for (let i = 0; i < 1000; i++) {
3941
parseMd(raw);
4042
}
4143
}, 500);
4244
});
4345

44-
it("100k OcPath resolutions on parsed AST in under 500 ms", () => {
46+
it("resolves 100k OcPaths on parsed AST within a 500 ms CPU budget", () => {
4547
const raw = `## A\n- a1\n- a2\n## B\n- b1\n- b2\n## C\n- c1: cv\n`;
4648
const { ast } = parseMd(raw);
4749
const path = { file: "X.md", section: "b", item: "b1" };
48-
expectWithinPerfBudget(() => {
50+
expectWithinCpuBudget(() => {
4951
for (let i = 0; i < 100_000; i++) {
5052
resolveOcPath(ast, path);
5153
}
@@ -108,7 +110,7 @@ describe("perf + determinism", () => {
108110
expect(ast.blocks[0]?.items.map((i) => i.text)).toEqual(["z", "a", "m"]);
109111
});
110112

111-
it("large fixture round-trip stays under 100 ms", () => {
113+
it("large fixture round-trip stays within a 100 ms CPU budget", () => {
112114
const lines: string[] = [];
113115
for (let i = 0; i < 500; i++) {
114116
lines.push(`## Section ${i}`);
@@ -120,7 +122,7 @@ describe("perf + determinism", () => {
120122
}
121123
const raw = lines.join("\n");
122124
let out = "";
123-
expectWithinPerfBudget(() => {
125+
expectWithinCpuBudget(() => {
124126
const { ast } = parseMd(raw);
125127
out = emitMd(ast);
126128
}, 100);

0 commit comments

Comments
 (0)