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

The signed-out home renders in under 30 ms and under 30 KB of HTML, measured on production #13014

Merged opened this

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

Merged

torque/forge:torque/12986-the-signed-out-home-renders-in-under-30 → main 7 commits fork 3 threads · 3 unresolved

  • Merged. torque/12986-the-signed-out-home-renders-in-under-30 was merged into main as 8c8369f.

Review

Sign in to review this change.
4 files changed +233 −55
src/web/studio/home.ts +29 −53
@@ -17,6 +17,7 @@ import {
1717 type FrontPicksJson,
1818 } from "../../actions/front.ts";
1919 import { catalog, refreshCatalogQuietly } from "../../packs/catalog.ts";
20 import type { Pack } from "../../packs/pack.ts";
2021 import { appsRuntime, type EnvState } from "../../apps/runtime.ts";
2122 import { accountsClosedIn } from "../../core/users.ts";
2223 import { plansRow } from "../plans_row.ts";
@@ -516,42 +517,16 @@ function tile(entry: FrontEntry, at: number, accounts: boolean, attrs?: Html): H
516517 const href = repoBase(repo);
517518 const open = tileHref(entry);
518519 const said = state === undefined ? undefined : DOOR_STATE[state];
519 return card(
520 html`<p class="door-kick"><span>${String(at + 1).padStart(2, "0")}</span>${when(said, (s) => html` / ${s}`)}</p>
521 ${when(
522 picture,
523 (src) =>
524 html`<a
525 class="look"
526 href="${open ?? href}"
527 tabindex="-1"
528 aria-hidden="true"
529 ${open === undefined ? null : html`rel="nofollow"`}
530 ><img
531 src="${src}"
532 width="${LOOK_SIZES.thumb.width}"
533 height="${LOOK_SIZES.thumb.height}"
534 alt=""
535 loading="lazy"
536 decoding="async"
537 /></a>`,
538 )}
539 <div class="card-body stack stack-tight">
540 <a href="${href}"><strong>${repo.name}</strong></a>
541 <span class="muted door-line" title="${repo.description}"
542 >${repo.description !== "" ? repo.description : `By ${repo.ownerName}`}</span
543 >
544 <p class="door-verbs">
545 ${when(
546 open,
547 (to) => html`<a class="door-verb-first" href="${to}" rel="nofollow" aria-label="Open ${repo.name}">Open</a>`,
548 )}
549 <a href="${href}" aria-label="The code of ${repo.name}">Code</a>
550 ${helpDoor(repo, accounts)}
551 </p>
552 </div>`,
553 { pad: false, ...(attrs === undefined ? {} : { attrs }) },
554 );
520 // One line a part: the shelf repeats this per tile, and its whitespace is bytes on the wire
521 // (#12986).
522 return html`<div class="card"${attrs === undefined ? null : html` ${attrs}`}><p class="door-kick"><span>${String(at + 1).padStart(2, "0")}</span>${when(said, (s) => html` / ${s}`)}</p>${when(
523 picture,
524 (src) =>
525 html`<a class="look" href="${open ?? href}" tabindex="-1" aria-hidden="true"${open === undefined ? null : html` rel="nofollow"`}><img src="${src}" width="${LOOK_SIZES.thumb.width}" height="${LOOK_SIZES.thumb.height}" alt="" loading="lazy" decoding="async"></a>`,
526 )}<div class="card-body stack stack-tight"><a href="${href}"><strong>${repo.name}</strong></a><span class="muted door-line" title="${repo.description}">${repo.description !== "" ? repo.description : `By ${repo.ownerName}`}</span><p class="door-verbs">${when(
527 open,
528 (to) => html`<a class="door-verb-first" href="${to}" rel="nofollow" aria-label="Open ${repo.name}">Open</a>`,
529 )}<a href="${href}" aria-label="The code of ${repo.name}">Code</a>${helpDoor(repo, accounts)}</p></div></div>`;
555530 }
556531
557532 /** A figure in the hero's live panel, left out when it is zero. */
@@ -617,6 +592,9 @@ function doorPulse(days: FrontPage["pulse"]["days"]): Html | null {
617592 if (total === 0) return null;
618593 const peak = Math.max(...days.map((d) => d.n));
619594 const height = 120;
595 // A quiet day still holds its place on the baseline: all in one path, not a rect each (#12986).
596 const active = days.map((d, at) => ({ d, at })).filter(({ d }) => d.n > 0);
597 const quiet = days.map((d, at) => (d.n === 0 ? `M${at * 10 + 1} ${height - 1}h8v1h-8z` : "")).join("");
620598 return html`<figure class="door-pulse">
621599 <svg
622600 viewBox="0 0 ${days.length * 10} ${height}"
@@ -624,18 +602,12 @@ function doorPulse(days: FrontPage["pulse"]["days"]): Html | null {
624602 role="img"
625603 aria-label="${total} changes finished in ${days.length} days, at most ${peak} in one day"
626604 >
627 ${days.map((d, at) => {
628 // A quiet day still holds its place on the baseline.
629 const h = d.n === 0 ? 1 : Math.max(3, Math.round((d.n / peak) * height));
630 return html`<rect
631 class="${d.n === 0 ? "door-pulse-quiet" : ""}"
632 x="${at * 10 + 1}"
633 y="${height - h}"
634 width="8"
635 height="${h}"
636 ><title>${d.day}: ${d.n}</title></rect
637 >`;
638 })}
605 ${quiet === "" ? null : html`<path class="door-pulse-quiet" d="${quiet}" />`}${active.map(
606 ({ d, at }) => {
607 const h = Math.max(3, Math.round((d.n / peak) * height));
608 return html`<rect x="${at * 10 + 1}" y="${height - h}" width="8" height="${h}"><title>${d.day}: ${d.n}</title></rect>`;
609 },
610 )}
639611 </svg>
640612 <figcaption class="muted small">
641613 Every bar is one day of finished work here: ${total.toLocaleString("en-US")} changes in ${days.length} days.
@@ -643,6 +615,9 @@ function doorPulse(days: FrontPage["pulse"]["days"]): Html | null {
643615 </figure>`;
644616 }
645617
618 /** How many ready-made forges the door shows; the rest are one link away. */
619 const DOOR_PACKS = 3;
620
646621 const FORGE_REPO = UPSTREAM_REPO;
647622
648623 /**
@@ -651,9 +626,9 @@ const FORGE_REPO = UPSTREAM_REPO;
651626 * later "host it for me" action sits beside "See what it brings"; it is empty
652627 * today, so there is no dead button.
653628 */
654 function packPresses(): Html | null {
655 const { packs } = catalog();
656 if (packs.length === 0) return null;
629 export function packPresses(all: readonly Pack[]): Html | null {
630 if (all.length === 0) return null;
631 const packs = all.slice(0, DOOR_PACKS);
outdated

The quiet-day path loses the per-day <title>, so a keyboard or screen-reader person cannot read a quiet day; the svg aria-label carries the total, so this is acceptable, but say so.

657632 const count = (n: number, one: string): string => `${n} ${one}${n === 1 ? "" : "s"}`;
658633 return html`<div class="door-packs" aria-label="Ready-made forges">
659634 <p class="door-kick">Start from a ready-made forge</p>
@@ -670,6 +645,7 @@ function packPresses(): Html | null {
670645 </article>`,
671646 )}
672647 </div>
648 ${when(all.length > packs.length, () => html`<p class="door-more"><a href="/packs">All ${all.length} ready-made forges</a></p>`)}
673649 </div>`;
674650 }
675651
@@ -830,7 +806,7 @@ export function studioDoor(ctx: Ctx): Response {
830806 </ul>
831807 </section>
832808
833 <div class="door-section">${homeMilestones(ctx)}</div>
809 <div class="door-section">${homeMilestones(ctx, undefined, visitor)}</div>
834810
835811 <section class="door-section" id="yours">
836812 <div class="stack">
@@ -845,7 +821,7 @@ export function studioDoor(ctx: Ctx): Response {
845821 cd forge
846822 bun install
847823 bun run src/main.ts</code></pre>
848 ${packPresses()}
824 ${packPresses(catalog().packs)}
849825 </div>
850826 </section>
851827 <p class="door-org">
src/web/studio/milestone_lines.ts +42 −2
@@ -17,6 +17,7 @@ import { html, type Html } from "../html.ts";
1717 import { row } from "../ui.ts";
1818 import type { User } from "../../core/users.ts";
1919 import { milestoneHref, planFor } from "../pages/milestones.ts";
20 import { during } from "../../perf/jobs.ts";
2021 import { appReuse } from "./app_cache.ts";
2122 import { appsOf, writableAppsOf } from "./asks.ts";
2223
@@ -41,6 +42,45 @@ export function biggestMilestones(
4142 const readBiggest = (ctx: Pick<Ctx, "app">, repos: readonly Repo[], count: number): Line[] =>
4243 biggestOf(repos.slice(0, REPOS_READ), count, (repo) => planFor(ctx, repo, false, new Date(), false));
4344
45 /** How long the first paint shows lines read before; then they are read again afterwards. */
46 const LATER_MS = 60_000;
47 type Kept = { key: string; lines: Line[]; at: number; reading: boolean };
48 const kept = new WeakMap<Pick<Ctx, "app">["app"]["db"], Kept>();
49
50 /**
51 * The lines for a stranger's first paint (#12986): the last reading while it is under a
52 * minute old, or older but still the same apps, in which case it is read again once the
53 * response is gone. A plan stamp is tens of milliseconds after any write, and a stranger's
54 * page never waits for one; only the very first reading is taken in the request.
55 *
56 * Accepted staleness: the lines a stranger sees were read at most one visit ago — over a
57 * minute old when the visit before was long ago — and the refresh runs only when someone
58 * visits, so a quiet forge shows old lines until then. A failed refresh keeps the old lines
59 * and tries again on the next visit. They are a front-door summary; the plan's own pages
60 * are always read fresh.
61 */
62 export function milestonesLater(ctx: Pick<Ctx, "app">, repos: readonly Repo[], now = Date.now()): Line[] {
63 const key = repos.slice(0, REPOS_READ).map((r) => r.id).join(",");
64 const db = ctx.app.db;
65 const have = kept.get(db);
66 if (have === undefined || have.key !== key) {
67 const lines = readBiggest(ctx, repos, FRONT_MILESTONES);
68 kept.set(db, { key, lines, at: now, reading: false });
69 return lines;
70 }
71 if (now - have.at >= LATER_MS && !have.reading) {
72 have.reading = true;
73 setTimeout(() => during("milestone-lines", () => {
74 try {
75 kept.set(db, { key, lines: readBiggest(ctx, repos, FRONT_MILESTONES), at: Date.now(), reading: false });
outdated

The refresh swallows errors silently and only runs when someone visits; the stranger's page can be arbitrarily stale. Name the accepted staleness or tie the refresh to the live milestones push.

76 } catch {
77 have.reading = false;
78 }
79 }, undefined, ctx.app), 0);
80 }
81 return have.lines;
82 }
83
4484 /** What a line says after its name: how far, what landed, the estimate if there is one. */
4585 export function lineFacts(r: Line, scoped: boolean): string {
4686 const w = r.limb.weight;
@@ -75,14 +115,14 @@ export function frontApps(ctx: Pick<Ctx, "app">, user: User | undefined): Repo[]
75115 }
76116
77117 /** The home's region: the viewer's apps, or the public ones for a visitor. */
78 export function homeMilestones(ctx: Ctx, apps?: readonly Repo[]): Html {
118 export function homeMilestones(ctx: Ctx, apps?: readonly Repo[], later = false): Html {
79119 const repos = apps ?? frontApps(ctx, ctx.auth.user);
80120 return html`<div
81121 id="live-milestones"
82122 data-live="${MILESTONE_LIVE}"
83123 data-live-src="/live/milestones"
84124 >
85 ${lines(biggestMilestones(ctx, repos), false)}
125 ${lines(later ? milestonesLater(ctx, repos) : biggestMilestones(ctx, repos), false)}
86126 </div>`;
87127 }
88128
test/job-slices.test.ts +1
@@ -253,6 +253,7 @@ const UNTIMED: Record<string, string> = {
253253 lane: "a closure in startChecks; starting the checks service starts cell checks — follow-up",
254254 "lane-catch-up": "a closure in startChecks, once on boot; same as lane",
255255 "verify-sweep": "a closure in startChecks, off unless verifyEveryMs is set; same as lane",
256 "milestone-lines": "a one-shot timer in web/studio/milestone_lines.ts, armed when the home's milestone lines are over a minute old: one read of the biggest milestones, off the response path; perf-home-signed-out.test.ts drives milestonesLater",
256257 looks: "a closure in startLooks; needs the apps runtime and chromium, and the fixture has no apps",
257258 "look-browser-idle": "kills a browser kept five seconds for a picture that did not come (looks/capture.ts): one SIGKILL and a profile removed off this thread; needs chromium",
258259 "perf-watcher": "a closure in startPerfWatcher over its own in-memory record; the fixture has no FORGE_REPO to file in",
test/perf-home-signed-out.test.ts added +161
@@ -0,0 +1,161 @@
1 /**
2 * The signed-out home is the fastest page (#12986): its `render` phase stays
3 * under 30 ms (median of 20 renders) and its HTML under 30 KB, on a forge
4 * seeded the way a busy one is: many public apps with plans that moved this
5 * week, merged changes (merged across the last 90 days).
6 */
7 import { afterAll, beforeAll, expect, spyOn, test } from "bun:test";
8 import { mkdirSync, writeFileSync } from "node:fs";
9 import { mkdtemp, rm } from "node:fs/promises";
10 import { tmpdir } from "node:os";
11 import { join } from "node:path";
12 import { linkIssue } from "../src/actions/dependencies.ts";
13 import { openIssue } from "../src/actions/issues.ts";
14 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
15 import { createApp, type App } from "../src/app.ts";
16 import { AppsRuntime, type EnvStatus } from "../src/apps/runtime.ts";
17 import { recordLook } from "../src/core/looks.ts";
18 import { encodePng } from "../src/design/image/png.ts";
19 import { createApp as createAppRecord } from "../src/core/apps.ts";
20 import { setIssueState } from "../src/core/issues.ts";
21 import type { Repo } from "../src/core/repos.ts";
22 import { createUser, type User } from "../src/core/users.ts";
23 import { serve, type Servers } from "../src/http/server.ts";
24 import { packPresses } from "../src/web/studio/home.ts";
25 import { PACK_FORMAT, type Pack } from "../src/packs/pack.ts";
26 import { mergedPull } from "./helpers/wave-pulls.ts";
27
28 /** The bars. */
29 export const RENDER_MS = 30;
30 export const HTML_BYTES = 30 * 1024;
31 const APPS = 40;
32 /** The shelf holds pictures too: this many apps have a thumb, so the 30 KB bar covers the tiles. */
33 const PICTURED = 12;
34 const HEAD = "a".repeat(40);
35 const PNG = Buffer.from(encodePng(1, 1, new Uint8Array([255, 255, 255, 255])));
36
37 let alice: User;
38 const must = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => {
39 if (!r.ok) throw new Error(JSON.stringify(r.error));
40 return r.value;
41 };
42
43 let dir: string;
44 let app: App;
45 let servers: Servers;
46 let bigRepo: Repo;
47 let writes = 0;
48 let runtime: AppsRuntime;
49 let restore: () => void = () => {};
50
51 beforeAll(async () => {
52 dir = await mkdtemp(join(tmpdir(), "forge-home-perf-"));
53 app = createApp({ dataDir: dir, port: 0 });
54 servers = await serve(app);
55 runtime = new AppsRuntime(app, { domain: "home.test", scheme: "http", publicPort: 0, forgeUrl: "http://localhost" });
56 const status = spyOn(AppsRuntime.prototype, "status").mockImplementation(
57 (): EnvStatus[] => [{ env: "production", state: "running", commitOid: HEAD } as EnvStatus],
58 );
59 restore = () => status.mockRestore();
60 alice = must(
61 await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }),
62 );
63 const build = async (r: number): Promise<void> => {
64 const repo = must(await createRepo(app, alice, { name: `app-${r}`, visibility: "public", description: `A tool for a business number ${r}, with a description of ordinary length for a shelf.` }));
65 must(await commitToBranch(app, alice, repo, { message: "init", files: [{ path: "README.md", content: "hi\n" }] }));
66 const record = must(createAppRecord(app.db, { repoId: repo.id, slug: repo.name, access: "public" }));
67 if (r < PICTURED) {
68 mkdirSync(join(dir, "looks", record.id), { recursive: true });
69 writeFileSync(join(dir, "looks", record.id, "thumb.png"), PNG);
70 recordLook(app.db, { appId: record.id, env: "production", commitOid: HEAD, kind: "thumb", path: `looks/${record.id}/thumb.png` });
71 }
72 const goal = must(openIssue(app, alice, repo, { title: `Ship milestone ${r}` }));
73 const parts = [1, 2, 3, 4, 5].map((n) => must(openIssue(app, alice, repo, { title: `Part ${n} of ${r}` })));
74 for (const p of parts) must(linkIssue(app, alice, repo, goal, p));
75 setIssueState(app.db, parts[0]!.id, "closed", "done");
76 const n = await mergedPull(app, alice, repo, `Part one of ${r}`);
77 app.db.query("UPDATE pull SET merged_at = ? WHERE issue_id = (SELECT id FROM issue WHERE repo_id = ? AND number = ?)").run(new Date(Date.now() - (r % 90) * 86_400_000).toISOString(), repo.id, n);
78 };
79 // The forge's own repository is the heavy one: a plan of a thousand issues, chained.
80 const big = must(await createRepo(app, alice, { name: "big", visibility: "public" }));
81 must(await commitToBranch(app, alice, big, { message: "init", files: [{ path: "README.md", content: "hi\n" }] }));
82 must(createAppRecord(app.db, { repoId: big.id, slug: "big", access: "public" }));
83 const goals = [0, 1, 2, 3].map((g) => must(openIssue(app, alice, big, { title: `Milestone ${g}` })));
84 for (let i = 0; i < 1000; i++) {
85 const part = must(openIssue(app, alice, big, { title: `Step ${i}` }));
86 must(linkIssue(app, alice, big, goals[i % 4]!, part));
87 if (i % 7 === 0) setIssueState(app.db, part.id, "closed", "done");
88 }
89 bigRepo = big;
90 for (let r = 0; r < APPS; r += 10) await Promise.all(Array.from({ length: Math.min(10, APPS - r) }, (_, k) => build(r + k)));
91 }, 300_000);
92
93 afterAll(async () => {
94 restore();
95 await runtime.stop();
96 servers.stop();
97 app.close();
98 await rm(dir, { recursive: true, force: true });
99 });
100
101 const phase = (timing: string, name: string): number => Number(new RegExp(`${name};dur=([0-9.]+)`).exec(timing)?.[1] ?? NaN);
102
103 async function renders(n: number): Promise<{ ms: number[]; bytes: number }> {
104 const ms: number[] = [];
105 let bytes = 0;
106 for (let i = 0; i < n; i++) {
107 // Something is always being written on a live forge: the plan stamps move under the page.
108 must(openIssue(app, alice, bigRepo, { title: `Written meanwhile ${writes++}` }));
109 const res = await fetch(`${servers.url}/`);
110 bytes = (await res.arrayBuffer()).byteLength;
111 ms.push(phase(res.headers.get("server-timing") ?? "", "render"));
112 }
113 return { ms, bytes };
114 }
115
116 test("the signed-out home's render phase is under 30 ms, median of 20", async () => {
117 await renders(2);
118 const { ms } = await renders(20);
119 ms.sort((a, b) => a - b);
120 const median = (ms[9]! + ms[10]!) / 2;
121 console.log("render median", median, "max", ms[19]);
122 expect(median).toBeLessThan(RENDER_MS);
123 });
124
125 test("the signed-out home's HTML is under 30 KB", async () => {
outdated

Principle-free but test-quality: this assertion passes whether or not the timer exists. Drive the timer (await a tick) and assert fresh lines afterwards, plus the stale lines in between.

126 const { bytes } = await renders(1);
127 console.log("bytes", bytes);
128 const page = await (await fetch(`${servers.url}/`)).text();
129 expect(page).toContain('class="look"'); // the shelf's pictures are in the measured page
130 expect(bytes).toBeLessThan(HTML_BYTES);
131 });
132
133 test("a long catalogue of packs is three on the door and a link to the rest", () => {
134 const packs: Pack[] = Array.from({ length: 40 }, (_, i) => ({
135 format: PACK_FORMAT, name: `pack-${i}`, title: `Pack ${i}`, version: "1", description: "A ready-made forge.", apps: [], crew: [],
136 }));
137 const out = String(packPresses(packs));
138 expect(out.match(/<article/g)?.length).toBe(3);
139 expect(out).toContain('href="/packs"');
140 expect(out).toContain("All 40 ready-made forges");
141 });
142
143 test("a stranger's first paint never waits for a plan to be read again: the old lines now, the fresh ones after the timer", async () => {
144 await renders(1);
145 const { milestonesLater } = await import("../src/web/studio/milestone_lines.ts");
146 const { appsOf } = await import("../src/web/studio/asks.ts");
147 const repos = appsOf({ app }, undefined).repos;
148 const first = milestonesLater({ app }, repos, Date.now());
149 const sig = (l: typeof first): string => JSON.stringify(l.map((x) => [x.repo.id, x.limb.weight]));
150 // The plan moves: every open part of the first line's plan is closed.
151 const open = app.db.query("SELECT id FROM issue WHERE repo_id = ? AND state = 'open'").all(first[0]!.repo.id) as { id: string }[];
152 for (const o of open) setIssueState(app.db, o.id, "closed", "done");
153 // Past the minute, the request in between gets the old lines, without reading.
154 const between = milestonesLater({ app }, repos, Date.now() + 120_000);
155 expect(between).toBe(first);
156 // The timer runs after the response; the next call returns lines read afresh.
157 await new Promise((done) => setTimeout(done, 50));
158 const later = milestonesLater({ app }, repos, Date.now() + 121_000);
159 expect(later).not.toBe(first);
160 expect(sig(later)).not.toBe(sig(first));
161 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs