|
|
@@ -0,0 +1,186 @@ |
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import { afterAll, beforeAll, expect, test } from "bun:test"; |
| 14 |
import { mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 15 |
import { tmpdir } from "node:os"; |
| 16 |
import { join } from "node:path"; |
| 17 |
import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 18 |
import { createApp, type App } from "../src/app.ts"; |
| 19 |
import { createIssue } from "../src/core/issues.ts"; |
| 20 |
import { pin } from "../src/core/pins.ts"; |
| 21 |
import { getRepo } from "../src/core/repos.ts"; |
| 22 |
import { createUser, type User } from "../src/core/users.ts"; |
| 23 |
import { buildApiRouter, buildRouter, serve, type Servers } from "../src/http/server.ts"; |
| 24 |
import { PAGE_P95_MS } from "../src/perf/budgets.ts"; |
| 25 |
import { quietLogs } from "./helpers/globals.ts"; |
| 26 |
import { promote } from "./helpers/members.ts"; |
| 27 |
import { proveAll } from "./helpers/verified.ts"; |
| 28 |
import { pageWork, type PageWork } from "./helpers/work.ts"; |
| 29 |
|
| 30 |
quietLogs(); |
| 31 |
|
| 32 |
|
| 33 |
const DEFAULT = { statements: 60, git: 8 }; |
| 34 |
|
| 35 |
const HEAVIER: Record<string, { statements?: number; git?: number }> = { |
| 36 |
"/:owner/:name/issues/:number": { statements: 70 }, |
| 37 |
"/:owner/:name/next": { statements: 64 }, |
| 38 |
"/:owner/:name/pulls/:number": { statements: 106 }, |
| 39 |
"/admin/limits": { statements: 80 }, |
| 40 |
"/board": { statements: 64 }, |
| 41 |
"/settings/capacity": { statements: 65 }, |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
const NOT_PAGES = new Set([ |
| 46 |
"/events", "/system/stream", "/settings/teams/stream", "/fleet/graph/live", |
| 47 |
"/settings/traffic/scrapes/export", "/settings/backups/live", "/:owner/:name/stream", |
| 48 |
]); |
| 49 |
|
| 50 |
|
| 51 |
const FIXTURE: Record<string, string> = { |
| 52 |
"/:owner/:name/issues/:number": "/alice/forge/issues/1", |
| 53 |
"/:owner/:name/pulls/:number": "/alice/forge/pulls/2", |
| 54 |
"/:owner/:name/display": "/alice/forge/display", |
| 55 |
}; |
| 56 |
|
| 57 |
function registeredGets(): Set<string> { |
| 58 |
const source = ["server.ts", "settings_routes.ts"] |
| 59 |
.map((f) => readFileSync(join(import.meta.dir, "../src/http", f), "utf8")) |
| 60 |
.join("\n"); |
| 61 |
const found = new Set<string>(); |
| 62 |
for (const m of source.matchAll(/router\.add\(\s*"GET",\s*"([^"]+)"/g)) found.add(m[1]!); |
| 63 |
return found; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
export function plan(): { walked: Array<[pattern: string, path: string]>; unfixtured: string[] } { |
| 68 |
const held = new Set([...buildRouter().patterns(), ...buildApiRouter().patterns()]); |
| 69 |
const walked: Array<[string, string]> = []; |
| 70 |
const unfixtured: string[] = []; |
| 71 |
for (const p of [...registeredGets()].filter((p) => held.has(p)).sort()) { |
| 72 |
if (NOT_PAGES.has(p) || p.startsWith("/assets/") || p.endsWith("/live") || p.includes("/__forge/")) continue; |
| 73 |
const path = FIXTURE[p] ?? p.replace(":owner", "alice").replace(":name", "forge"); |
| 74 |
if (path.includes(":") || path.includes("*")) unfixtured.push(p); |
| 75 |
else walked.push([p, path]); |
| 76 |
} |
| 77 |
return { walked, unfixtured }; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
export function judge(pattern: string, w: PageWork, judgeClock: boolean): string[] { |
| 82 |
const max = { ...DEFAULT, ...HEAVIER[pattern] }; |
| 83 |
const slowest = w.phases[0] === undefined ? "no phases" : `slowest phase ${w.phases[0].name} ${w.phases[0].ms.toFixed(1)} ms`; |
| 84 |
const out: string[] = []; |
| 85 |
if (w.statements > max.statements) out.push(`${pattern}: ${w.statements} SQL statements, budget ${max.statements} (${slowest})`); |
| 86 |
if (w.git > max.git) out.push(`${pattern}: ${w.git} git processes, budget ${max.git} (${slowest})`); |
| 87 |
if (judgeClock && w.ms > PAGE_P95_MS) out.push(`${pattern}: ${w.ms.toFixed(0)} ms, budget ${PAGE_P95_MS} ms (${slowest})`); |
| 88 |
return out; |
| 89 |
} |
| 90 |
|
| 91 |
let root = ""; |
| 92 |
let app: App; |
| 93 |
let servers: Servers; |
| 94 |
let cookie = ""; |
| 95 |
|
| 96 |
async function user(name: string, first: boolean): Promise<User> { |
| 97 |
const u = await createUser(app.db, { |
| 98 |
origin: "seed", trusted: true, username: name, email: `${name}@forge.local`, |
| 99 |
password: "a-good-passphrase", adminIfFirst: first, |
| 100 |
}); |
| 101 |
if (!u.ok) throw new Error(u.error); |
| 102 |
promote(app.db, u.value); |
| 103 |
return u.value; |
| 104 |
} |
| 105 |
|
| 106 |
beforeAll(async () => { |
| 107 |
root = mkdtempSync(join(tmpdir(), "forge-pages-budget-")); |
| 108 |
app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); |
| 109 |
const alice = await user("alice", true); |
| 110 |
const bob = await user("bob", false); |
| 111 |
await user("carol", false); |
| 112 |
proveAll(app); |
| 113 |
for (const [who, name, visibility] of [ |
| 114 |
[alice, "forge", "public"], [alice, "vault", "private"], [bob, "notes", "public"], [bob, "diary", "private"], |
| 115 |
] as const) { |
| 116 |
const made = await createRepo(app, who, { name, visibility }); |
| 117 |
if (!made.ok) throw new Error(String(made.error)); |
| 118 |
} |
| 119 |
const forge = getRepo(app.db, "alice", "forge")!; |
| 120 |
const files = await commitToBranch(app, alice, forge, { |
| 121 |
message: "display", |
| 122 |
files: [ |
| 123 |
{ path: "README.md", content: "# forge\n" }, |
| 124 |
{ path: "display.json", content: JSON.stringify({ title: "Night", layout: "grid", sources: ["alice/forge"] }) }, |
| 125 |
], |
| 126 |
}); |
| 127 |
if (!files.ok) throw new Error(JSON.stringify(files.error)); |
| 128 |
pin(app.db, alice.id, forge.id); |
| 129 |
pin(app.db, alice.id, getRepo(app.db, "bob", "notes")!.id); |
| 130 |
const issue = createIssue(app.db, { repoId: forge.id, authorId: bob.id, title: "An issue", body: "body" }); |
| 131 |
if (!issue.ok) throw new Error(issue.error); |
| 132 |
const pull = createIssue(app.db, { repoId: forge.id, authorId: bob.id, title: "A pull", body: "body", kind: "pull" }); |
| 133 |
if (!pull.ok) throw new Error(pull.error); |
| 134 |
app.db.run("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'topic', '', 'h')", [pull.value.id]); |
| 135 |
servers = await serve(app); |
| 136 |
const signin = await fetch(`${servers.url}/signin`, { |
| 137 |
method: "POST", |
| 138 |
headers: { origin: servers.url }, |
| 139 |
body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), |
| 140 |
redirect: "manual", |
| 141 |
}); |
| 142 |
cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; |
| 143 |
}, 60_000); |
| 144 |
afterAll(() => { |
| 145 |
servers.stop(); |
| 146 |
app.close(); |
| 147 |
rmSync(root, { recursive: true, force: true }); |
| 148 |
}); |
| 149 |
|
| 150 |
test("every GET page is within its counted work, warm, naming its slowest phase", async () => { |
| 151 |
const { walked, unfixtured } = plan(); |
| 152 |
expect(walked.length).toBeGreaterThan(20); |
| 153 |
const judgeClock = process.env.FORGE_JUDGE_CLOCK === "1"; |
| 154 |
const findings: string[] = []; |
| 155 |
const table: string[] = []; |
| 156 |
for (const [pattern, path] of walked) { |
| 157 |
const init = { headers: { cookie }, redirect: "manual" as const }; |
| 158 |
await (await fetch(`${servers.url}${path}`, init)).arrayBuffer(); |
| 159 |
const w = await pageWork(app.db, `${servers.url}${path}`, init); |
| 160 |
if (w.status >= 500) findings.push(`${pattern}: answered ${w.status}`); |
| 161 |
if (w.status >= 300) continue; |
| 162 |
const top = w.phases[0]; |
| 163 |
table.push(`${pattern}\t${w.statements} sql\t${w.git} git\t${w.ms.toFixed(1)} ms\t${top ? `${top.name} ${top.ms.toFixed(1)}` : "-"}`); |
| 164 |
findings.push(...judge(pattern, w, judgeClock)); |
| 165 |
} |
| 166 |
console.log(`pages walked warm (route, statements, git, clock, slowest phase):\n${table.join("\n")}`); |
| 167 |
console.log(`no fixture, not walked (${unfixtured.length}):\n${unfixtured.join("\n")}`); |
| 168 |
expect(findings).toEqual([]); |
| 169 |
}, 300_000); |
| 170 |
|
| 171 |
test("a page over its counted work is named with its route and slowest phase", () => { |
| 172 |
const w: PageWork = { status: 200, statements: 99, git: 20, ms: 400, phases: [{ name: "git", ms: 300 }, { name: "render", ms: 5 }] }; |
| 173 |
expect(judge("/x", w, false)).toEqual([ |
| 174 |
"/x: 99 SQL statements, budget 60 (slowest phase git 300.0 ms)", |
| 175 |
"/x: 20 git processes, budget 8 (slowest phase git 300.0 ms)", |
| 176 |
]); |
| 177 |
expect(judge("/x", w, true).at(-1)).toBe("/x: 400 ms, budget 150 ms (slowest phase git 300.0 ms)"); |
| 178 |
expect(judge("/x", { ...w, statements: 3, git: 0, ms: 5 }, true)).toEqual([]); |
| 179 |
}); |
| 180 |
|
| 181 |
test("a route the fixture cannot fill is listed, not skipped silently", () => { |
| 182 |
const { walked, unfixtured } = plan(); |
| 183 |
expect(unfixtured.length).toBeGreaterThan(0); |
| 184 |
expect(unfixtured.every((p) => p.includes(":") || p.includes("*"))).toBe(true); |
| 185 |
expect(walked.some(([p]) => p === "/:owner/:name/issues/:number")).toBe(true); |
| 186 |
}); |