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

The new pull page names its Server-Timing phases: session, the compare, render #3641

Merged opened this

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

Merged

scout/forge:scout/3634-the-new-pull-page-names-its-server-timin → main 1 commit fork 1 thread · 1 unresolved

  • Merged. scout/3634-the-new-pull-page-names-its-server-timin was merged into main as 561ec79.

Review

Sign in to review this change.
2 files changed +71 −1
src/web/pages/pulls.ts +8 −1
@@ -12,6 +12,7 @@ import {
1212 forkParentFor,
1313 type Repo,
1414 } from "../../core/repos.ts";
15 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
1516 import { repoDir } from "../../git/repos.ts";
1617 import type { Ctx } from "../../http/context.ts";
1718 import {
@@ -148,11 +149,15 @@ async function composeContext(ctx: Ctx, repo: Repo, forceSelf: boolean) {
148149
149150 // GET /:owner/:name/pulls/new
150151 export async function newPullPage(ctx: Ctx): Promise<Response> {
152 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
151153 const repo = loadRepo(ctx);

loadRepo on the next line is a read and is not timed; consider a phase for it so the phases account for the request.

152154 if (!repo) return notFoundPage(ctx);
153155 if (!ctx.auth.user) return signInFirst(ctx);
154156 const forceSelf = ctx.url.searchParams.get("self") !== null;
157 const comparing = performance.now();
155158 const c = await composeContext(ctx, repo, forceSelf);
159 notePhase(ctx.request, "compare", performance.now() - comparing);
160 const rendering = performance.now();
156161 // The branches page proposes a branch by linking here with it named, so
157162 // "New pull" from a row arrives with that row already chosen.
158163 const wantedHead = ctx.url.searchParams.get("head") ?? "";
@@ -197,13 +202,15 @@ export async function newPullPage(ctx: Ctx): Promise<Response> {
197202 action: btn("Back to code", { href: repoBase(repo) }),
198203 })}
199204 </div>`;
200 return page({
205 const response = page({
201206 ctx,
202207 title: `New pull request · ${repo.name}`,
203208 crumbs: crumbsFor(repo),
204209 palette: repoPalette(ctx, repo),
205210 body,
206211 });
212 notePhase(ctx.request, "render", performance.now() - rendering);
213 return response;
207214 }
208215
209216 const COMPOSE_ERRORS: Record<string, string> = {
test/new-pull-timing.test.ts added +63
@@ -0,0 +1,63 @@
1 /** `/:owner/:name/pulls/new`'s Server-Timing names its phases (#3634). */
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-new-pull-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 for (const [branch, file] of [[undefined, "forge.json"], ["topic", "t.txt"]] as const) {
40 const done = await commitToBranch(app, alice, repo.value, {
41 ...(branch === undefined ? {} : { branch }),
42 message: "change",
43 files: [{ path: file, content: file === "forge.json" ? "{}" : "x" }],
44 });
45 if (!done.ok) throw new Error(JSON.stringify(done.error));
46 }
47 });
48
49 afterAll(async () => {
50 await servers.stop();
51 app.close();
52 if (cells === undefined) delete process.env["FORGE_CELLS"];
53 else process.env["FORGE_CELLS"] = cells;
54 await rm(dataDir, { recursive: true, force: true });
55 });
56
57 test("the new pull page names session, compare and render after app", async () => {
58 const res = await fetch(`${servers.url}/alice/r/pulls/new`, { headers: { cookie } });
59 expect(res.status).toBe(200);
60 await res.arrayBuffer();
61 const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
62 expect(names.slice(names.indexOf("app") + 1)).toEqual(["session", "compare", "render"]);
63 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs