Cotton code
68 lines · 4.1 KB · TypeScriptRaw
/** * LIVE CPU (coordinator, 23 Nov 2026: his object reset for exceeding its CPU, 18:13 to 18:17 UTC). Every step a request * or the alarm runs, timed on an invented object of his shape. `BIG_OBJECT=1` builds it at full size. */import { env, runDurableObjectAlarm, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { BIG, personalShape, writeShape } from "./big_object.ts";import { http, personalObject } from "./personal.ts";const SCALE = BIG ? 1.25 : 1 / 100;describe("the object's own work at his size", { timeout: 1_800_000 }, () => {  it("times each step a request or the alarm runs", async () => {    const out: string[] = [];    const slug = "live-cpu-big";    const { credential } = await personalObject(slug);    const stub = env.BUSINESS.get(env.BUSINESS.idFromName(slug));    await runInDurableObject(stub, (_i, s) => {      const last = writeShape(s.storage.sql, personalShape(SCALE));      s.storage.sql.exec("UPDATE meta SET value = CAST(? AS TEXT) WHERE key = 'version'", last);    });    // row_counts counted, as on a live object long since (the alarm's recount).    await runInDurableObject(stub, (instance, s) => {      const store = (instance as unknown as { store: { carryCounts(steps: number, tx: (step: () => void) => void): boolean } }).store;      while (store.carryCounts(50, (step) => s.storage.transactionSync(step)));    });    const size = await runInDurableObject(stub, (_i, s) => s.storage.sql.databaseSize);    out.push(`file ${(size / 1e6).toFixed(0)} MB`);    const phone = { authorization: `Bearer ${credential}` };    const time = async (label: string, run: () => Promise<unknown>) => {      const t0 = Date.now();      await run();      out.push(`${label}: ${Date.now() - t0} ms`);    };    await time("GET /v1/send/inbox", () => http(slug, "GET", "/v1/send/inbox", undefined, phone));    // Every statement the next request runs, with the rows it read, the most first.    await runInDurableObject(stub, (_i, st) => {      const sql = st.storage.sql as unknown as { exec: (q: string, ...a: unknown[]) => { rowsRead: number } & Iterable<unknown>; seen?: [string, () => number][] };      const real = sql.exec.bind(sql);      sql.seen = [];      sql.exec = (q: string, ...a: unknown[]) => {        const c = real(q, ...a);        sql.seen!.push([q.replace(/\s+/g, " ").slice(0, 160), () => c.rowsRead]);        return c;      };    });    await http(slug, "GET", "/v1/sync?since=0", undefined, phone);    const seen = await runInDurableObject(stub, (_i, st) => {      const sql = st.storage.sql as unknown as { seen: [string, () => number][]; exec: unknown };      const all = sql.seen.map(([q, n]) => [q, n()] as [string, number]);      return { count: all.length, top: all.sort((x, y) => y[1] - x[1]).slice(0, 15) };    });    out.push(`sync since=0 ran ${seen.count} statements; most rows read:`);    for (const [q, n] of seen.top) out.push(`  ${n} ${q}`);    await time("GET /v1/send/inbox again", () => http(slug, "GET", "/v1/send/inbox", undefined, phone));    await time("GET /v1/chat/inbox", () => http(slug, "GET", "/v1/chat/inbox", undefined, phone));    await time("GET /v1/sync", () => http(slug, "GET", "/v1/sync?since=0", undefined, phone));    await time("alarm (nothing asked)", () => runDurableObjectAlarm(stub));    await time("a list of message in order (asks its order index)", () => runInDurableObject(stub, (instance: unknown) => (instance as { store: { selectOrdered(t: string, w: unknown[], o: string, l: number): unknown } }).store.selectOrdered("message", [], "updated_at DESC, id", 20)));    await time("alarm (builds the order index)", () => runDurableObjectAlarm(stub));    // The kit kept again, as on the first request after a deploy whose kit moved.    await runInDurableObject(stub, (_i, s) => s.storage.sql.exec("UPDATE meta SET value = 'old' WHERE key = 'kit_version'"));    await time("first request after a kit change (keepKit)", () => http(slug, "GET", "/v1/send/inbox", undefined, phone));    console.log(out.join("\n"));    expect(out.length).toBeGreaterThan(0);  });});