Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(perf): time entity-resolve shapes interleaved so one runner stall cannot cover every fast sample (master red 4.61x)

garrytan8fed28eba3ef
diff --git a/test/entity-resolve-perf.slow.test.ts b/test/entity-resolve-perf.slow.test.ts
index 78a6593ce0..ccbcc64219 100644
--- a/test/entity-resolve-perf.slow.test.ts
+++ b/test/entity-resolve-perf.slow.test.ts
@@ -25,6 +25,7 @@
import { describe, it, expect, beforeAll, afterAll } from 'bun:test';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { interleavedMedians } from './helpers/interleaved-timing.ts';
let engine: PGLiteEngine;
@@ -33,7 +34,7 @@ let engine: PGLiteEngine;
const PAGES = 5_000;
const LINKS = 50_000;
const CHUNKS = 25_000;
-const RUNS = 5; // median of 5 runs per shape
+const RUNS = 5; // median of 5 interleaved runs per shape
beforeAll(async () => {
engine = new PGLiteEngine();
@@ -185,25 +186,16 @@ async function timeQuery(sql: string): Promise<number> {
return elapsed;
}
-function median(xs: number[]): number {
- const sorted = [...xs].sort((a, b) => a - b);
- const mid = Math.floor(sorted.length / 2);
- return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid];
-}
-
describe('tryPrefixExpansion perf regression — NEW shape >= 5x faster than OLD', () => {
it('correlated subqueries beat derived-table aggregation by 5x or more', async () => {
// Warm both shapes once so the planner caches its plan, then measure.
await timeQuery(OLD_SQL);
await timeQuery(NEW_SQL);
- const oldTimes: number[] = [];
- const newTimes: number[] = [];
- for (let i = 0; i < RUNS; i++) oldTimes.push(await timeQuery(OLD_SQL));
- for (let i = 0; i < RUNS; i++) newTimes.push(await timeQuery(NEW_SQL));
-
- const oldMedian = median(oldTimes);
- const newMedian = median(newTimes);
+ // Alternate the shapes: the NEW block is only ~5ms long, so timed as a
+ // block one runner stall covers all of its samples (see interleaved-timing.ts).
+ const { first: oldMedian, second: newMedian } = await interleavedMedians(RUNS,
+ () => timeQuery(OLD_SQL), () => timeQuery(NEW_SQL));
const speedup = oldMedian / newMedian;
// Emit timing data to stderr so a regression review can see the actual
diff --git a/test/helpers/interleaved-timing.ts b/test/helpers/interleaved-timing.ts
new file mode 100644
index 0000000000..3821c51c45
--- /dev/null
+++ b/test/helpers/interleaved-timing.ts
@@ -0,0 +1,22 @@
+/**
+ * Shape-ratio perf guards time a slow shape against a fast one. Timed in two
+ * blocks, the fast block is only a few milliseconds long, so one short stall
+ * (a GC pause, CPU steal on a shared runner) covers every one of its samples
+ * and the median moves with it, while the slow block loses at most one sample.
+ * Alternating the shapes leaves a stall that short on one sample of each.
+ */
+export function median(xs: number[]): number {
+ const sorted = [...xs].sort((a, b) => a - b);
+ const mid = Math.floor(sorted.length / 2);
+ return sorted.length % 2 === 0 ? (sorted[mid - 1]! + sorted[mid]!) / 2 : sorted[mid]!;
+}
+
+export async function interleavedMedians(runs: number, first: () => Promise<number>, second: () => Promise<number>): Promise<{ first: number; second: number }> {
+ const a: number[] = [];
+ const b: number[] = [];
+ for (let i = 0; i < runs; i++) {
+ a.push(await first());
+ b.push(await second());
+ }
+ return { first: median(a), second: median(b) };
+}
diff --git a/test/interleaved-timing.test.ts b/test/interleaved-timing.test.ts
new file mode 100644
index 0000000000..78b5adc73d
--- /dev/null
+++ b/test/interleaved-timing.test.ts
@@ -0,0 +1,34 @@
+import { describe, expect, test } from 'bun:test';
+import { interleavedMedians } from './helpers/interleaved-timing.ts';
+
+// A synthetic clock: the slow shape costs 20ms and the fast one 1ms (the
+// entity-resolve-perf fixture's usual medians), and every call that starts
+// inside one stall window runs 5x slower. Master CI saw exactly this once:
+// old_median=24.60ms new_median=5.34ms speedup=4.61x against a usual ~1ms.
+function stalledClock(stallStart: number, stallLength: number) {
+ let now = 0;
+ const call = (cost: number) => async () => {
+ const stalled = now >= stallStart && now < stallStart + stallLength;
+ const elapsed = stalled ? cost * 5 : cost;
+ now += elapsed;
+ return elapsed;
+ };
+ return { slow: call(20), fast: call(1) };
+}
+
+describe('interleavedMedians', () => {
+ test('a stall long enough to cover every fast sample timed as a block cannot move the ratio, wherever it lands', async () => {
+ const runs = 5;
+ const window = runs * 1 * 5;
+ for (let stallStart = 0; stallStart <= runs * 21 * 5; stallStart += 0.5) {
+ const clock = stalledClock(stallStart, window);
+ const { first, second } = await interleavedMedians(runs, clock.slow, clock.fast);
+ expect(first / second).toBeGreaterThanOrEqual(15);
+ }
+ });
+
+ test('medians of an unstalled run are the per-call costs', async () => {
+ const clock = stalledClock(Number.POSITIVE_INFINITY, 0);
+ expect(await interleavedMedians(5, clock.slow, clock.fast)).toEqual({ first: 20, second: 1 });
+ });
+});