92 lines · 4.9 KB · TypeScriptRaw
/** * AUDIT D (the row-read rule on the background): an alarm's jobs and the calendar's pass must cost what changed, not * what the account holds. The calendar's classifier read 200k to 400k event rows a pass on an object of 200k events. * * Here: an object of 200k rows across the tables an account of the owner's shape is heavy in (events, their * attendees, notes with people, sessions, messages), all written long ago and settled. One calendar pass, and one * whole alarm, are counted: no statement may read a twentieth of the rows. Synthetic rows only. */import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { personalObject } from "./personal.ts";const SLUG = "alarm-cost";const ROWS = 200_000;const stub = () => env.BUSINESS.get(env.BUSINESS.idFromName(SLUG));type Exec = (...a: unknown[]) => SqlStorageCursor<Record<string, SqlStorageValue>>;type Counted = { cursors: SqlStorageCursor<Record<string, SqlStorageValue>>[]; said: string[] };/** Counts every statement `work` runs on the object's SQL, and answers the total read and the heavy ones. */async function cost(work: (instance: unknown) => Promise<unknown>): Promise<{ rowsRead: number; heavy: { n: number; sql: string }[] }> { return runInDurableObject(stub(), async (instance, state) => { const sql = state.storage.sql as unknown as { exec: Exec; __exec?: Exec }; const exec = (sql.__exec ??= sql.exec.bind(sql)); const counted: Counted = { cursors: [], said: [] }; sql.exec = (...a: unknown[]) => { const cursor = exec(...a); counted.cursors.push(cursor); counted.said.push(String(a[0]).replace(/\s+/g, " ").slice(0, 240)); return cursor; }; try { await work(instance); } finally { sql.exec = exec; } return { rowsRead: counted.cursors.reduce((n, c) => n + c.rowsRead, 0), heavy: counted.cursors.map((c, i) => ({ n: c.rowsRead, sql: counted.said[i]! })).filter((x) => x.n > ROWS / 20), }; });}describe("the background's cost does not grow with the database (AUDIT D)", () => { it("a calendar pass and a whole alarm read a fraction of 200k settled rows", async () => { await personalObject(SLUG, { open: ["event", "event_attendee", "note", "person"] }); const old = "2026-01-01T00:00:00.000Z"; await runInDurableObject(stub(), (_i, state) => { const sql = state.storage.sql; const seed = (table: string, n: number, data: string) => sql.exec( `WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ?) INSERT INTO rows (table_id, id, data, owner, version, deleted, created_at, updated_at) SELECT ?, ? || i, ${data}, NULL, 1, 0, ?, ? FROM n`, n, table, `${table}-`, old, old, ); seed("event", ROWS * 0.4, `'{"title":"Invented meeting ' || i || '","starts_at":"2026-01-01","kind":"meeting"}'`); seed("event_attendee", ROWS * 0.3, `'{"event_id":"event-' || i || '","email":"a' || (i % 500) || '@example.test"}'`); seed("note", ROWS * 0.2, `'{"kind":"note","title":"n","people":["person-' || (i % 50) || '"]}'`); seed("session", ROWS * 0.1, `'{"external_id":"s-' || i || '","title":"Invented session"}'`); }); // Settle what the object does once for new rows (its fills and first passes), as an account long in use has. const started = Date.now(); for (let i = 0; i < 400; i++) { let more = false; const step = await cost(async (instance) => { const b = instance as unknown as { calendarPass(e: null): Promise<{ more: boolean } | null>; store: { listLinksStep(n?: number): boolean; measureStorage(now: Date): boolean } }; const filling = b.store.listLinksStep(5000); const pass = await b.calendarPass(null); // The storage measure: a fixed step of work (WORK_A_STEP) an alarm, begun again only every six hours; settled here. const measuring = b.store.measureStorage(new Date()); more = filling || measuring || Boolean(pass?.more); }); if (i % 20 === 0 || !more) console.log(`ALARM COST settle ${i}: ${Date.now() - started} ms, read ${step.rowsRead}, heavy ${step.heavy.map((h) => `${h.n} ${h.sql.slice(0, 120)}`).join(" | ")}`); if (!more) break; } const pass = await cost((instance) => (instance as { calendarPass(e: null): Promise<unknown> }).calendarPass(null)); for (const h of pass.heavy) console.log(`ALARM COST calendar heavy: ${h.n} rows: ${h.sql}`); console.log(`ALARM COST calendar pass read ${pass.rowsRead} rows`); expect(pass.heavy.map((h) => `${h.n}: ${h.sql}`)).toEqual([]); const alarm = await cost((instance) => (instance as { alarm(): Promise<void> }).alarm()); for (const h of alarm.heavy) console.log(`ALARM COST alarm heavy: ${h.n} rows: ${h.sql}`); console.log(`ALARM COST alarm read ${alarm.rowsRead} rows`); expect(alarm.heavy.map((h) => `${h.n}: ${h.sql}`)).toEqual([]); }, 900_000);});