Public: anyone can read this; the crew can change it.
A repository's home page's Server-Timing names its phases: session, each read, render #3331
scout/forge:scout/3325-a-repository-s-home-page-s-server-timing → main 2 commits fork 2 threads · 2 unresolved
2 files
changed +77
−7
src/web/pages/repo.ts +17 −7
| @@ -46,7 +46,7 @@ import { | ||
| 46 | 46 | } from "../chrome.ts"; |
| 47 | 47 | import { codeJs } from "../client.ts"; |
| 48 | 48 | import { highlightBlob } from "../code.ts"; |
| 49 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; | |
| 49 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; | |
| 50 | 50 | import { liveClientJs, liveMount } from "../live.ts"; |
| 51 | 51 | import { appStrip } from "./apps.ts"; |
| 52 | 52 | import { studioApp } from "../studio/app.ts"; |
| @@ -706,11 +706,13 @@ export async function renderCode( | ||
| 706 | 706 | entries: readonly TreeEntry[]; |
| 707 | 707 | extra?: Html; |
| 708 | 708 | rail?: Html; |
| 709 | /** The README, when the caller has already read it (and timed the read). */ | |
| 710 | readme?: Html; | |
| 709 | 711 | }, |
| 710 | 712 | ): Promise<Html> { |
| 711 | 713 | const base = scope.base; |
| 712 | 714 | const { ref, path } = o; |
| 713 | const readme = await readmeFor(scope.source, ref, o.entries, path); | |
| 715 | const readme = o.readme ?? (await readmeFor(scope.source, ref, o.entries, path)); | |
| 714 | 716 | const historyParams = new URLSearchParams({ ref }); |
| 715 | 717 | if (path !== "") historyParams.set("path", path); |
| 716 | 718 | const bar = |
| @@ -959,11 +961,12 @@ function archivedLine(ctx: Ctx, repo: Repo): Html | null { | ||
| 959 | 961 | /** The code at a ref: the tree, the README and the About rail — with, in |
| 960 | 962 | * the studio, the offer to make a repository that is not an app one. */ |
| 961 | 963 | async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { |
| 962 | const isApp = getAppByRepo(ctx.app.db, repo.id) !== undefined; | |
| 964 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 965 | const isApp = timePhase(ctx.request, "appof", () => getAppByRepo(ctx.app.db, repo.id) !== undefined); | |
| 963 | 966 | const noApp = isApp ? null : noAppCard(ctx, repo); |
| 964 | 967 | const scope = localScope(ctx, repo); |
| 965 | 968 | const cloneUrl = `${ctx.url.origin}/${repo.ownerName}/${repo.name}.git`; |
| 966 | const refs = await refSetFrom(scope.source); | |
| 969 | const refs = await timePhaseAsync(ctx.request, "refs", () => refSetFrom(scope.source)); | |
| 967 | 970 | |
| 968 | 971 | if (refs.branches.length === 0) { |
| 969 | 972 | const body = html`${scopeHeader(ctx, scope, "code")}${archivedLine(ctx, repo)}${noApp} |
| @@ -1018,12 +1021,16 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 1018 | 1021 | // the tree read, not after it, because neither waits on the other. |
| 1019 | 1022 | const parent = forkParent(ctx, repo); |
| 1020 | 1023 | const [entries, drift] = await Promise.all([ |
| 1021 | scope.source.tree(ref, ""), | |
| 1024 | timePhaseAsync(ctx.request, "tree", () => scope.source.tree(ref, "")), | |
| 1022 | 1025 | parent === undefined ? undefined : forkDrift(ctx.app, repo, parent), |
| 1023 | 1026 | ]); |
| 1024 | 1027 | const tree = entries.ok ? entries.value : []; |
| 1025 | 1028 | |
| 1026 | const following = await followingOf(ctx, repo); | |
| 1029 | const readme = await timePhaseAsync(ctx.request, "readme", () => | |
| 1030 | readmeFor(scope.source, ref, tree, ""), | |
| 1031 | ); | |
| 1032 | const following = await timePhaseAsync(ctx.request, "following", () => followingOf(ctx, repo)); | |
| 1033 | const rendering = performance.now(); | |
| 1027 | 1034 | const body = html`${scopeHeader(ctx, scope, "code", { |
| 1028 | 1035 | branchCount: refs.branches.length, |
| 1029 | 1036 | })}${archivedLine(ctx, repo)}${noApp} |
| @@ -1036,10 +1043,11 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 1036 | 1043 | ref, |
| 1037 | 1044 | path: "", |
| 1038 | 1045 | entries: tree, |
| 1046 | readme, | |
| 1039 | 1047 | rail: aboutCard(ctx, repo, refs, drift), |
| 1040 | 1048 | })} |
| 1041 | 1049 | ${codeMount(scope, ref)} ${finderDialog()} ${liveMount({ repo: scope.base })}`; |
| 1042 | return page({ | |
| 1050 | const response = page({ | |
| 1043 | 1051 | ctx, |
| 1044 | 1052 | title: repo.name, |
| 1045 | 1053 | description: repo.description, |
| @@ -1053,6 +1061,8 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 1053 | 1061 | body, |
| 1054 | 1062 | scriptJs: `${codeJs}\n${liveClientJs}`, |
| 1055 | 1063 | }); |
| 1064 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 1065 | return response; | |
| 1056 | 1066 | } |
| 1057 | 1067 | |
| 1058 | 1068 | function knownRef(refs: RefSet, name: string): boolean { |
test/repo-timing.test.ts added +60
| @@ -0,0 +1,60 @@ | ||
| 1 | /** A repository home's Server-Timing names its phases, in order (#3325). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createSession } from "../src/core/sessions.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | ||
| 13 | quietLogs(); | |
| 14 | ||
| 15 | let dataDir: string; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | let cookie = ""; | |
| 19 | const cells = process.env["FORGE_CELLS"]; | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | process.env["FORGE_CELLS"] = "off"; | |
| 23 | dataDir = await mkdtemp(join(tmpdir(), "forge-repo-timing-")); | |
| 24 | app = createApp({ dataDir, port: 0 }); | |
| 25 | servers = await serve(app); | |
| 26 | const made = await createUser(app.db, { | |
| 27 | origin: "seed", | |
| 28 | trusted: true, | |
| 29 | username: "alice", | |
| 30 | email: "[email protected]", | |
| 31 | password: "a-good-passphrase", | |
| 32 | adminIfFirst: true, | |
| 33 | }); | |
| 34 | if (!made.ok) throw new Error(String(made.error)); | |
| 35 | const alice = made.value; | |
| 36 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 37 | const repo = await createRepo(app, alice, { name: "r", visibility: "public" }); | |
| 38 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 39 | const done = await commitToBranch(app, alice, repo.value, { | |
| 40 | message: "change", | |
outdated
A loop over a one-element array with an unused branch variable is dead generality. Call commitToBranch once with README.md. |
||
| 41 | files: [{ path: "README.md", content: "# hi" }], | |
| 42 | }); | |
| 43 | if (!done.ok) throw new Error(JSON.stringify(done.error)); | |
| 44 | }); | |
| 45 | ||
| 46 | afterAll(async () => { | |
| 47 | await servers.stop(); | |
| 48 | app.close(); | |
| 49 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 50 | else process.env["FORGE_CELLS"] = cells; | |
| 51 | await rm(dataDir, { recursive: true, force: true }); | |
| 52 | }); | |
| 53 | ||
| 54 | test("a repository home names session, appof, refs, tree, readme, following and render in order", async () => { | |
| 55 | const res = await fetch(`${servers.url}/alice/r`, { headers: { cookie } }); | |
| 56 | expect(res.status).toBe(200); | |
| 57 | await res.arrayBuffer(); | |
| 58 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 59 | expect(names).toEqual(["app", "session", "appof", "refs", "tree", "readme", "following", "render"]); | |
| 60 | }); |
new above
offline
Duplicate metric name: the lookup is called "app", which the header already uses for the total. Name it e.g. "appof"; the test then reads app, session, appof, ...