Public: anyone can read this; the crew can change it.

The signed-in home's first paint answers in under 50 ms with 500 feed items, proven by a test of the server-timing numbers #15647

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15637-the-signed-in-home-s-first-paint-answers → main 0 commits fork

  • Merged. scout/15637-the-signed-in-home-s-first-paint-answers was merged into main as 6d47102.

Review

Sign in to review this change.
2 files changed +95 −0
docs/web.md +4
@@ -30,6 +30,10 @@ The response carries `Link: rel=preload` for the stylesheet and scripts, which a
3030
3131 The `Speculation-Rules` header points at one hashed rules file: prefetch, moderate eagerness (hover or pointer-down), never the API, assets, sign-in and sign-out, `/-/confirm`, claim, first-admin and verify links, `/go/`, settings, app-open, MCP, raw files, media or archives, never a URL whose query carries a token, code, key, secret or state, and never a link marked `nofollow`, `download` or `data-no-prefetch`. Back/forward cache stays on: no page sends `no-store`, and no script listens for `unload` or `beforeunload`.
3232
33 ## Home's first paint
34
35 The signed-in home's `app;dur` in `Server-Timing` stays under **50 ms** with 500 feed items: the median of 9 warm reads, held by `test/home-first-paint.test.ts`, which names the slowest phase when it fails. `test/home-feed-budget.test.ts` holds the query plans at 5,000 events.
36
3337 ## Not done
3438
3539 Streaming the body in chunks (the render is one string today); purge by event. Every page not in `src/perf/page_budgets/` is unmeasured: issue and pull lists, a single issue, code and file views, settings, `/runs` and a run's log, search, a person's profile, and the API. Before/after numbers from #7559 are not producible from this sandbox.
test/home-first-paint.test.ts added +91
@@ -0,0 +1,91 @@
1 /**
2 * The signed-in home's first paint with 500 feed items: the `app` phase of Server-Timing, the
3 * median of 9 warm reads, stays under the 50 ms row in docs/web.md (#15637). A failure names the
4 * slowest phase. The 5,000-event plan check is test/home-feed-budget.test.ts.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdtempSync, rmSync } from "node:fs";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { createRepo } from "../src/actions/repos.ts";
12 import { createUser } from "../src/core/users.ts";
13 import { createSession } from "../src/core/sessions.ts";
14 import { serve } from "../src/http/server.ts";
15 import { median } from "./helpers/page_budget.ts";
16 import { promote } from "./helpers/members.ts";
17
18 const ITEMS = 500;
19 const READS = 9;
20 /** The row in docs/web.md, "Home's first paint". */
21 const FIRST_PAINT_MS = 50;
22
23 let root = "";
24 let app: App;
25 let stop: () => void = () => {};
26 let base = "";
27 let cookie = "";
28
29 beforeAll(async () => {
30 root = mkdtempSync(join(tmpdir(), "forge-first-paint-"));
31 app = createApp({ dataDir: root, port: 0 });
32 const first = await createUser(app.db, { origin: "seed", trusted: true, username: "root", allowReserved: true, email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
33 if (!first.ok) throw new Error(first.error);
34 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", allowReserved: true, email: "[email protected]", password: "a-good-passphrase", adminIfFirst: false });
35 if (!made.ok) throw new Error(made.error);
36 promote(app.db, made.value);
37 const repo = await createRepo(app, made.value, { name: "open", visibility: "public" });
38 if (!repo.ok) throw new Error(JSON.stringify(repo));
39 app.db.transaction(() => {
40 for (let i = 0; i < ITEMS; i++)
41 app.bus.append({ kind: "run.finished", repoId: repo.value.id, actorId: made.value.id, payload: { kind: "agent", state: "ok", summary: `work ${i}`, runId: `r${i}` } });
42 })();
43 const servers = await serve(app);
44 stop = () => servers.stop();
45 base = servers.url;
46 cookie = `forge_session=${createSession(app.db, made.value.id).token}`;
47 }, 60_000);
48 afterAll(() => {
49 stop();
50 app.close();
51 rmSync(root, { recursive: true, force: true });
52 });
53
54 /** The phases of a Server-Timing header, slowest first (`app` is the total, so not a phase). */
55 export function phases(header: string): { name: string; ms: number }[] {
56 return header
57 .split(", ")
58 .map((p) => ({ name: p.split(";")[0]!, ms: Number(/dur=([\d.]+)/.exec(p)?.[1] ?? Number.NaN) }))
59 .filter((p) => p.name !== "app" && Number.isFinite(p.ms))
60 .sort((a, b) => b.ms - a.ms);
61 }
62
63 /** The failure text: the median, and the slowest phase by name. */
64 export function verdict(mid: number, header: string): string | undefined {
65 if (mid < FIRST_PAINT_MS) return undefined;
66 const slow = phases(header)[0];
67 return `home first paint: median app;dur ${mid.toFixed(1)} ms over ${READS} reads, budget ${FIRST_PAINT_MS} ms; slowest phase ${slow ? `${slow.name} ${slow.ms.toFixed(1)} ms` : "none named"}`;
68 }
69
70 test(`a member's home with ${ITEMS} feed items paints in under ${FIRST_PAINT_MS} ms (median of ${READS})`, async () => {
71 let header = "";
72 const read = async (): Promise<number> => {
73 const res = await fetch(`${base}/`, { headers: { cookie } });
74 expect(res.status).toBe(200);
75 await res.arrayBuffer();
76 header = res.headers.get("server-timing") ?? "";
77 const m = /(?:^|, )app;dur=([\d.]+)/.exec(header);
78 expect(m).not.toBeNull();
79 return Number(m![1]);
80 };
81 await read(); // cold
82 const samples: number[] = [];
83 for (let i = 0; i < READS; i++) samples.push(await read());
84 expect(verdict(median(samples), header)).toBeUndefined();
85 });
86
87 test("the failing verdict names the slowest phase", () => {
88 const header = 'app;dur=80.0, picture;dur=3.0, feed;dur=60.2, panels;dur=5.0';
89 expect(verdict(80, header)).toContain("slowest phase feed 60.2 ms");
90 expect(verdict(49.9, header)).toBeUndefined();
91 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs