Public: anyone can read this; the crew can change it.
Each route's hourly p50 and p95 reads in the terminal and as a boards source, slowest first #16082
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16065-each-route-s-hourly-p50-and-p95-reads-in → main 0 commits fork
6 files
changed +134
−1
docs/features/performance-watcher.md +3 −1
| @@ -8,7 +8,9 @@ row: Performance watcher | ||
| 8 | 8 | |
| 9 | 9 | `history.ts` (#11477): every tick the duty folds the record's new samples into an in-memory ledger under the hour each sample happened in (so 10:50 traffic is never stamped 11:00) and upserts one row per route pattern (route, hour, n, p50, p95) into `route_hour`, so a row is the hour's p50 and p95, not the last 15 minutes'. At most 5,000 samples per route and hour are kept (past it the row stands on the first), at most 200 routes (the busiest) and 168 hours; older rows go in the same write. A restart starts an empty ledger: an empty record writes nothing, and a row is never replaced by one with fewer requests, so the hours before the restart keep the fuller row. Admins read them at `GET /api/v1/telemetry/routes?hours=N` (1 to 168, default 24; else 400). |
| 10 | 10 | |
| 11 | Not yet: a synthetic probe for routes with no traffic (a client-written label would let anyone hide requests; it needs an admin-only door first), route-hour stats in the terminal (findings reach it as issues; the MCP `system` `kind: routes` reads the live routes), browser timings, the board page, release before/after. | |
| 11 | The last 24 hours read in the terminal's System screen (admin who is a person; the five slowest p95 first, with n, p50, p95 and the budget) and as the admin-only `route_hour` boards source (route, hour, n, p50, p95; `slowestRoutes` is one indexed query). | |
| 12 | ||
| 13 | Not yet: a synthetic probe for routes with no traffic (a client-written label would let anyone hide requests; it needs an admin-only door first), browser timings, release before/after. | |
| 12 | 14 | |
| 13 | 15 | ## Sampling the main thread: `FORGE_PROFILE=1` (#12455) |
| 14 | 16 |
src/boards/guide.ts +2
| @@ -34,6 +34,7 @@ export const GUIDE: Readonly<Record<string, Guide>> = { | ||
| 34 | 34 | tokens: { name: "API tokens", rows: "tokens", kind: "events" }, |
| 35 | 35 | adminActions: { name: "Admin actions", rows: "admin actions", kind: "events" }, |
| 36 | 36 | machine: { name: "Machine readings", rows: "15-second readings", kind: "samples", value: "cpu_cores", filter: { column: "target", op: "eq", value: "forge" } }, |
| 37 | route_hour: { name: "Route speed by hour", rows: "route-hours", kind: "samples", value: "p95", filter: { column: "route", op: "eq", value: "/" } }, | |
| 37 | 38 | efficiency: { name: "Efficiency by cause", rows: "hourly readings", kind: "samples", value: "value", filter: { column: "cause", op: "eq", value: "npm-registry-bytes" } }, |
| 38 | 39 | reach: { name: "Visitors and page speed", rows: "daily figures", kind: "samples", value: "value", filter: { column: "metric", op: "eq", value: "visitors" } }, |
| 39 | 40 | app_vital: { name: "App page speed", rows: "app-days", kind: "samples", value: "load_p75" }, |
| @@ -73,6 +74,7 @@ const UNITS: Readonly<Record<string, string>> = { | ||
| 73 | 74 | const UNIT_OF: Readonly<Record<string, Readonly<Record<string, string>>>> = { |
| 74 | 75 | reach: { value: "visitors, ms or a score, by metric" }, |
| 75 | 76 | spawnTime: { value: "ms" }, |
| 77 | route_hour: { p50: "ms", p95: "ms" }, | |
| 76 | 78 | load: { load: "runnable tasks" }, |
| 77 | 79 | }; |
| 78 | 80 |
src/boards/sources.ts +16
| @@ -166,6 +166,22 @@ export const SOURCES: Readonly<Record<string, Source>> = { | ||
| 166 | 166 | n: "requests in that hour with that pattern, method, status class, role and surface", |
| 167 | 167 | }, |
| 168 | 168 | }, |
| 169 | // Each route's p50 and p95 by the hour (`perf/history.ts`, #11477): admin only. `hour` is | |
| 170 | // the hour's start as a time, so a board can chart a route's p95 by hour. | |
| 171 | route_hour: { | |
| 172 | table: "route_hour", | |
| 173 | owner: "admin", | |
| 174 | select: | |
| 175 | "SELECT route, strftime('%Y-%m-%dT%H:00:00.000Z', hour * 3600, 'unixepoch') AS hour, n, p50, p95 FROM route_hour", | |
| 176 | time: "hour", | |
| 177 | columns: { route: "text", hour: "time", n: "number", p50: "number", p95: "number" }, | |
| 178 | docs: { | |
| 179 | route: "the route pattern that matched, never a path", | |
| 180 | n: "requests in that hour", | |
| 181 | p50: "median answer time in that hour, in ms", | |
| 182 | p95: "95th percentile answer time in that hour, in ms", | |
| 183 | }, | |
| 184 | }, | |
| 169 | 185 | // Browser sign-ins: one row per session. The token hash and the session |
| 170 | 186 | // id are not listed. Failed sign-ins are not a row anywhere; they are the |
| 171 | 187 | // `usage` rows for POST /signin with a 4xx status. |
src/perf/history.ts +16
| @@ -104,3 +104,19 @@ export function readHours(raw: string | null): number | null { | ||
| 104 | 104 | const n = Number(raw); |
| 105 | 105 | return n >= 1 && n <= MAX_HOURS ? n : null; |
| 106 | 106 | } |
| 107 | ||
| 108 | /** | |
| 109 | * The last `hours` hours' route rows, slowest p95 first (ties: the busier route, then the name), | |
| 110 | * at most `limit`. One query on the `hour` index. | |
| 111 | */ | |
| 112 | export function slowestRoutes(db: Database, hours: number, limit: number, now: number = Date.now()): RouteHour[] { | |
| 113 | const last = Math.floor(now / HOUR_MS); | |
| 114 | return db | |
| 115 | .query<RouteHour, [number, number, number]>( | |
| 116 | `SELECT route, hour, n, p50, p95 FROM ( | |
| 117 | SELECT route, MAX(hour) AS hour, SUM(n) AS n, MAX(p50) AS p50, MAX(p95) AS p95 | |
| 118 | FROM route_hour WHERE hour > ?1 AND hour <= ?2 GROUP BY route) | |
| 119 | ORDER BY p95 DESC, n DESC, route LIMIT ?3`, | |
| 120 | ) | |
| 121 | .all(last - hours, last, limit); | |
| 122 | } |
src/tui/screens/system.ts +25
| @@ -57,6 +57,8 @@ import { dbSize, dbSizeNeed, type DbSize } from "../../system/db_size.ts"; | ||
| 57 | 57 | import { footprintVerdict, lastFootprint, smallWords } from "../../system/footprint.ts"; |
| 58 | 58 | import { lastProof } from "../../release/prove.ts"; |
| 59 | 59 | import { leastUsed, routeUsage, WINDOW_DAYS } from "../../core/usage_report.ts"; |
| 60 | import { slowestRoutes } from "../../perf/history.ts"; | |
| 61 | import { budgetFor } from "../../perf/budgets.ts"; | |
| 60 | 62 | import { knownPatterns } from "../../http/server.ts"; |
| 61 | 63 | import type { PaletteEntry } from "./entries.ts"; |
| 62 | 64 | import { shapeWords } from "../../engine/mode.ts"; |
| @@ -228,6 +230,27 @@ export function routeUseLines(session: Session, inner = WIDTH - 6): string[] { | ||
| 228 | 230 | return ["Least used pages and tools", truncate(`${WINDOW_DAYS} days: ${rows.map((r) => `${r.pattern} ${r.total}`).join(", ")}`, inner)]; |
| 229 | 231 | } |
| 230 | 232 | |
| 233 | const SLOW_ROUTES = 5; | |
| 234 | ||
| 235 | /** | |
| 236 | * /system's "Slowest routes, last 24 h" (#16065): the route patterns with the highest hourly p95, | |
| 237 | * with requests, p50, p95 and the budget beside each. Empty for anyone but a forge admin who is | |
| 238 | * a person, and when no hour is stored. | |
| 239 | */ | |
| 240 | export function slowRouteLines(session: Session, now = Date.now(), inner = WIDTH - 6): string[] { | |
| 241 | if (!mayGrant(session)) return []; | |
| 242 | const rows = slowestRoutes(session.app.db, 24, SLOW_ROUTES, now); | |
| 243 | if (rows.length === 0) return []; | |
| 244 | const ms = (x: number): string => `${Math.round(x)} ms`; | |
| 245 | return [ | |
| 246 | "Slowest routes, last 24 h (n, p50, p95, budget)", | |
| 247 | ...rows.map((r) => { | |
| 248 | const b = budgetFor(r.route); | |
| 249 | return truncate(`${pad(truncate(r.route, 24), 24)} ${pad(String(r.n), 6, "right")} ${pad(ms(r.p50), 8, "right")} ${pad(ms(r.p95), 8, "right")} ${b ? `<${b.p95Ms} ms` : "—"}`, inner); | |
| 250 | }), | |
| 251 | ]; | |
| 252 | } | |
| 253 | ||
| 231 | 254 | /** |
| 232 | 255 | * The database's size, as /system's "Database size" says it (#12010): the |
| 233 | 256 | * file, its WAL, the room inside and its growth, then the largest tables |
| @@ -396,6 +419,7 @@ export function systemScreen(ctx: Ctx, lines: readonly string[], session: Sessio | ||
| 396 | 419 | const db = reads ? dbSizeLines(session, dbSize.peek(session.app.db, session.app.config.dataDir, session.app.log), true) : []; |
| 397 | 420 | const footprint = footprintLines(session); |
| 398 | 421 | const routes = routeUseLines(session); |
| 422 | const slow = slowRouteLines(session); | |
| 399 | 423 | const buttons = dialog(s, area, ctx.theme.c, { |
| 400 | 424 | title: "System", |
| 401 | 425 | body: [ |
| @@ -407,6 +431,7 @@ export function systemScreen(ctx: Ctx, lines: readonly string[], session: Sessio | ||
| 407 | 431 | ...db, |
| 408 | 432 | ...(footprint.length === 0 ? [] : ["", ...footprint]), |
| 409 | 433 | ...(routes.length === 0 ? [] : ["", ...routes]), |
| 434 | ...(slow.length === 0 ? [] : ["", ...slow]), | |
| 410 | 435 | ...(offered ? ["", ...readerLines(session)] : []), |
| 411 | 436 | ...(features.length === 0 ? [] : ["", ...features]), |
| 412 | 437 | ...(offered ? ["", grantLine(session), "g: grant or take back telemetry:read"] : []), |
test/route-hours-read.test.ts added +72
| @@ -0,0 +1,72 @@ | ||
| 1 | /** | |
| 2 | * Route-hour rows read (#16065): slowest first in the terminal, admin only, and as a boards source. | |
| 3 | */ | |
| 4 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 5 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 6 | import { tmpdir } from "node:os"; | |
| 7 | import { join } from "node:path"; | |
| 8 | import { createApp, type App } from "../src/app.ts"; | |
| 9 | import { SOURCES } from "../src/boards/sources.ts"; | |
| 10 | import { createUser, type User } from "../src/core/users.ts"; | |
| 11 | import { HOUR_MS, slowestRoutes } from "../src/perf/history.ts"; | |
| 12 | import { Session } from "../src/tui/forge.ts"; | |
| 13 | import { slowRouteLines } from "../src/tui/screens/system.ts"; | |
| 14 | import { quietLogs } from "./helpers/globals.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | let dir: string; | |
| 18 | let app: App; | |
| 19 | let admin: User; | |
| 20 | let member: User; | |
| 21 | const now = Date.parse("2026-11-23T12:30:00Z"); | |
| 22 | const hour = Math.floor(now / HOUR_MS); | |
| 23 | const id = (user: User) => ({ sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user }); | |
| 24 | ||
| 25 | beforeAll(async () => { | |
| 26 | dir = await mkdtemp(join(tmpdir(), "forge-route-hours-")); | |
| 27 | app = await createApp({ dataDir: dir }); | |
| 28 | const mk = async (username: string, first = false) => { | |
| 29 | const r = await createUser(app.db, { | |
| 30 | origin: "seed", trusted: true, username, email: `${username}@forge.local`, | |
| 31 | password: "a-good-passphrase", adminIfFirst: first, | |
| 32 | }); | |
| 33 | if (!r.ok) throw new Error(r.error); | |
| 34 | return r.value; | |
| 35 | }; | |
| 36 | admin = await mk("carol", true); | |
| 37 | member = await mk("bob"); | |
| 38 | const put = (r: string, h: number, n: number, p50: number, p95: number) => | |
| 39 | app.db.run("INSERT INTO route_hour (route, hour, n, p50, p95) VALUES (?, ?, ?, ?, ?)", [r, h, n, p50, p95]); | |
| 40 | put("/", hour, 10, 20, 90); | |
| 41 | put("/explore", hour, 5, 100, 900); | |
| 42 | put("/explore", hour - 1, 5, 80, 400); | |
| 43 | put("/boards", hour - 2, 3, 50, 300); | |
| 44 | put("/old", hour - 30, 3, 5000, 9000); | |
| 45 | }); | |
| 46 | afterAll(async () => { | |
| 47 | await rm(dir, { recursive: true, force: true }); | |
| 48 | }); | |
| 49 | ||
| 50 | test("slowest p95 first, within 24 hours only", () => { | |
| 51 | const rows = slowestRoutes(app.db, 24, 10, now); | |
| 52 | expect(rows.map((r) => r.route)).toEqual(["/explore", "/boards", "/"]); | |
| 53 | expect(slowestRoutes(app.db, 24, 2, now)).toHaveLength(2); | |
| 54 | }); | |
| 55 | ||
| 56 | test("an admin reads the lines with the budget; a member reads nothing", () => { | |
| 57 | const lines = slowRouteLines(new Session(app, id(admin)), now); | |
| 58 | expect(lines[0]).toContain("Slowest routes"); | |
| 59 | expect(lines[1]).toContain("/explore"); | |
| 60 | expect(lines[1]).toContain("900 ms"); | |
| 61 | expect(lines[1]).toContain("<800 ms"); | |
| 62 | expect(lines.join("\n")).not.toContain("/old"); | |
| 63 | expect(slowRouteLines(new Session(app, id(member)), now)).toEqual([]); | |
| 64 | }); | |
| 65 | ||
| 66 | test("route_hour is an admin source with its columns, hour as a time", () => { | |
| 67 | const s = SOURCES.route_hour!; | |
| 68 | expect(s.owner).toBe("admin"); | |
| 69 | expect(Object.keys(s.columns)).toEqual(["route", "hour", "n", "p50", "p95"]); | |
| 70 | const row = app.db.query<{ hour: string }, []>(`SELECT hour FROM (${s.select}) LIMIT 1`).get(); | |
| 71 | expect(row!.hour).toMatch(/^\d{4}-\d\d-\d\dT\d\d:00:00\.000Z$/); | |
| 72 | }); |
new above
offline