Cotton code
108 lines · 5.9 KB · TypeScriptRaw
// The fan's budgets as guards (fanperf, owner 15 Oct 2026: "the animations// must never make the app heavy; prove it, keep it proven"). Each test runs// on the one measured run below — scripts/perf/fan.sh on a ProMotion// simulator, synthetic Home, 30 tiles — and fails when its figure is over the// budget the owner set. These are the only timing pins the test policy allows// (11 Oct 2026): they guard budgets the owner named, not a screen or a count.////   pnpm perf:fan                       # build, measure, guard (~15 min, macOS + Xcode)//   FAN_PERF_RUN=<dir> pnpm perf:fan    # guard a finished run without measuring again//   FAN_PERF_GROWTH=<dir> …             # the growth runs growth.py reads (fan.sh --turns)//// Not part of `pnpm test:scripts`: it needs a simulator and the build lock.import { execFileSync } from "node:child_process";import { mkdirSync, mkdtempSync, readFileSync } from "node:fs";import { homedir } from "node:os";import { join } from "node:path";import { fileURLToPath } from "node:url";import { beforeAll, describe, expect, it } from "vitest";interface Figure { budget_name: string; value: number | string | null; budget: number | string; ok: boolean; unit: string; note: string; trace: string; runtime: string; known?: string }const here = fileURLToPath(new URL(".", import.meta.url));let figures: Map<string, Figure>;beforeAll(() => {  let run = process.env.FAN_PERF_RUN;  if (!run) {    const captures = join(homedir(), "cotton-captures/fan-perf");    mkdirSync(captures, { recursive: true });    run = mkdtempSync(join(captures, "guard-"));    try {      execFileSync("bash", [join(here, "fan.sh"), "--out", run], { stdio: "inherit" });    } catch {      // Over budget exits 1; the figures below say which.    }  }  const report = JSON.parse(readFileSync(join(run, "fan.json"), "utf8")) as { figures: Figure[] };  figures = new Map(report.figures.map((figure) => [figure.budget_name, figure]));}, 40 * 60 * 1000);function within(name: string) {  const figure = figures.get(name);  expect(figure, `${name} was measured`).toBeDefined();  expect(figure!.ok, `${name}: ${figure!.value} ${figure!.unit} against ${figure!.budget} on iOS ${figure!.runtime || "unknown"} (${figure!.note}; trace ${figure!.trace})${figure!.known ? ` — ${figure!.known}` : ""}`).toBe(true);}describe("the fan is light", () => {  // RULE: The fan is measured on the runtime the owner's phone runs (iOS 26.5): its memory grows 403 MB over 100 turns there and −1 MB on iOS 27 at the same commit (fanperf2, 15 Oct 2026), so no budget passes on another runtime.  it("is measured on the owner's runtime", () => {    within("runtime");  });  // RULE: Turning the fan never costs a frame: no frame's work over 8.3 ms (one 120 Hz frame) and p99 under 6 ms over 20 s of swiping with 30 tiles.  it("turns within a 120 Hz frame", () => {    within("turns logged");    within("one app process");    within("frame work max");    within("frame work p99");  });  // RULE: The fan folding into its circle and blooming back never costs a frame either: no frame's work over 8.3 ms while the page is scrolled down and up (fancollapse, 15 Oct 2026).  it("folds and blooms within a 120 Hz frame", () => {    within("fold frame work max");  });  // RULE: A fan at rest costs nothing: no Core Animation commit, no CPU beyond the idle floor and no main-thread work over 30 s showing — no timer, no display link, no redraw.  it("costs nothing at rest", () => {    within("rest commits");    within("rest CPU");    within("rest main-thread samples");  });  // RULE: A record open on a fan card's page costs nothing at rest either (architect, 15 Oct 2026): no commit, no CPU beyond the floor, no main-thread work while it stands untouched.  it("costs nothing with a record open on its page", () => {    within("record rest commits");    within("record rest CPU");    within("record rest main-thread samples");  });  // RULE: Page pictures stay within fan.md's bound (about 150 KB compressed per tile).  it("holds its pictures within the bound", () => {    within("page pictures");  });  // RULE: Turning does not grow the app (architect, 15 Oct 2026): on iOS 26.5 and the newest runtime, with the shipped values and the owner's, one run at 300 turns grows under 6 MB and no more than the median of five runs at 100 turns — a leak scales with turns, noise does not; one run is a footprint sample minus another, which swing 4–10 MB, so the 100-turn median is reported by growth.py as the noise floor and is not a gate. FAN_PERF_GROWTH names the folder of runs growth.py reads.  it("does not grow over turns", () => {    const root = process.env.FAN_PERF_GROWTH;    expect(root, "FAN_PERF_GROWTH names a folder of runs: <values>/<run>, five at 100 turns and one at 300 per runtime (growth.py)").toBeDefined();    try {      execFileSync("python3", [join(here, "growth.py"), root!], { stdio: "inherit" });    } catch {      // Over budget exits 1; growth.json says which.    }    const growth = JSON.parse(readFileSync(join(root!, "growth.json"), "utf8")) as { ok: boolean; known: string; rows: { runtime: string; values: string; median_100: number | null; growth_300: number | null; note: string }[] };    const rows = growth.rows.map((row) => `iOS ${row.runtime} ${row.values}: 300 turns ${row.growth_300 ?? "—"} MB against a noise floor of ${row.median_100 ?? "—"} MB ${row.note}`).join("; ");    expect(growth.ok, `${rows}${growth.known ? ` — ${growth.known}` : ""}`).toBe(true);  });  // RULE: A minute of swiping keeps the app's energy impact Low.  it("keeps energy impact low while swiping", () => {    within("energy impact");  });  // RULE: The fan does not slow the app's start: its cold start stays within the grid Home's + 5%, both launched alternately in one run on one simulator (architect, 15 Oct 2026; the absolute figure is reported, not guarded).  it("starts as fast as Home did", () => {    within("cold start");  });});