Public: anyone can read this; the crew can change it.
GET a repository's milestones answers under 50 ms: trevato/forge's `read` phase is 1,365 ms today #16735
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/6199-get-a-repository-s-milestones-answers-un → main 0 commits fork 3 threads · 3 unresolved
3 files
changed +148
−6
src/web/pages/milestones.ts +12 −4
| @@ -723,10 +723,17 @@ export async function milestonesJson( | ||
| 723 | 723 | ctx: Ctx, |
| 724 | 724 | repo: Repo, |
| 725 | 725 | ): Promise<{ repo: { owner: string; name: string }; limbs: Record<string, unknown>[] }> { |
| 726 | const plan = readPlan(ctx.app.db, repo, { | |
| 727 | spend: canSeeOwnerFacts(ctx.app.db, repo, ctx.auth.user), | |
| 728 | }); | |
| 729 | const ask = askByUmbrella(ctx, repo); | |
| 726 | // The page's own plan, kept while nothing it reads changes (#6199): the | |
| 727 | // route used to rebuild it on every request. | |
| 728 | const plan = planFor(ctx, repo, canSeeOwnerFacts(ctx.app.db, repo, ctx.auth.user), new Date()); | |
| 729 | const stretch = <T>(name: string, run: () => T): T => { | |
| 730 | const t0 = performance.now(); | |
| 731 | const out = run(); | |
| 732 | notePhase(ctx.request, `read-${name}`, performance.now() - t0); | |
| 733 | return out; | |
| 734 | }; | |
| 735 | const ask = stretch("ask", () => askByUmbrella(ctx, repo)); | |
| 736 | const t1 = performance.now(); | |
| 730 | 737 | const waves = await lastWaves({ |
| 731 | 738 | db: ctx.app.db, |
| 732 | 739 | reposRoot: ctx.app.reposRoot, |
| @@ -734,6 +741,7 @@ export async function milestonesJson( | ||
| 734 | 741 | viewer: ctx.auth.user, |
| 735 | 742 | plan: () => plan, |
| 736 | 743 | }); |
| 744 | notePhase(ctx.request, "read-waves", performance.now() - t1); | |
| 737 | 745 | return { |
| 738 | 746 | repo: { owner: repo.ownerName, name: repo.name }, |
| 739 | 747 | limbs: plan.limbs.map((limb) => ({ |
test/api-milestones-speed.test.ts added +132
| @@ -0,0 +1,132 @@ | ||
| 1 | /** GET /milestones answers from the page's kept plan: same facts, `read` under 50 ms (#6199). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createRepo } from "../src/actions/repos.ts"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { limbJson, readPlan } from "../src/core/milestones.ts"; | |
| 9 | import { canSeeOwnerFacts, getRepo } from "../src/core/repos.ts"; | |
| 10 | import { pillarView } from "../src/core/pillars.ts"; | |
| 11 | import { createUser, type User } from "../src/core/users.ts"; | |
| 12 | import { lastWaves } from "../src/story/wave_last.ts"; | |
| 13 | import { askToLiveFor } from "../src/web/ask_to_live.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { clearMapCache } from "../src/web/map_cache.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | import { proveAll } from "./helpers/verified.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | const PILLARS = 10; | |
| 22 | const CLOSED = 2000; | |
| 23 | const OPEN = 300; | |
| 24 | ||
| 25 | let root = ""; | |
| 26 | let app: App; | |
| 27 | let servers: Servers; | |
| 28 | let cookie = ""; | |
| 29 | let alice: User; | |
| 30 | ||
| 31 | beforeAll(async () => { | |
| 32 | root = mkdtempSync(join(tmpdir(), "forge-api-milestones-speed-")); | |
| 33 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 34 | const u = await createUser(app.db, { | |
| 35 | origin: "seed", | |
| 36 | trusted: true, | |
| 37 | username: "alice", | |
| 38 | email: "[email protected]", | |
| 39 | password: "a-good-passphrase", | |
| 40 | adminIfFirst: true, | |
| 41 | }); | |
| 42 | if (!u.ok) throw new Error(u.error); | |
| 43 | alice = u.value; | |
| 44 | proveAll(app); | |
| 45 | const made = await createRepo(app, u.value, { name: "forge" }); | |
| 46 | if (!made.ok) throw new Error(made.error); | |
| 47 | const repoId = getRepo(app.db, "alice", "forge")!.id; | |
| 48 | const issue = app.db.prepare( | |
| 49 | `INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at, updated_at, closed_at) | |
| 50 | VALUES (?1, ?2, ?3, 'issue', ?4, '', ?5, ?6, ?7, ?7, ?8)`, | |
| 51 | ); | |
| 52 | const edge = app.db.prepare("INSERT INTO issue_dependency (issue_id, blocked_by_id) VALUES (?1, ?2)"); | |
| 53 | const at = new Date(Date.now() - 3_600_000).toISOString(); | |
| 54 | app.db.transaction(() => { | |
| 55 | let n = 0; | |
| 56 | for (let p = 0; p < PILLARS; p++) | |
| 57 | issue.run(`u-${p}`, repoId, ++n, `P${p + 1} Pillar ${p + 1}: ask`, u.value.id, "open", at, null); | |
| 58 | for (let k = 0; k < CLOSED + OPEN; k++) { | |
| 59 | const closed = k < CLOSED; | |
| 60 | issue.run(`c-${k}`, repoId, ++n, `Step ${k}`, u.value.id, closed ? "closed" : "open", at, closed ? at : null); | |
| 61 | edge.run(`u-${k % PILLARS}`, `c-${k}`); | |
| 62 | } | |
| 63 | app.db.run("UPDATE repo SET issue_seq = ?1 WHERE id = ?2", [n, repoId]); | |
| 64 | })(); | |
| 65 | servers = await serve(app); | |
| 66 | const signin = await fetch(`${servers.url}/signin`, { | |
| 67 | method: "POST", | |
| 68 | headers: { origin: servers.url }, | |
| 69 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 70 | redirect: "manual", | |
| 71 | }); | |
| 72 | cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 73 | }); | |
| 74 | afterAll(() => { | |
| 75 | servers.stop(); | |
| 76 | app.close(); | |
| 77 | rmSync(root, { recursive: true, force: true }); | |
| 78 | }); | |
| 79 | ||
| 80 | const get = async () => { | |
| 81 | const res = await fetch(`${servers.url}/api/v1/repos/alice/forge/milestones?limit=50`, { headers: { cookie } }); | |
| 82 | const body = (await res.json()) as { items: { number: number }[] }; | |
| 83 | const timing = res.headers.get("server-timing") ?? ""; | |
| 84 | const read = Number(/read;dur=([\d.]+)/.exec(timing)?.[1]); | |
| 85 | return { status: res.status, body, read, timing }; | |
| 86 | }; | |
| 87 | ||
| 88 | test("the answer is the old path's, item for item: plan, ask-to-live and last wave", async () => { | |
| 89 | clearMapCache(); | |
| 90 | const got = await get(); | |
| 91 | expect(got.status).toBe(200); | |
| 92 | const repo = getRepo(app.db, "alice", "forge")!; | |
| 93 | // The old path: a fresh plan, then the same ask and last-wave calls the route makes. | |
| 94 | const fresh = readPlan(app.db, repo, { spend: canSeeOwnerFacts(app.db, repo, alice) }); | |
| 95 | const view = pillarView(app.db, repo)!; | |
| 96 | const ask = askToLiveFor(app, repo, view, alice); | |
| 97 | const waves = await lastWaves({ | |
| 98 | db: app.db, | |
| 99 | reposRoot: app.reposRoot, | |
| 100 | repo, | |
| 101 | viewer: alice, | |
| 102 | plan: () => fresh, | |
| 103 | }); | |
| 104 | const want = fresh.limbs.map((limb) => { | |
| 105 | const pillar = view.set.pillars.find((p) => p.umbrella.number === limb.issue.number); | |
| 106 | const w = waves.get(limb.issue.number); | |
| 107 | return JSON.parse( | |
| 108 | JSON.stringify({ | |
| 109 | ...limbJson(limb), | |
| 110 | ...(pillar ? { askToLive: ask.find((a) => a.pillar === pillar.name)?.askToLive ?? null } : {}), | |
| 111 | ...(w === undefined ? {} : { lastWave: w }), | |
| 112 | }), | |
| 113 | ); | |
| 114 | }); | |
| 115 | expect(got.body.items).toHaveLength(PILLARS); | |
| 116 | const items = got.body.items as unknown[]; | |
| 117 | // Pages drop `repo`; everything else must be equal, key for key. | |
| 118 | expect(items).toEqual(want); | |
| 119 | expect(want.every((w: Record<string, unknown>) => "askToLive" in w)).toBe(true); | |
| 120 | }); | |
| 121 | ||
| 122 | test("read names its stretches; warm stays under 50 ms, cold under a loose bound", async () => { | |
| 123 | clearMapCache(); | |
| 124 | const cold = await get(); | |
| 125 | for (const name of ["plan-graph", "read-ask", "read-waves"]) expect(cold.timing).toContain(`${name};dur=`); | |
| 126 | // A cold plan over 2,300 issues can spike on a shared runner: the strict | |
| 127 | // bound is the warm read's, the cold one only guards against the old 1.4 s. | |
| 128 | expect(cold.read).toBeLessThan(1000); | |
| 129 | // Best of a few warm reads, so one busy moment cannot turn the check red. | |
| 130 | const warm = Math.min(...[(await get()).read, (await get()).read, (await get()).read]); | |
| 131 | expect(warm).toBeLessThan(50); | |
| 132 | }); |
test/api-milestones-timing.test.ts +4 −2
| @@ -68,10 +68,12 @@ async function phases(path: string): Promise<{ status: number; names: string[]; | ||
| 68 | 68 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | test("the milestones list names session, read, render", async () => { | |
| 71 | test("the milestones list names session, read (and its stretches), render", async () => { | |
| 72 | 72 | const got = await phases("/api/v1/repos/alice/r/milestones"); |
| 73 | 73 | expect(got.status).toBe(200); |
| 74 | expect(got.names).toEqual(["session", "read", "render"]); | |
| 74 | // The stretches of `read` (#6199) are named; they come before the phase that holds them. | |
| 75 | expect(got.names.filter((n) => !/^(plan|read)-/.test(n))).toEqual(["session", "read", "render"]); | |
| 76 | expect(got.names).toContain("read-ask"); | |
| 75 | 77 | }); |
| 76 | 78 | |
| 77 | 79 | test("the proposals read names session, read, render", async () => { |
new above
offline
Good: this reuses the page's plan, so there is one way to build it. The ask and wave stretches are still computed per request. If the whole answer is not compared against the old output in a test, a change in either goes unseen.