Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(entity-card-perf): gate the 100ms p99 on the call's thread CPU time, not wall time
garrytan64605a5d8eda
diff --git a/test/entity-card-perf.slow.test.ts b/test/entity-card-perf.slow.test.tsindex 9240ec944a..89d7e9f7c3 100644--- a/test/entity-card-perf.slow.test.ts+++ b/test/entity-card-perf.slow.test.ts@@ -11,7 +11,12 @@* 1. HARD ABSOLUTE — p99 < 100ms × GBRAIN_TEST_PERF_BUDGET_MULTIPLIER (default 1;* loosen in CI only with evidence of runner noise). The protocol DOC* promises this number; the bound is op-layer latency (transport- * excluded, as documented).+ * excluded, as documented). Latency here is the call's CPU time on this+ * thread (`timed` below): PGLite runs every query in-process on this+ * thread with no I/O, so on an idle machine it equals wall time, and on a+ * loaded CI runner it does not count the time other processes held the+ * CPU. Wall time was measuring the shard's load (p99 100.52ms on a loaded+ * 8-VM shard against ~30ms idle). Wall p99 is still logged.* 2. RATIO GUARD (machine-independent) — entity p99 ≤ 100× max(getPage p50,* 1ms) on the same corpus. Calibration: the card is ~7 indexed reads +* a keyword search on the miss path. It measures ~21× a getPage p50 of@@ -90,6 +95,20 @@ const HUB_RATIO_CEILING = 8;// Catastrophe backstop only; the ratio is the gate.const HUB_ABSOLUTE_CEILING_MS = 1000 * (Number(process.env.GBRAIN_TEST_PERF_BUDGET_MULTIPLIER) || 1);+/**+ * One call's wall time and this thread's CPU time (user + system) across it, in ms. The gates read `cpu`: the+ * code path's own work, including the queries PGLite runs on this thread and GC on it, but not time this+ * process spent waiting for a CPU while other test processes ran.+ */+async function timed(fn: () => Promise<unknown>): Promise<{ wall: number; cpu: number }> {+ const c0 = process.threadCpuUsage();+ const t0 = performance.now();+ await fn();+ const wall = performance.now() - t0;+ const c = process.threadCpuUsage(c0);+ return { wall, cpu: (c.user + c.system) / 1000 };+}+function percentile(sorted: number[], p: number): number {const idx = Math.min(sorted.length - 1, Math.ceil((p / 100) * sorted.length) - 1);return sorted[Math.max(0, idx)];@@ -211,22 +230,22 @@ describe('entity card p99 latency gate', () => {}const samples: number[] = [];+ const walls: number[] = [];for (let i = 0; i < MEASURED; i++) {const name = names[(i * 13) % names.length];- const t0 = performance.now();- await buildEntityCard(engine, 'default', name, { remote: true });- samples.push(performance.now() - t0);+ const t = await timed(() => buildEntityCard(engine, 'default', name, { remote: true }));+ samples.push(t.cpu);+ walls.push(t.wall);}samples.sort((a, b) => a - b);+ walls.sort((a, b) => a - b);const p50 = percentile(samples, 50);const p99 = percentile(samples, 99);- // Ratio baseline: getPage p50 on the same corpus.+ // Ratio baseline: getPage p50 on the same corpus, timed the same way.const pageSamples: number[] = [];for (let i = 0; i < 50; i++) {- const t0 = performance.now();- await engine.getPage(`people/target-person-${i % TARGET_ENTITIES}`, { sourceId: 'default' });- pageSamples.push(performance.now() - t0);+ pageSamples.push((await timed(() => engine.getPage(`people/target-person-${i % TARGET_ENTITIES}`, { sourceId: 'default' }))).cpu);}pageSamples.sort((a, b) => a - b);const pageP50 = Math.max(percentile(pageSamples, 50), 1.0); // 1ms floor vs sub-ms division noise@@ -234,7 +253,7 @@ describe('entity card p99 latency gate', () => {// eslint-disable-next-line no-consoleconsole.log(`[entity-card-perf] corpus=${PAGES}p+${LINKS}l+${ALIASES}a+${FACTS}f ` +- `entity p50=${p50.toFixed(2)}ms p99=${p99.toFixed(2)}ms | getPage p50=${pageP50.toFixed(2)}ms ` ++ `entity cpu p50=${p50.toFixed(2)}ms p99=${p99.toFixed(2)}ms (wall p99=${percentile(walls, 99).toFixed(2)}ms) | getPage p50=${pageP50.toFixed(2)}ms ` +`| ratio=${(p99 / pageP50).toFixed(1)}x (ceiling ${RATIO_CEILING}x) | budget=${P99_BUDGET_MS}ms`,);@@ -251,16 +270,18 @@ describe('entity verb with referenced_by (entity recall)', () => {for (let i = 0; i < TARGET_ENTITIES; i++) names.push(`Target Person ${i}`, `tp${i}`, `zzz-absent-${i}`);for (let i = 0; i < WARMUP; i++) await buildEntityCard(engine, 'default', names[i % names.length], opts);const samples: number[] = [];+ const walls: number[] = [];for (let i = 0; i < MEASURED; i++) {- const t0 = performance.now();- await buildEntityCard(engine, 'default', names[(i * 13) % names.length], opts);- samples.push(performance.now() - t0);+ const t = await timed(() => buildEntityCard(engine, 'default', names[(i * 13) % names.length], opts));+ samples.push(t.cpu);+ walls.push(t.wall);}samples.sort((a, b) => a - b);+ walls.sort((a, b) => a - b);const p50 = percentile(samples, 50);const p99 = percentile(samples, 99);// eslint-disable-next-line no-console- console.log(`[entity-recall-perf] ordinary entity+references p50=${p50.toFixed(2)}ms p99=${p99.toFixed(2)}ms | budget=${P99_BUDGET_MS}ms`);+ console.log(`[entity-recall-perf] ordinary entity+references cpu p50=${p50.toFixed(2)}ms p99=${p99.toFixed(2)}ms (wall p99=${percentile(walls, 99).toFixed(2)}ms) | budget=${P99_BUDGET_MS}ms`);expect(p99).toBeLessThan(P99_BUDGET_MS);}, 300_000);