Public: anyone can read this; the crew can change it.
/asked names its Server-Timing phases and answers under 50 ms for a person with 500 asks #4006
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3358-asked-names-its-server-timing-phases-and → main 3 commits fork
2 files
changed +102
−6
src/web/studio/asked_page.ts +12 −6
| @@ -25,6 +25,7 @@ import { | ||
| 25 | 25 | type StreamThought, |
| 26 | 26 | type ThoughtState, |
| 27 | 27 | } from "../../core/thought_stream.ts"; |
| 28 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 28 | 29 | import type { Ctx } from "../../http/context.ts"; |
| 29 | 30 | import { repoBase, signInFirst } from "../chrome.ts"; |
| 30 | 31 | import { html, type Html } from "../html.ts"; |
| @@ -155,16 +156,18 @@ function askedRow(a: Asked): Html { | ||
| 155 | 156 | * `flagged` or `all`; oldest first unless `?order=newest`; `?repo=` for one. |
| 156 | 157 | */ |
| 157 | 158 | export function askedPage(ctx: Ctx): Response { |
| 159 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 158 | 160 | if (!ctx.auth.user) return signInFirst(ctx); |
| 161 | const user = ctx.auth.user; | |
| 159 | 162 | const q = ctx.url.searchParams; |
| 160 | 163 | const rawShow = q.get("show"); |
| 161 | 164 | const show: AskedShow = isAskedShow(rawShow) ? rawShow : "unshipped"; |
| 162 | 165 | const rawOrder = q.get("order"); |
| 163 | 166 | const order: AskedOrder = isAskedOrder(rawOrder) ? rawOrder : "oldest"; |
| 164 | 167 | const repoSpec = q.get("repo"); |
| 165 | const repos = askedRepos(ctx.app.db, ctx.auth.user, repoSpec); | |
| 166 | const counts = askedCounts(ctx.app.db, repos); | |
| 167 | const listed = askedIn(ctx.app.db, repos, { show, order }); | |
| 168 | const repos = timePhase(ctx.request, "repos", () => askedRepos(ctx.app.db, user, repoSpec)); | |
| 169 | const counts = timePhase(ctx.request, "counts", () => askedCounts(ctx.app.db, repos)); | |
| 170 | const listed = timePhase(ctx.request, "asks", () => askedIn(ctx.app.db, repos, { show, order })); | |
| 168 | 171 | const href = (s: AskedShow, o: AskedOrder = order) => { |
| 169 | 172 | const parts = [ |
| 170 | 173 | s === "unshipped" ? "" : `show=${s}`, |
| @@ -173,8 +176,9 @@ export function askedPage(ctx: Ctx): Response { | ||
| 173 | 176 | ].filter(Boolean); |
| 174 | 177 | return `/asked${parts.length > 0 ? `?${parts.join("&")}` : ""}`; |
| 175 | 178 | }; |
| 176 | const stream = thoughtStream(ctx.app.db, ctx.auth.user, { limit: 50 }); | |
| 177 | const days = flagDays(ctx.app.db, ctx.auth.user.id); | |
| 179 | const stream = timePhase(ctx.request, "thoughts", () => thoughtStream(ctx.app.db, user, { limit: 50 })); | |
| 180 | const days = timePhase(ctx.request, "flags", () => flagDays(ctx.app.db, user.id)); | |
| 181 | const rendering = performance.now(); | |
| 178 | 182 | const next = oldestFlaggedHref(stream); |
| 179 | 183 | const flaggedThoughts = stream.filter((t) => t.flagged).length; |
| 180 | 184 | const thoughts = |
| @@ -239,5 +243,7 @@ export function askedPage(ctx: Ctx): Response { | ||
| 239 | 243 | : "Every ask in this view has something built for it.", |
| 240 | 244 | }) |
| 241 | 245 | : listCard(listed.map(askedRow))}`; |
| 242 | return page({ ctx, title: "What you asked for", body }); | |
| 246 | const response = page({ ctx, title: "What you asked for", body }); | |
| 247 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 248 | return response; | |
| 243 | 249 | } |
test/asked-timing.test.ts added +90
| @@ -0,0 +1,90 @@ | ||
| 1 | /** `/asked` names session, each read and render in Server-Timing, and answers a person with 500 asks in under 50 ms (#3358). */ | |
| 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 { createIssue } from "../src/core/issues.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { proveAll } from "./helpers/verified.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | let root = ""; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | let cookie = ""; | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | root = mkdtempSync(join(tmpdir(), "forge-asked-timing-")); | |
| 23 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 24 | const u = await createUser(app.db, { | |
| 25 | origin: "seed", | |
| 26 | trusted: true, | |
| 27 | username: "alice", | |
| 28 | email: "[email protected]", | |
| 29 | password: "a-good-passphrase", | |
| 30 | adminIfFirst: true, | |
| 31 | }); | |
| 32 | if (!u.ok) throw new Error(u.error); | |
| 33 | proveAll(app); | |
| 34 | const owner = { id: u.value.id, username: "alice" } as never; | |
| 35 | // 500 asks over 5 repositories; every other ask is closed by a merged pull. | |
| 36 | for (let r = 0; r < 5; r++) { | |
| 37 | const made = await createRepo(app, owner, { name: `r${r}` }); | |
| 38 | if (!made.ok) throw new Error(made.error); | |
| 39 | for (let i = 0; i < 100; i++) { | |
| 40 | const ask = createIssue(app.db, { repoId: made.value.id, authorId: u.value.id, title: `Ask ${r}.${i}` }); | |
| 41 | if (!ask.ok) throw new Error(ask.error); | |
| 42 | if (i % 2 === 1) continue; | |
| 43 | const pull = createIssue(app.db, { | |
| 44 | repoId: made.value.id, | |
| 45 | authorId: u.value.id, | |
| 46 | title: `Answers ${ask.value.number}`, | |
| 47 | body: `Closes #${ask.value.number}`, | |
| 48 | kind: "pull", | |
| 49 | }); | |
| 50 | if (!pull.ok) throw new Error(pull.error); | |
| 51 | app.db | |
| 52 | .query( | |
| 53 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) | |
| 54 | VALUES (?1, 'main', ?2, 'a', 'b', ?3)`, | |
| 55 | ) | |
| 56 | .run(pull.value.id, `b${i}`, new Date().toISOString()); | |
| 57 | } | |
| 58 | } | |
| 59 | servers = await serve(app); | |
| 60 | const signin = await fetch(`${servers.url}/signin`, { | |
| 61 | method: "POST", | |
| 62 | headers: { origin: servers.url }, | |
| 63 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 64 | redirect: "manual", | |
| 65 | }); | |
| 66 | cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 67 | }); | |
| 68 | afterAll(() => { | |
| 69 | servers.stop(); | |
| 70 | app.close(); | |
| 71 | rmSync(root, { recursive: true, force: true }); | |
| 72 | }); | |
| 73 | ||
| 74 | test("GET /asked names its phases and answers under 50 ms with 500 asks", async () => { | |
| 75 | const get = () => fetch(`${servers.url}/asked?show=all`, { headers: { cookie } }); | |
| 76 | await (await get()).text(); | |
| 77 | // The best of three: a shared runner's stall hits one request, not the page's cost. | |
| 78 | let best = Infinity; | |
| 79 | for (let i = 0; i < 3; i++) { | |
| 80 | const res = await get(); | |
| 81 | const body = await res.text(); | |
| 82 | expect(res.status).toBe(200); | |
| 83 | expect(body).toContain("500 asked · 250 shipped"); | |
| 84 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 85 | for (const name of ["app", "session", "repos", "counts", "asks", "thoughts", "flags", "render"]) | |
| 86 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/); | |
| 87 | best = Math.min(best, Number(/app;dur=([\d.]+)/.exec(parts.join(", "))![1])); | |
| 88 | } | |
| 89 | expect(best).toBeLessThan(50); | |
| 90 | }); |
new above
offline