Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

entity-card-perf: run latency contracts alone on a ci:ubicloud VM; collect setup garbage before measuring

garrytane824b0b70920
diff --git a/scripts/ci-ubicloud.ts b/scripts/ci-ubicloud.ts
index 572115b953..632f76ca16 100755
--- a/scripts/ci-ubicloud.ts
+++ b/scripts/ci-ubicloud.ts
@@ -29,7 +29,7 @@
* pgvector server + PgBouncer per slot, PGLite snapshots), streamed over SSH
* while the checkout uploads. The first VM to
* finish setup runs the machine-level items in order (gitleaks, verify, the
- * serial lane's machine-exclusive files) and then joins the pool. Every other
+ * serial and slow lanes' machine-exclusive files) and then joins the pool. Every other
* test file is an item in one global heaviest-first queue that idle slots pull
* from (scripts/ubicloud/schedule.ts); items run through the same wrappers
* ci:local uses (scripts/ubicloud/ci-item.sh). Durations of every run are
@@ -212,10 +212,11 @@ async function main() {
}
const exclusive = lines(sh("bash", ["scripts/run-serial-tests.sh", "--dry-run-list-exclusive"]));
const serialPool = lines(sh("bash", ["scripts/run-serial-tests.sh", "--dry-run-list"])).filter((f) => !exclusive.includes(f));
+ const slowExclusive = lines(sh("bash", ["scripts/run-slow-tests.sh", "--dry-run-list-exclusive"]));
const inventory: Record<Lane, string[]> = {
unit: lines(sh("bash", ["scripts/run-unit-shard.sh", "--dry-run-list"])),
serial: serialPool,
- slow: lines(sh("bash", ["scripts/run-slow-tests.sh", "--dry-run-list"])),
+ slow: lines(sh("bash", ["scripts/run-slow-tests.sh", "--dry-run-list"])).filter((f) => !slowExclusive.includes(f)),
e2e: e2eFiles,
};
@@ -236,6 +237,7 @@ async function main() {
if (opts.lanes.has("gitleaks")) control.push({ lane: "gitleaks", files: [] });
if (opts.lanes.has("verify")) control.push({ lane: "verify", files: [] });
if (opts.lanes.has("serial") && exclusive.length) control.push({ lane: "serial", files: exclusive });
+ if (opts.lanes.has("slow") && slowExclusive.length) control.push({ lane: "slow", files: slowExclusive });
const totalItems = queue.length + control.reduce((n, c) => n + Math.max(c.files.length, 1), 0);
const estimate = queue.reduce((s, i) => s + i.weight, 0) / 1000;
log(`${totalItems} items (${Object.entries(inventory).filter(([l]) => opts.lanes.has(l)).map(([l, f]) => `${l} ${f.length}`).join(", ")}, control ${control.map((c) => c.lane).join("+") || "none"}); ~${Math.round(estimate)}s of weighted work`);
diff --git a/scripts/run-slow-tests.sh b/scripts/run-slow-tests.sh
index 0e6f929ee5..e727d3c206 100755
--- a/scripts/run-slow-tests.sh
+++ b/scripts/run-slow-tests.sh
@@ -16,13 +16,24 @@ unset DATABASE_URL GBRAIN_DATABASE_URL
unset GBRAIN_HOME
cd "$(dirname "$0")/.."
-# --dry-run-list prints the selection; positional FILE arguments replace
-# discovery (scripts/ci-ubicloud.ts dispatches explicit batches).
-DRY_RUN=0
-if [ "${1:-}" = "--dry-run-list" ]; then
- DRY_RUN=1
- shift
-fi
+# --dry-run-list prints the selection; --dry-run-list-exclusive prints only
+# the machine-exclusive entries. Positional FILE arguments replace discovery
+# (scripts/ci-ubicloud.ts dispatches explicit batches).
+DRY_RUN_MODE=""
+case "${1:-}" in
+ --dry-run-list|--dry-run-list-exclusive) DRY_RUN_MODE="$1"; shift ;;
+esac
+
+# EXCLUSIVE_FILES: latency contracts that must run on an otherwise idle
+# machine, the way GitHub CI runs them on a dedicated perf runner.
+# scripts/ci-ubicloud.ts runs them as control items on a VM with no other
+# work; this script's own lane already runs files one at a time.
+EXCLUSIVE_FILES=(
+ # entity() p99 < 100ms on a 20K-page corpus. Next to 7 heavy items on a
+ # 16-vCPU VM the whole process runs 1.6-2.6x slower (thread CPU time
+ # rises with wall time, so no in-process clock can separate it).
+ "test/entity-card-perf.slow.test.ts"
+)
slow_files=()
if [ "$#" -gt 0 ]; then
@@ -33,10 +44,18 @@ else
done < <(find test -name '*.slow.test.ts' -not -path 'test/e2e/*' | sort)
fi
-if [ "$DRY_RUN" = "1" ]; then
+if [ "$DRY_RUN_MODE" = "--dry-run-list" ]; then
if [ "${#slow_files[@]}" -gt 0 ]; then printf '%s\n' "${slow_files[@]}"; fi
exit 0
fi
+if [ "$DRY_RUN_MODE" = "--dry-run-list-exclusive" ]; then
+ for f in ${slow_files[@]+"${slow_files[@]}"}; do
+ for e in "${EXCLUSIVE_FILES[@]}"; do
+ [ "$f" != "$e" ] || printf '%s\n' "$f"
+ done
+ done
+ exit 0
+fi
. scripts/lib/test-env.sh
receipts_init slow
diff --git a/scripts/ubicloud/README.md b/scripts/ubicloud/README.md
index 99c8e35eeb..aafe1a3087 100644
--- a/scripts/ubicloud/README.md
+++ b/scripts/ubicloud/README.md
@@ -85,7 +85,9 @@ the VM running the machine-level items below counts only once it joins the
pool.
The first VM to finish setup runs the
machine-level work first: gitleaks, `verify`, then the serial lane's
-machine-exclusive files one at a time with nothing else on that VM. After that
+machine-exclusive files and the slow lane's latency contracts
+(`run-slow-tests.sh --dry-run-list-exclusive`, the files GitHub CI runs on a
+dedicated perf runner) one at a time with nothing else on that VM. After that
it joins the pool. Before `verify`, it uploads the incremental typecheck state
(`node_modules/.cache/gbrain-typecheck.tsbuildinfo`, which the checkout
tarball never carries). The orchestrator builds that state with
diff --git a/test/entity-card-perf.slow.test.ts b/test/entity-card-perf.slow.test.ts
index 89d7e9f7c3..31d3705002 100644
--- a/test/entity-card-perf.slow.test.ts
+++ b/test/entity-card-perf.slow.test.ts
@@ -53,6 +53,7 @@
*/
import { describe, it, expect, beforeAll, afterAll } from 'bun:test';
+import { heapSize } from 'bun:jsc';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { buildEntityCard } from '../src/core/verbs/entity-card.ts';
import {
@@ -95,18 +96,24 @@ 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);
+interface Sample { name: string; wall: number; cpu: number; heapDeltaMB: number }
+
/**
- * 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.
+ * One call's wall time, this thread's CPU time (user + system) across it, in ms, and the JS heap change (a large
+ * change means a GC ran inside it). 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();
+async function sample(name: string, fn: () => Promise<unknown>): Promise<Sample> {
+ const h0 = heapSize(), c0 = process.threadCpuUsage(), t0 = performance.now();
await fn();
- const wall = performance.now() - t0;
- const c = process.threadCpuUsage(c0);
- return { wall, cpu: (c.user + c.system) / 1000 };
+ const wall = performance.now() - t0, c = process.threadCpuUsage(c0);
+ return { name, wall, cpu: (c.user + c.system) / 1000, heapDeltaMB: (heapSize() - h0) / 2 ** 20 };
+}
+
+/** The five slowest samples for the log: cpu well under wall is scheduler wait, a heap jump is a GC, cpu≈wall with neither is a slower CPU. */
+function slowest(samples: Sample[]): string {
+ return [...samples].sort((a, b) => b.cpu - a.cpu).slice(0, 5)
+ .map(s => `${s.name} wall=${s.wall.toFixed(1)} cpu=${s.cpu.toFixed(1)} heapΔ=${s.heapDeltaMB.toFixed(1)}MB`).join(' | ');
}
function percentile(sorted: number[], p: number): number {
@@ -229,23 +236,23 @@ describe('entity card p99 latency gate', () => {
await buildEntityCard(engine, 'default', names[i % names.length], { remote: true });
}
- const samples: number[] = [];
- const walls: number[] = [];
+ Bun.gc(true);
+ const runs: Sample[] = [];
for (let i = 0; i < MEASURED; i++) {
const name = names[(i * 13) % names.length];
- const t = await timed(() => buildEntityCard(engine, 'default', name, { remote: true }));
- samples.push(t.cpu);
- walls.push(t.wall);
+ runs.push(await sample(name, () => buildEntityCard(engine, 'default', name, { remote: true })));
}
- samples.sort((a, b) => a - b);
- walls.sort((a, b) => a - b);
+ // eslint-disable-next-line no-console
+ console.log(`[entity-card-perf] slowest: ${slowest(runs)}`);
+ const samples = runs.map(r => r.cpu).sort((a, b) => a - b);
+ const walls = runs.map(r => r.wall).sort((a, b) => a - b);
const p50 = percentile(samples, 50);
const p99 = percentile(samples, 99);
// Ratio baseline: getPage p50 on the same corpus, timed the same way.
const pageSamples: number[] = [];
for (let i = 0; i < 50; i++) {
- pageSamples.push((await timed(() => engine.getPage(`people/target-person-${i % TARGET_ENTITIES}`, { sourceId: 'default' }))).cpu);
+ pageSamples.push((await sample('getPage', () => 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
@@ -269,15 +276,16 @@ describe('entity verb with referenced_by (entity recall)', () => {
const names: string[] = [];
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[] = [];
+ Bun.gc(true);
+ const runs: Sample[] = [];
for (let i = 0; i < MEASURED; i++) {
- const t = await timed(() => buildEntityCard(engine, 'default', names[(i * 13) % names.length], opts));
- samples.push(t.cpu);
- walls.push(t.wall);
+ const name = names[(i * 13) % names.length];
+ runs.push(await sample(name, () => buildEntityCard(engine, 'default', name, opts)));
}
- samples.sort((a, b) => a - b);
- walls.sort((a, b) => a - b);
+ // eslint-disable-next-line no-console
+ console.log(`[entity-recall-perf] slowest: ${slowest(runs)}`);
+ const samples = runs.map(r => r.cpu).sort((a, b) => a - b);
+ const walls = runs.map(r => r.wall).sort((a, b) => a - b);
const p50 = percentile(samples, 50);
const p99 = percentile(samples, 99);
// eslint-disable-next-line no-console
diff --git a/test/scripts/ci-ubicloud-schedule.test.ts b/test/scripts/ci-ubicloud-schedule.test.ts
index 8f2a1f6fb8..afb4cde330 100644
--- a/test/scripts/ci-ubicloud-schedule.test.ts
+++ b/test/scripts/ci-ubicloud-schedule.test.ts
@@ -165,6 +165,14 @@ describe("explicit-file seams in the ci:local wrappers", () => {
expect(list("run-slow-tests.sh", "--dry-run-list", "test/x.slow.test.ts")).toEqual(["test/x.slow.test.ts"]);
});
+ it("run-slow-tests.sh exposes its exclusive subset of discovery", () => {
+ const exclusive = list("run-slow-tests.sh", "--dry-run-list-exclusive");
+ expect(exclusive).toContain("test/entity-card-perf.slow.test.ts");
+ const all = list("run-slow-tests.sh", "--dry-run-list");
+ for (const file of exclusive) expect(all).toContain(file);
+ expect(list("run-slow-tests.sh", "--dry-run-list-exclusive", "test/x.slow.test.ts")).toEqual([]);
+ });
+
it("run-serial-tests.sh lists positional files and exposes its exclusive subset", () => {
expect(list("run-serial-tests.sh", "--dry-run-list", "test/x.serial.test.ts")).toEqual(["test/x.serial.test.ts"]);
const exclusive = list("run-serial-tests.sh", "--dry-run-list-exclusive");