Public: anyone can read this; the crew can change it.
perfRecord, loopWatch, stallBlame and lagMinutes belong to a forge, so page-budget and perf-server-hook stop failing in a full gate #12667
scout/forge:scout/12615-perfrecord-loopwatch-stallblame-and-lagm → main 7 commits fork
30 files
changed +238
−122
- src/apps/proxy.ts
- src/http/api.ts
- src/http/mcp/tools/system.ts
- src/http/server.ts
- src/main.ts
- src/metrics/forge.ts
- src/perf/crawlers.ts
- src/perf/live.ts
- src/tui/screens/loopstalls.ts
- src/tui/screens/traffic.ts
- src/web/pages/scrapes.ts
- src/web/pages/security.ts
- src/web/pages/system.ts
- src/web/pages/traffic.ts
- src/web/studio/kpi_probe.ts
- test/helpers/stall-state.ts
- test/loop-stall-by-route.test.ts
- test/mcp-system-routes.test.ts
- test/mcp-system-stalls.test.ts
- test/metrics-forge.test.ts
- test/pages-budget.test.ts
- test/perf-lag-minutes.test.ts
- test/perf-loop-jobs.test.ts
- test/perf-per-forge.test.ts
- test/perf-server-hook.test.ts
- test/perf-stall-blame.test.ts
- test/relay-stall-name.test.ts
- test/spawn-in-process-visible.test.ts
- test/stall-in-flight.test.ts
- test/traffic-history.test.ts
src/apps/proxy.ts +2 −2
| @@ -70,7 +70,7 @@ import { holdPortRequest, holdPortSocket } from "./handoff.ts"; | ||
| 70 | 70 | import { secureFor } from "../http/cookie_secure.ts"; |
| 71 | 71 | import { currentLoopLag, forgeWaited, LAG_WINDOW_MS } from "../perf/loop_worst.ts"; |
| 72 | 72 | import { HOOK_MAX_BYTES, HOOK_PREFIX, hookPath, readCapped } from "./hook_relay.ts"; |
| 73 | import { loopWatch } from "../perf/live.ts"; | |
| 73 | import { perfOf } from "../perf/live.ts"; | |
| 74 | 74 | import { heldBy, noteRelayWait, stallTiming } from "./relay_wait.ts"; |
| 75 | 75 | import { cspNonce, htmlAnswer, readPage, rewritable, TOOLBAR_ASSET_PATH, TOOLBAR_ASSET_PREFIX, toolbarResponse, withToolbar, type PillWhere } from "./toolbar.ts"; |
| 76 | 76 | |
| @@ -533,7 +533,7 @@ function relayUpstream( | ||
| 533 | 533 | // What held a wait that was the forge's (#11000): the stalls of the lag's |
| 534 | 534 | // window, read only then — at most 40 of them, so the common relay pays nothing. |
| 535 | 535 | const now = performance.now(); |
| 536 | const held = forgeWaited(lag, headersMs) ? heldBy(loopWatch.within(now - LAG_WINDOW_MS, now)) : []; | |
| 536 | const held = forgeWaited(lag, headersMs) ? heldBy(perfOf(runtime.app).loopWatch.within(now - LAG_WINDOW_MS, now)) : []; | |
| 537 | 537 | // Production's slowest wait, for the app's speed card (`apps/relay_wait.ts`). |
| 538 | 538 | if (target.env === "production") noteRelayWait(target.app.id, headersMs, lag, Date.now(), held); |
| 539 | 539 | if (held.length > 0 && forgeAdmin(runtime, who)) stallFor.set(request, stallTiming(held)); |
src/http/api.ts +1 −1
| @@ -3523,7 +3523,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3523 | 3523 | router.add("GET", "/metrics", (ctx) => { |
| 3524 | 3524 | const denied = accountAdmin(ctx, "ro"); |
| 3525 | 3525 | if (denied) return denied; |
| 3526 | return new Response(egressMetrics(egressRowsFor(ctx.app), egressTotalsFor(ctx.app)) + forgeMetrics(ctx.app.db, appSources(ctx.app)), { | |
| 3526 | return new Response(egressMetrics(egressRowsFor(ctx.app), egressTotalsFor(ctx.app)) + forgeMetrics(ctx.app.db, appSources(ctx.app), Date.now(), ctx.app), { | |
| 3527 | 3527 | headers: { |
| 3528 | 3528 | "content-type": "text/plain; version=0.0.4; charset=utf-8", |
| 3529 | 3529 | "cache-control": "no-store", |
src/http/mcp/tools/system.ts +4 −4
| @@ -4,7 +4,7 @@ import { scriptErrorCounts } from "../../script_errors.ts"; | ||
| 4 | 4 | import { violationsByDirective, windowStart } from "../../csp_report.ts"; |
| 5 | 5 | import { countRead } from "../../../system/telemetry_reads.ts"; |
| 6 | 6 | import { stallHour, TOP_ROUTES } from "../../../metrics/stalls.ts"; |
| 7 | import { lagMinutes, loopWatch, perfRecord } from "../../../perf/live.ts"; | |
| 7 | import { perfOf } from "../../../perf/live.ts"; | |
| 8 | 8 | import { LAG_BAR_MS, overBar } from "../../../perf/lag_minutes.ts"; |
| 9 | 9 | import { slowestRoutes } from "../../../perf/watch.ts"; |
| 10 | 10 | import { footing, riskLines } from "../../../system/footing.ts"; |
| @@ -28,7 +28,7 @@ export const tool: Tool = { | ||
| 28 | 28 | const reader = telemetryReader(ctx.app, user); |
| 29 | 29 | if (!reader) return bad("forbidden", "only an admin of this forge, or an agent granted telemetry:read, reads its route timings"); |
| 30 | 30 | noteRead(ctx.app, user, reader, "system"); |
| 31 | return good({ routes: slowestRoutes(perfRecord, Date.now()) }); | |
| 31 | return good({ routes: slowestRoutes(perfOf(ctx.app).perfRecord, Date.now()) }); | |
| 32 | 32 | } |
| 33 | 33 | if (args["kind"] === "size") { |
| 34 | 34 | const reader = telemetryReader(ctx.app, user); |
| @@ -63,11 +63,11 @@ export const tool: Tool = { | ||
| 63 | 63 | noteRead(ctx.app, user, reader, "system"); |
| 64 | 64 | const out = systemWithReaders(ctx.app, reader); |
| 65 | 65 | countRead("system", user.username); |
| 66 | const stalls = stallHour(loopWatch.tally(performance.now())) | |
| 66 | const stalls = stallHour(perfOf(ctx.app).loopWatch.tally(performance.now())) | |
| 67 | 67 | .slice(0, TOP_ROUTES) |
| 68 | 68 | .map((r) => ({ cause: r.route, ms: Math.round(r.ms), count: r.count })); |
| 69 | 69 | // The same 20 minutes /system draws (#11220); `what` is null when no stall past 200 ms was named. |
| 70 | const rows = lagMinutes.readings(); | |
| 70 | const rows = perfOf(ctx.app).lagMinutes.readings(); | |
| 71 | 71 | const readings = rows.map((r) => ({ at: new Date(r.at).toISOString(), ms: Math.round(r.ms * 10) / 10, what: r.what })); |
| 72 | 72 | // At most 0.8 s on the walk (#11800); the last measurement, or null, when it is slower. |
| 73 | 73 | const largest = await largestDirs.read(ctx.app.config.dataDir); |
src/http/server.ts +8 −5
| @@ -553,7 +553,7 @@ import { | ||
| 553 | 553 | takeDbTime, |
| 554 | 554 | type RouteTiming, |
| 555 | 555 | } from "../core/route_timing.ts"; |
| 556 | import { captureStore, inFlight, loopWatch, perfRecord, readLedger, stallBlame } from "../perf/live.ts"; | |
| 556 | import { captureStore, inFlight, perfOf, readLedger } from "../perf/live.ts"; | |
| 557 | 557 | import { childLabel, markChildRequest, noteChildRequest } from "../perf/child_time.ts"; |
| 558 | 558 | import { parseHostedChildName } from "../spawn/placement.ts"; |
| 559 | 559 | import { childHost } from "../spawn/door.ts"; |
| @@ -2674,8 +2674,11 @@ export async function serve(app: App): Promise<Servers> { | ||
| 2674 | 2674 | // map increment; a refused event would be a programming error, not worth a 500. |
| 2675 | 2675 | const counters = new Map<Database, UsageCounter>(); |
| 2676 | 2676 | const timings = new Map<Database, RouteTiming>(); |
| 2677 | // This forge's own record and loop watch (#12615), looked up once: | |
| 2678 | // a request pays a property read. | |
| 2679 | const perf = perfOf(app); | |
| 2677 | 2680 | // The minute ledger's history: one write a minute, never on a request. |
| 2678 | const trafficWriter = createTrafficWriter(app.db, perfRecord.ledger, app.log); | |
| 2681 | const trafficWriter = createTrafficWriter(app.db, perf.perfRecord.ledger, app.log); | |
| 2679 | 2682 | // Page views are buffered by the beacon doors and written here; GitHub's traffic is pulled daily when a token is set. |
| 2680 | 2683 | const viewWriter = createViewWriter(app.db, app.log); |
| 2681 | 2684 | const githubPull = startGithubPull(app.db, app.log); |
| @@ -2710,7 +2713,7 @@ export async function serve(app: App): Promise<Servers> { | ||
| 2710 | 2713 | response.headers.set("server-timing", tag.role === "admin" ? withLag(header, currentLoopLag()) : header); |
| 2711 | 2714 | const end = performance.now(); |
| 2712 | 2715 | const family = familyOf(request.headers.get("user-agent") ?? ""); |
| 2713 | perfRecord.add( | |
| 2716 | perf.perfRecord.add( | |
| 2714 | 2717 | tag.pattern, |
| 2715 | 2718 | { at: Date.now(), ms, signedIn: tag.role !== "anon" && tag.role !== "crawler", crawler: wasCrawler(request), deepStranger: wasDeepStranger(request), internal: wasInternal(request), refused: response.status === 429 || wasShielded(request), phases: parseServerTiming(header) }, |
| 2716 | 2719 | { |
| @@ -2721,7 +2724,7 @@ export async function serve(app: App): Promise<Servers> { | ||
| 2721 | 2724 | repo: response.status < 400 ? repoOf(tag.pattern, new URL(request.url).pathname) : null, |
| 2722 | 2725 | }, |
| 2723 | 2726 | ); |
| 2724 | loopWatch.finished(hostedChild === undefined ? tag.pattern : childLabel(hostedChild), started, end); | |
| 2727 | perf.loopWatch.finished(hostedChild === undefined ? tag.pattern : childLabel(hostedChild), started, end); | |
| 2725 | 2728 | if (hostedChild !== undefined) noteChildRequest(hostedChild, ms); |
| 2726 | 2729 | // Evidence of what was read, and the running capture, if any (never a signed-in person's). |
| 2727 | 2730 | const signedIn = tag.role !== "anon" && tag.role !== "crawler"; |
| @@ -3102,7 +3105,7 @@ async function answerGet( | ||
| 3102 | 3105 | () => app.db.query("select 1").get(), |
| 3103 | 3106 | app.log, |
| 3104 | 3107 | wantsJson ? currentLoopLag() : undefined, |
| 3105 | wantsJson ? (stallBlame.worst(performance.now(), LAG_WINDOW_MS, 1)[0]?.what ?? null) : null, | |
| 3108 | wantsJson ? (perfOf(app).stallBlame.worst(performance.now(), LAG_WINDOW_MS, 1)[0]?.what ?? null) : null, | |
| 3106 | 3109 | ); |
| 3107 | 3110 | noteDbTime(request, performance.now() - dbStart); |
| 3108 | 3111 | // The release proof learns which release answered, old pod or new; a |
src/main.ts +7 −7
| @@ -132,7 +132,7 @@ import { watchStalls } from "./kernel/loop_lag.ts"; | ||
| 132 | 132 | import { startPerfWatcher, TICK_MS } from "./perf/duty.ts"; |
| 133 | 133 | import { during } from "./perf/jobs.ts"; |
| 134 | 134 | import { stallLine } from "./perf/loop.ts"; |
| 135 | import { bootStall, gitRecord, lagMinutes, loopWatch, perfRecord, stallBlame } from "./perf/live.ts"; | |
| 135 | import { adoptPerf, bootStall, createPerf, gitRecord, noteWake } from "./perf/live.ts"; | |
| 136 | 136 | import { keepFlowReadings, KEPT_MS } from "./flow/kept.ts"; |
| 137 | 137 | import { startKeeping } from "./flow/keep.ts"; |
| 138 | 138 | import { keepForgePages } from "./flow/pages.ts"; |
| @@ -269,13 +269,12 @@ if (parentProblem !== null) { | ||
| 269 | 269 | // second is a warning that names its minute (kernel/loop_lag.ts, #6957), |
| 270 | 270 | // draining included. |
| 271 | 271 | const loopLog = createLog("loop"); |
| 272 | // The forge's own readings (#12615), begun before it exists and adopted once it does. | |
| 273 | const perf = createPerf(); | |
| 272 | 274 | watchStalls(loopLog, undefined, undefined, undefined, (late, at) => { |
| 273 | 275 | bootStall.note(late, at); |
| 274 | lagMinutes.note(late, at); | |
| 275 | const stall = loopWatch.stalled(late, at); | |
| 276 | if (stall === null) return; | |
| 277 | loopLog.warn(stallLine(stall)); | |
| 278 | stallBlame.note(stall); | |
| 276 | const stall = noteWake(perf, late, at); | |
| 277 | if (stall !== null) loopLog.warn(stallLine(stall)); | |
| 279 | 278 | }); |
| 280 | 279 | |
| 281 | 280 | const previewKey = previewIdentityKey(process.env); |
| @@ -363,6 +362,7 @@ const app = createApp({ | ||
| 363 | 362 | }), |
| 364 | 363 | }), |
| 365 | 364 | }); |
| 365 | adoptPerf(app, perf); | |
| 366 | 366 | |
| 367 | 367 | // A statement that holds the thread past 200 ms names its caller (#6957). |
| 368 | 368 | timeStatements(app.db, createLog("sql")); |
| @@ -397,7 +397,7 @@ if (readOnly) { | ||
| 397 | 397 | const bootSignup = signupFromEnv(process.env.FORGE_SIGNUP); |
| 398 | 398 | if (bootSignup.ok && bootSignup.value !== null) |
| 399 | 399 | setSignup(app.db, bootSignup.value, BOOT_ACTOR); |
| 400 | startPerfWatcher(app, perfRecord, Date.now, TICK_MS, gitRecord); | |
| 400 | startPerfWatcher(app, perf.perfRecord, Date.now, TICK_MS, gitRecord); | |
| 401 | 401 | // A release shipped by hand is recorded as live at boot, so live estimates and the base rate |
| 402 | 402 | // resolve whichever way a release ships; estimates of "done" come due as time passes (#11955). |
| 403 | 403 | const byHand = recordByHand(app.db); |
src/metrics/forge.ts +8 −3
| @@ -18,7 +18,7 @@ import type { EnvState } from "../apps/runtime.ts"; | ||
| 18 | 18 | import { EDGES_MS, readTotals } from "../core/route_timing.ts"; |
| 19 | 19 | import { closesFromBody } from "../core/pulls.ts"; |
| 20 | 20 | import { telemetryReadsFamily } from "../system/telemetry_reads.ts"; |
| 21 | import { loopWatch } from "../perf/live.ts"; | |
| 21 | import { perfOf } from "../perf/live.ts"; | |
| 22 | 22 | import { stallFamilies } from "./stalls.ts"; |
| 23 | 23 | import { openMetrics, type Family, type HistogramSample, type Sample } from "./openmetrics.ts"; |
| 24 | 24 | |
| @@ -329,11 +329,16 @@ export function appFamilies({ apps, cells }: AppSources): Family[] { | ||
| 329 | 329 | ]; |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | /** The forge's families as Prometheus text (no `# EOF`: the body is `text/plain; version=0.0.4`). */ | |
| 332 | /** | |
| 333 | * The forge's families as Prometheus text, its stalls its own (#12615) | |
| 334 | * (no `# EOF`: the body is `text/plain; version=0.0.4`). | |
| 335 | */ | |
| 333 | 336 | export function forgeMetrics( |
| 334 | 337 | db: Database, |
| 335 | 338 | sources: AppSources = { apps: [], cells: [] }, |
| 336 | 339 | now: number = Date.now(), |
| 340 | /** Whose stalls (`perfOf`); absent, the scrape reports no stall families. */ | |
| 341 | app?: object, | |
| 337 | 342 | ): string { |
| 338 | 343 | return openMetrics([ |
| 339 | 344 | durationFamily(db), |
| @@ -347,6 +352,6 @@ export function forgeMetrics( | ||
| 347 | 352 | askToLiveFamily(db), |
| 348 | 353 | mainRedFamily(db, now), |
| 349 | 354 | telemetryReadsFamily(), |
| 350 | ...stallFamilies(loopWatch.tally(performance.now())), | |
| 355 | ...(app ? stallFamilies(perfOf(app).loopWatch.tally(performance.now())) : []), | |
| 351 | 356 | ]).replace(/# EOF\n$/, ""); |
| 352 | 357 | } |
src/perf/crawlers.ts +2 −2
| @@ -6,11 +6,11 @@ | ||
| 6 | 6 | |
| 7 | 7 | import type { App } from "../app.ts"; |
| 8 | 8 | import { crawlRefusalScopes, crawlRefusals } from "../http/crawl.ts"; |
| 9 | import { perfRecord } from "./live.ts"; | |
| 9 | import { perfOf } from "./live.ts"; | |
| 10 | 10 | import { WINDOW_MS, crawlerShare } from "./record.ts"; |
| 11 | 11 | |
| 12 | 12 | export function crawlReport(app: App, now = Date.now()) { |
| 13 | const share = crawlerShare(perfRecord, now - WINDOW_MS); | |
| 13 | const share = crawlerShare(perfOf(app).perfRecord, now - WINDOW_MS); | |
| 14 | 14 | return { |
| 15 | 15 | window_minutes: WINDOW_MS / 60_000, |
| 16 | 16 | requests: share.requests, |
src/perf/live.ts +64 −15
| @@ -1,9 +1,13 @@ | ||
| 1 | /** The process's one record, git record (#8158) and loop watch (#7559): what the server feeds and the duty reads. */ | |
| 1 | /** | |
| 2 | * The process's git record (#8158), in-flight requests and captures, and each | |
| 3 | * forge's own route record and loop watch (#7559, #12615): what the server | |
| 4 | * feeds and the duty reads. | |
| 5 | */ | |
| 2 | 6 | |
| 3 | 7 | import { createBootStall } from "./boot_stall.ts"; |
| 4 | 8 | import { createGitRecord } from "./git.ts"; |
| 5 | 9 | import { jobWatch } from "./jobs.ts"; |
| 6 | import { createLoopWatch } from "./loop.ts"; | |
| 10 | import { createLoopWatch, type Stall } from "./loop.ts"; | |
| 7 | 11 | import { createInFlight } from "./in_flight.ts"; |
| 8 | 12 | import { childRequestLabel } from "./child_time.ts"; |
| 9 | 13 | import { patternOf } from "../http/usage_tag.ts"; |
| @@ -13,26 +17,71 @@ import { createStallBlame } from "./stall_blame.ts"; | ||
| 13 | 17 | import { createPerfRecord } from "./record.ts"; |
| 14 | 18 | import { createLagMinutes } from "./lag_minutes.ts"; |
| 15 | 19 | |
| 16 | export const perfRecord = createPerfRecord(); | |
| 17 | 20 | /** The requests the server is answering now (#11478): begun and ended around each one in `http/server.ts`. */ |
| 18 | 21 | export const inFlight = createInFlight<Request>((request) => childRequestLabel(request) ?? patternOf(request)); |
| 19 | /** | |
| 20 | * A stall names the requests that ran across it, finished or still in flight | |
| 21 | * (#11478); when none did, the background jobs that did (#10834). A job that | |
| 22 | * held the loop for most of a stall is named ahead of the requests that waited | |
| 23 | * behind it, and a name that only brushed the stall is left out (`loop.ts`). | |
| 24 | */ | |
| 25 | export const loopWatch = createLoopWatch(undefined, jobWatch.across, inFlight.since, jobWatch.held, jobWatch.awaited); | |
| 26 | 22 | export const gitRecord = createGitRecord(); |
| 27 | 23 | /** The worst stall in this process's first five minutes, read by the release keeper (#8848). */ |
| 28 | 24 | export const bootStall = createBootStall(); |
| 29 | 25 | /** What was read from repositories (`reads.ts`), and the one bounded capture an admin may run (`capture.ts`). */ |
| 30 | 26 | export const readLedger = createReadLedger(); |
| 31 | 27 | export const captureStore = createCaptureStore(); |
| 32 | /** Each stall over 200 ms with what ran (#11069): the last hour's worst on /system, the last 10 s's on `/healthz`. */ | |
| 33 | export const stallBlame = createStallBlame(); | |
| 28 | ||
| 34 | 29 | /** |
| 35 | * The last 20 minutes' worst loop lag (#11220), each blamed on the worst stall | |
| 36 | * `stallBlame` named in it: /system and the MCP `system` tool read it. | |
| 30 | * One forge's readings (#12615): its route record, loop watch, stall blame | |
| 31 | * and lag minutes. One set for the process counted every forge in it as one, | |
| 32 | * so in a test run one file's slow request was a row in the next file's | |
| 33 | * /system and page budgets; a hosted child's were its parent's. | |
| 34 | * | |
| 35 | * A stall names the requests that ran across it, finished or still in flight | |
| 36 | * (#11478); when none did, the background jobs that did (#10834). A job that | |
| 37 | * held the loop for most of a stall is named ahead of the requests that waited | |
| 38 | * behind it, and a name that only brushed the stall is left out (`loop.ts`). | |
| 39 | * Each stall over 200 ms with what ran (#11069) is kept in `stallBlame`: the | |
| 40 | * last hour's worst on /system, the last 10 s's on `/healthz`; `lagMinutes` | |
| 41 | * holds the last 20 minutes' worst loop lag (#11220), each blamed on the | |
| 42 | * worst stall `stallBlame` named in it. | |
| 37 | 43 | */ |
| 38 | export const lagMinutes = createLagMinutes((to, span) => stallBlame.worst(to, span, 1)[0]?.what ?? null); | |
| 44 | export interface Perf { | |
| 45 | readonly perfRecord: ReturnType<typeof createPerfRecord>; | |
| 46 | readonly loopWatch: ReturnType<typeof createLoopWatch>; | |
| 47 | readonly stallBlame: ReturnType<typeof createStallBlame>; | |
| 48 | readonly lagMinutes: ReturnType<typeof createLagMinutes>; | |
| 49 | } | |
| 50 | ||
| 51 | export function createPerf(): Perf { | |
| 52 | const stallBlame = createStallBlame(); | |
| 53 | return { | |
| 54 | perfRecord: createPerfRecord(), | |
| 55 | loopWatch: createLoopWatch(undefined, jobWatch.across, inFlight.since, jobWatch.held, jobWatch.awaited), | |
| 56 | stallBlame, | |
| 57 | lagMinutes: createLagMinutes((to, span) => stallBlame.worst(to, span, 1)[0]?.what ?? null), | |
| 58 | }; | |
| 59 | } | |
| 60 | ||
| 61 | const perfs = new WeakMap<object, Perf>(); | |
| 62 | ||
| 63 | /** This forge's readings, made the first time it is asked for. */ | |
| 64 | export function perfOf(app: object): Perf { | |
| 65 | let perf = perfs.get(app); | |
| 66 | if (perf === undefined) perfs.set(app, (perf = createPerf())); | |
| 67 | return perf; | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * One wake of the loop watcher, `late` ms after it was due: the lag minute, | |
| 72 | * and the stall it was, if it was one, blamed. | |
| 73 | */ | |
| 74 | export function noteWake(perf: Perf, late: number, at: number): Stall | null { | |
| 75 | perf.lagMinutes.note(late, at); | |
| 76 | const stall = perf.loopWatch.stalled(late, at); | |
| 77 | if (stall !== null) perf.stallBlame.note(stall); | |
| 78 | return stall; | |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * `main.ts` watches the loop before its forge exists: what it measured from | |
| 83 | * boot becomes the forge's. | |
| 84 | */ | |
| 85 | export function adoptPerf(app: object, perf: Perf): void { | |
| 86 | perfs.set(app, perf); | |
| 87 | } |
src/tui/screens/loopstalls.ts +6 −6
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | |
| 13 | 13 | import { stallHour } from "../../metrics/stalls.ts"; |
| 14 | 14 | import { HOUR_MS, type RouteStall } from "../../perf/loop.ts"; |
| 15 | import { lagMinutes, loopWatch, stallBlame } from "../../perf/live.ts"; | |
| 15 | import { perfOf } from "../../perf/live.ts"; | |
| 16 | 16 | import { LAG_BAR_MS, LAG_READINGS, overBar, type LagReading } from "../../perf/lag_minutes.ts"; |
| 17 | 17 | import { WORST_STALLS, type BlamedStall } from "../../perf/stall_blame.ts"; |
| 18 | 18 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -34,9 +34,9 @@ export interface WorstNow { | ||
| 34 | 34 | readonly wall: number; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | function worstNow(): WorstNow { | |
| 37 | function worstNow(app: object): WorstNow { | |
| 38 | 38 | const mono = performance.now(); |
| 39 | return { rows: stallBlame.worst(mono, HOUR_MS, WORST_STALLS), mono, wall: Date.now() }; | |
| 39 | return { rows: perfOf(app).stallBlame.worst(mono, HOUR_MS, WORST_STALLS), mono, wall: Date.now() }; | |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
| @@ -46,8 +46,8 @@ function worstNow(): WorstNow { | ||
| 46 | 46 | */ |
| 47 | 47 | export function stallLines( |
| 48 | 48 | session: Session, |
| 49 | rows: readonly RouteStall[] = stallHour(loopWatch.tally(performance.now())), | |
| 50 | worst: WorstNow = worstNow(), | |
| 49 | rows: readonly RouteStall[] = stallHour(perfOf(session.app).loopWatch.tally(performance.now())), | |
| 50 | worst: WorstNow = worstNow(session.app), | |
| 51 | 51 | ): string[] { |
| 52 | 52 | if (!mayRead(session)) return []; |
| 53 | 53 | const col = (s: string) => pad(s, 10, "right"); |
| @@ -71,7 +71,7 @@ export function stallLines( | ||
| 71 | 71 | * start in UTC, its worst ms, `over` when strictly past the bar, and what ran |
| 72 | 72 | * (or "not named"). Empty for anyone the gate refuses. |
| 73 | 73 | */ |
| 74 | export function lagLines(session: Session, rows: readonly LagReading[] = lagMinutes.readings()): string[] { | |
| 74 | export function lagLines(session: Session, rows: readonly LagReading[] = perfOf(session.app).lagMinutes.readings()): string[] { | |
| 75 | 75 | if (!mayRead(session)) return []; |
| 76 | 76 | const head = `Loop lag, last ${String(LAG_READINGS)} minutes`; |
| 77 | 77 | if (rows.length === 0) return [head, "No full minute measured yet."]; |
src/tui/screens/traffic.ts +2 −2
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | import type { Ctx, Screen } from "../app/app.ts"; |
| 9 | 9 | import { binding } from "../app/keys.ts"; |
| 10 | 10 | import { trafficShare, TRAFFIC_WORDS } from "../../flow/kpi_words.ts"; |
| 11 | import { perfRecord } from "../../perf/live.ts"; | |
| 11 | import { perfOf } from "../../perf/live.ts"; | |
| 12 | 12 | import { TRAFFIC_CLASSES } from "../../perf/traffic.ts"; |
| 13 | 13 | import { trafficHistory } from "../../perf/traffic_store.ts"; |
| 14 | 14 | import type { Session } from "../session.ts"; |
| @@ -24,7 +24,7 @@ export function trafficScreen(f: Session, ctx: Ctx): Screen { | ||
| 24 | 24 | const state = new ListState(); |
| 25 | 25 | const lines = (): ListRow[] => { |
| 26 | 26 | if (!may(f)) return []; |
| 27 | const t = trafficHistory(f.app.db, perfRecord.ledger, Date.now(), "15m"); | |
| 27 | const t = trafficHistory(f.app.db, perfOf(f.app).perfRecord.ledger, Date.now(), "15m"); | |
| 28 | 28 | if (t.requests === 0) return [{ title: "No requests in the last 15 minutes", meta: "each finished minute is written down" }]; |
| 29 | 29 | const rows: ListRow[] = [ |
| 30 | 30 | { title: `${t.requests} requests, ${(t.ms / 1000).toFixed(1)} s of answering time`, meta: `${t.refused} refused, about ${t.addresses} client addresses` }, |
src/web/pages/scrapes.ts +2 −2
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| 10 | 10 | import type { Ctx } from "../../http/context.ts"; |
| 11 | 11 | import { repoDir } from "../../git/repos.ts"; |
| 12 | import { perfRecord, captureStore, readLedger } from "../../perf/live.ts"; | |
| 12 | import { perfOf, captureStore, readLedger } from "../../perf/live.ts"; | |
| 13 | 13 | import { groupRequests, type Group } from "../../perf/actors.ts"; |
| 14 | 14 | import { CAPTURE_MAX, CAPTURE_MS } from "../../perf/capture.ts"; |
| 15 | 15 | import { networkTable, parseNetworks, saveNetworks, type Network, type NetworkTable } from "../../perf/network.ts"; |
| @@ -154,7 +154,7 @@ export async function scrapesPage(ctx: Ctx): Promise<Response> { | ||
| 154 | 154 | ); |
| 155 | 155 | }), |
| 156 | 156 | ); |
| 157 | const prefixes = trafficHistory(ctx.app.db, perfRecord.ledger, now, "24h").tops.prefixes; | |
| 157 | const prefixes = trafficHistory(ctx.app.db, perfOf(ctx.app).perfRecord.ledger, now, "24h").tops.prefixes; | |
| 158 | 158 | const cap = captureStore.current(now); |
| 159 | 159 | const groups = cap === null ? [] : groupRequests(cap.entries).filter((g) => g.size >= 3); |
| 160 | 160 | const picked = groups.find((g) => g.id === q.get("group")); |
src/web/pages/security.ts +2 −2
| @@ -17,7 +17,7 @@ import { readBackup } from "../../flow/kpis.ts"; | ||
| 17 | 17 | import type { Ctx } from "../../http/context.ts"; |
| 18 | 18 | import { violationsByDirective } from "../../http/csp_report.ts"; |
| 19 | 19 | import { scriptErrorCounts } from "../../http/script_errors.ts"; |
| 20 | import { perfRecord } from "../../perf/live.ts"; | |
| 20 | import { perfOf } from "../../perf/live.ts"; | |
| 21 | 21 | import { getSignup } from "../../settings/signup.ts"; |
| 22 | 22 | import { trafficHistory } from "../../perf/traffic_store.ts"; |
| 23 | 23 | import { lastDrill } from "../../snapshot/drills.ts"; |
| @@ -277,7 +277,7 @@ export function securityPage(ctx: Ctx): Response { | ||
| 277 | 277 | const db = ctx.app.db; |
| 278 | 278 | const dataDir = ctx.app.config.dataDir; |
| 279 | 279 | |
| 280 | const traffic = trafficHistory(db, perfRecord.ledger, nowMs, "15m"); | |
| 280 | const traffic = trafficHistory(db, perfOf(ctx.app).perfRecord.ledger, nowMs, "15m"); | |
| 281 | 281 | const wrong = trafficAttention(traffic); |
| 282 | 282 | const { admins, adminTokens, added } = peopleRows(db, since, nowIso); |
| 283 | 283 | const { backup, backupAt } = readBackup(db, dataDir, nowMs); |
src/web/pages/system.ts +4 −4
| @@ -37,7 +37,7 @@ import { readerPaces, type ReaderPace } from "../../system/telemetry_reads.ts"; | ||
| 37 | 37 | import { slowPageLine, slowPages, type SlowPage } from "../../perf/watch.ts"; |
| 38 | 38 | import { stallHour } from "../../metrics/stalls.ts"; |
| 39 | 39 | import { HOUR_MS, type RouteStall } from "../../perf/loop.ts"; |
| 40 | import { lagMinutes, loopWatch, stallBlame } from "../../perf/live.ts"; | |
| 40 | import { perfOf } from "../../perf/live.ts"; | |
| 41 | 41 | import { childHours, childHourWords, type ChildHour } from "../../perf/child_time.ts"; |
| 42 | 42 | import { openChildren } from "../../spawn/host.ts"; |
| 43 | 43 | import { LAG_BAR_MS, LAG_READINGS, overBar, type LagReading } from "../../perf/lag_minutes.ts"; |
| @@ -597,7 +597,7 @@ export async function systemPage(ctx: Ctx): Promise<Response> { | ||
| 597 | 597 | const self = owner ? timePhase(ctx.request, "self", () => selfUse(realSelf(ctx.app.config.dataDir))) : null; |
| 598 | 598 | const listed = owner ? await listChildren(ctx.app) : []; |
| 599 | 599 | const children = childrenUse(listed, (bodyId) => childDirOf(ctx.app, bodyId)); |
| 600 | const tally = loopWatch.tally(performance.now()); | |
| 600 | const tally = perfOf(ctx.app).loopWatch.tally(performance.now()); | |
| 601 | 601 | const stalls = timePhase(ctx.request, "stalls", () => stallHour(tally)); |
| 602 | 602 | // The forges open in this process now, and any that ran here in the hour: each with what it took |
| 603 | 603 | // of this thread. |
| @@ -606,7 +606,7 @@ export async function systemPage(ctx: Ctx): Promise<Response> { | ||
| 606 | 606 | const mine = new Set(listed.map((c) => c.name)); |
| 607 | 607 | const inProcess = childHours(Date.now(), tally.hour, openChildren(ctx.app).map((c) => c.name)).filter((r) => mine.has(r.name)); |
| 608 | 608 | const nowMono = performance.now(); |
| 609 | const worst = stallBlame.worst(nowMono, HOUR_MS, WORST_STALLS); | |
| 609 | const worst = perfOf(ctx.app).stallBlame.worst(nowMono, HOUR_MS, WORST_STALLS); | |
| 610 | 610 | // Never waits on the cluster: the last reading, or the declared rows while the first read runs behind. |
| 611 | 611 | const standsOn = footing.peek() ?? declaredFooting("the cluster is being read; reload in a moment"); |
| 612 | 612 | // Never waits on the walk: the last measurement, a new one started behind it when stale. |
| @@ -655,7 +655,7 @@ export async function systemPage(ctx: Ctx): Promise<Response> { | ||
| 655 | 655 | ${footingSection(standsOn)} |
| 656 | 656 | ${offsiteSection(timePhase(ctx.request, "offsite", () => readOffsiteLane(ctx.app.config.dataDir, ctx.app.db)))} |
| 657 | 657 | ${featuresSection(timePhase(ctx.request, "features", () => featureHealth(ctx.app, reader)))} |
| 658 | ${lagSection(lagMinutes.readings())} | |
| 658 | ${lagSection(perfOf(ctx.app).lagMinutes.readings())} | |
| 659 | 659 | <div id="sys-stalls">${stallSection(stalls, worstStallsTable(worst, nowMono, Date.now()))}</div> |
| 660 | 660 | ${owner ? askingSection(askingOf(ctx.app).heavy(), turnStatsOf(ctx.app)) : html``} |
| 661 | 661 | <div id="sys-slow-pages">${slowPagesSection(timePhase(ctx.request, "slowpages", () => slowPagesCached(ctx.app.db)))}</div> |
src/web/pages/traffic.ts +2 −2
| @@ -14,7 +14,7 @@ import { | ||
| 14 | 14 | trafficShare, |
| 15 | 15 | TRAFFIC_WORDS, |
| 16 | 16 | } from "../../flow/kpi_words.ts"; |
| 17 | import { perfRecord } from "../../perf/live.ts"; | |
| 17 | import { perfOf } from "../../perf/live.ts"; | |
| 18 | 18 | import { |
| 19 | 19 | TRAFFIC_CLASSES, |
| 20 | 20 | type TopRow, |
| @@ -118,7 +118,7 @@ export function trafficPage(ctx: Ctx): Response { | ||
| 118 | 118 | const rendering = performance.now(); |
| 119 | 119 | const now = Date.now(); |
| 120 | 120 | const chosen = windowOf(new URL(ctx.request.url).searchParams.get("window")); |
| 121 | const t = trafficHistory(ctx.app.db, perfRecord.ledger, now, chosen); | |
| 121 | const t = trafficHistory(ctx.app.db, perfOf(ctx.app).perfRecord.ledger, now, chosen); | |
| 122 | 122 | const tops = t.tops; |
| 123 | 123 | const wrong = trafficAttention(t); |
| 124 | 124 | const span = TRAFFIC_WINDOWS[chosen].words; |
src/web/studio/kpi_probe.ts +2 −2
| @@ -39,7 +39,7 @@ import { | ||
| 39 | 39 | visitorsValue, |
| 40 | 40 | } from "../../flow/kpi_words.ts"; |
| 41 | 41 | import { HOURS, median } from "../../flow/throughput.ts"; |
| 42 | import { perfRecord } from "../../perf/live.ts"; | |
| 42 | import { perfOf } from "../../perf/live.ts"; | |
| 43 | 43 | import { scanTraffic, type PerfRecord } from "../../perf/record.ts"; |
| 44 | 44 | import { TRAFFIC_CLASSES } from "../../perf/traffic.ts"; |
| 45 | 45 | import { HOME_TTL_MS, cachedKpis } from "./home_cache.ts"; |
| @@ -315,6 +315,6 @@ export function kpiProbe(app: App, now: Date = new Date()): KpiProbe { | ||
| 315 | 315 | now, |
| 316 | 316 | { kpis: cachedKpis(app.db, now, forge), live: liveNow() }, |
| 317 | 317 | forge, |
| 318 | perfRecord, | |
| 318 | perfOf(app).perfRecord, | |
| 319 | 319 | ); |
| 320 | 320 | } |
test/helpers/stall-state.ts +7 −10
| @@ -1,20 +1,17 @@ | ||
| 1 | 1 | /** |
| 2 | * The process's stall watch, starting clean and left clean (#11069). The | |
| 3 | * suite runs every file in one process, and `loopWatch`, `stallBlame` and the | |
| 4 | * telemetry reader counts (`perf/live.ts`, `system/telemetry_reads.ts`) are | |
| 5 | * that process's; a stall one file noted would otherwise be a row in the next | |
| 6 | * file's /system. Call at a file's top level, as `quietLogs` is | |
| 7 | * (`globals.ts`): it clears before the file's first test and after its last. | |
| 2 | * The process's telemetry reader counts, starting clean and left clean | |
| 3 | * (#11069). The suite runs every file in one process, and the counts | |
| 4 | * (`system/telemetry_reads.ts`) are that process's. A forge's stalls are its | |
| 5 | * own (`perfOf`, #12615) and need no clearing. Call at a file's top level, as | |
| 6 | * `quietLogs` is (`globals.ts`): it clears before the file's first test and | |
| 7 | * after its last. | |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | import { afterAll, beforeAll } from "bun:test"; |
| 11 | import { loopWatch, stallBlame } from "../../src/perf/live.ts"; | |
| 12 | 11 | import { resetTelemetryReads } from "../../src/system/telemetry_reads.ts"; |
| 13 | 12 | |
| 14 | /** Forget every stall and telemetry read the process has noted. */ | |
| 13 | /** Forget every telemetry read the process has noted. */ | |
| 15 | 14 | export function clearStallState(): void { |
| 16 | loopWatch.clear(); | |
| 17 | stallBlame.clear(); | |
| 18 | 15 | resetTelemetryReads(); |
| 19 | 16 | } |
| 20 | 17 |
test/loop-stall-by-route.test.ts +10 −10
| @@ -11,7 +11,7 @@ import { serve, type Servers } from "../src/http/server.ts"; | ||
| 11 | 11 | import { watchStalls } from "../src/kernel/loop_lag.ts"; |
| 12 | 12 | import { openMetrics } from "../src/metrics/openmetrics.ts"; |
| 13 | 13 | import { stallFamilies, TOP_ROUTES } from "../src/metrics/stalls.ts"; |
| 14 | import { lagMinutes, loopWatch, stallBlame } from "../src/perf/live.ts"; | |
| 14 | import { perfOf } from "../src/perf/live.ts"; | |
| 15 | 15 | import { WORST_STALLS } from "../src/perf/stall_blame.ts"; |
| 16 | 16 | import { createLoopWatch, HOUR_MS, NO_REQUEST } from "../src/perf/loop.ts"; |
| 17 | 17 | import { createAgent } from "../src/actions/crew.ts"; |
| @@ -144,8 +144,8 @@ test("GET /metrics names the route a stall ran across", async () => { | ||
| 144 | 144 | // millisecond was only across a 400 ms stall, not its cause (`WITNESS_SHARE`), so the one |
| 145 | 145 | // that held the loop is noted as the server would have noted it: 300 ms of the window. |
| 146 | 146 | const at = performance.now(); |
| 147 | loopWatch.finished("/api/v1/health", at - 300, at); | |
| 148 | expect(loopWatch.stalled(400, at)).not.toBeNull(); | |
| 147 | perfOf(app).loopWatch.finished("/api/v1/health", at - 300, at); | |
| 148 | expect(perfOf(app).loopWatch.stalled(400, at)).not.toBeNull(); | |
| 149 | 149 | const text = await (await fetch(`${servers.url}/metrics`, { headers: { authorization: `Bearer ${ro}` } })).text(); |
| 150 | 150 | expect(text).toMatch(/^forge_loop_stalls_total\{route="\/api\/v1\/health"\} [1-9]/m); |
| 151 | 151 | expect(text).toMatch(/^forge_loop_stall_hour_milliseconds\{route="\/api\/v1\/health"\} [1-9]/m); |
| @@ -229,12 +229,12 @@ test("the Stalls screen lists the hour's worst single stalls as /system does: wh | ||
| 229 | 229 | test("the Stalls screen reads the process's stall blame by default, the five /system reads", () => { |
| 230 | 230 | const now = performance.now(); |
| 231 | 231 | for (const [ms, r] of [[300, "/a"], [800, "/b"], [250, "/c"], [500, "/d"], [700, "/e"], [210, "/f"]] as const) |
| 232 | stallBlame.note({ at: now - 1_000, lateMs: ms, routes: [r], jobs: [] }); | |
| 232 | perfOf(app).stallBlame.note({ at: now - 1_000, lateMs: ms, routes: [r], jobs: [] }); | |
| 233 | 233 | const lines = stallLines(new Session(app, identity(alice)), []); |
| 234 | 234 | const named = lines.slice(lines.indexOf("") + 2).map((l) => l.split(/\s+/)[0] ?? ""); |
| 235 | 235 | expect(named).toEqual(["/b", "/e", "/d", "/a", "/c"]); |
| 236 | expect(stallBlame.worst(performance.now(), HOUR_MS, WORST_STALLS).map((s) => s.what)).toEqual(named); | |
| 237 | stallBlame.clear(); | |
| 236 | expect(perfOf(app).stallBlame.worst(performance.now(), HOUR_MS, WORST_STALLS).map((s) => s.what)).toEqual(named); | |
| 237 | perfOf(app).stallBlame.clear(); | |
| 238 | 238 | }); |
| 239 | 239 | |
| 240 | 240 | test("the Stalls screen shows the last 20 minutes' loop lag as /system does: over count, each minute, what ran (#11220)", async () => { |
| @@ -258,9 +258,9 @@ test("the Stalls screen shows the last 20 minutes' loop lag as /system does: ove | ||
| 258 | 258 | for (const who of [bob, bot, undefined]) expect(lagLines(new Session(app, identity(who)), rows)).toEqual([]); |
| 259 | 259 | |
| 260 | 260 | // The screen itself draws the process's readings above the stalls. |
| 261 | lagMinutes.clear(); | |
| 262 | lagMinutes.note(250, 0); | |
| 263 | lagMinutes.note(0, 60_000); // closes the first minute | |
| 261 | perfOf(app).lagMinutes.clear(); | |
| 262 | perfOf(app).lagMinutes.note(250, 0); | |
| 263 | perfOf(app).lagMinutes.note(0, 60_000); // closes the first minute | |
| 264 | 264 | const { app: tui, session } = createForgeApp(app, identity(alice), { terminal: headlessTerminal(120, 40), exitHooks: false }); |
| 265 | 265 | tui.start(); |
| 266 | 266 | await settleApp(tui); |
| @@ -268,7 +268,7 @@ test("the Stalls screen shows the last 20 minutes' loop lag as /system does: ove | ||
| 268 | 268 | await settleApp(tui); |
| 269 | 269 | const shown = tui.screen.toText(); |
| 270 | 270 | tui.stop(); |
| 271 | lagMinutes.clear(); | |
| 271 | perfOf(app).lagMinutes.clear(); | |
| 272 | 272 | expect(shown).toContain("Loop lag, last 20 minutes: 1 of 1 over 100 ms"); |
| 273 | 273 | expect(shown).toContain("Route (last hour)"); |
| 274 | 274 | expect(shown.indexOf("Loop lag")).toBeLessThan(shown.indexOf("Route (last hour)")); |
test/mcp-system-routes.test.ts +3 −3
| @@ -9,7 +9,7 @@ import { setTelemetryGrant } from "../src/actions/telemetry.ts"; | ||
| 9 | 9 | import { createToken } from "../src/core/tokens.ts"; |
| 10 | 10 | import { createUser, getUserByName } from "../src/core/users.ts"; |
| 11 | 11 | import { serve, type Servers } from "../src/http/server.ts"; |
| 12 | import { perfRecord } from "../src/perf/live.ts"; | |
| 12 | import { perfOf } from "../src/perf/live.ts"; | |
| 13 | 13 | import { quietLogs } from "./helpers/globals.ts"; |
| 14 | 14 | |
| 15 | 15 | quietLogs(); |
| @@ -47,8 +47,8 @@ const call = async (token: string) => { | ||
| 47 | 47 | }; |
| 48 | 48 | |
| 49 | 49 | test("routes lists the over-budget route first, with its budget and slowest phase; a non-admin is refused", async () => { |
| 50 | // perfRecord is the process's own, and a shard-mate's requests leave samples (and patterns, up to the cap) in it. | |
| 51 | perfRecord.clear(); | |
| 50 | // The forge's own record (#12615): a shard-mate's requests leave nothing in it. | |
| 51 | const perfRecord = perfOf(app).perfRecord; | |
| 52 | 52 | const at = Date.now(); |
| 53 | 53 | for (let i = 0; i < 25; i++) { |
| 54 | 54 | perfRecord.add("/boards", { at, ms: 100, signedIn: true, phases: [{ name: "app", ms: 100, hit: false }] }); |
test/mcp-system-stalls.test.ts +3 −4
| @@ -8,7 +8,7 @@ import { createToken } from "../src/core/tokens.ts"; | ||
| 8 | 8 | import { createUser } from "../src/core/users.ts"; |
| 9 | 9 | import { serve, type Servers } from "../src/http/server.ts"; |
| 10 | 10 | import { during } from "../src/perf/jobs.ts"; |
| 11 | import { loopWatch } from "../src/perf/live.ts"; | |
| 11 | import { perfOf } from "../src/perf/live.ts"; | |
| 12 | 12 | import { LIMIT_MS } from "../src/perf/loop.ts"; |
| 13 | 13 | import { quietLogs } from "./helpers/globals.ts"; |
| 14 | 14 | |
| @@ -35,9 +35,8 @@ afterAll(async () => { | ||
| 35 | 35 | }); |
| 36 | 36 | |
| 37 | 37 | test("a stall under a named job is read through system, at most 10, most ms first", async () => { |
| 38 | // The watch is the process's one: stalls and requests an earlier file left | |
| 39 | // would crowd the top 10 or be blamed ahead of the job. Start from nothing. | |
| 40 | loopWatch.clear(); | |
| 38 | // The forge's own watch (#12615): what an earlier file noted is not in it. | |
| 39 | const { loopWatch } = perfOf(app); | |
| 41 | 40 | // The job holds the loop for a real share of the stall: one that only brushed it is not |
| 42 | 41 | // named (`perf/loop.ts`). |
| 43 | 42 | during("test-job-10907", () => { |
test/metrics-forge.test.ts +7
| @@ -64,3 +64,10 @@ test("totals are kept per database and stop growing new patterns at the cap", () | ||
| 64 | 64 | ta.record("/p0", 1); |
| 65 | 65 | expect(readTotals(a).get("/p0")!.counts[0]).toBe(2); |
| 66 | 66 | }); |
| 67 | ||
| 68 | test("a scrape with no forge reports no stall families; with one it reports that forge's", () => { | |
| 69 | const db = new Database(":memory:"); | |
| 70 | migrate(db, migrationFiles); | |
| 71 | expect(forgeMetrics(db)).not.toContain("forge_loop_stall_hour_count"); | |
| 72 | expect(forgeMetrics(db, undefined, Date.now(), {})).toContain("forge_loop_stall_hour_count"); | |
| 73 | }); |
test/pages-budget.test.ts −3
| @@ -36,7 +36,6 @@ import { keepTick } from "../src/flow/keep.ts"; | ||
| 36 | 36 | import { readLanesInline, useLanesReader } from "../src/flow/lanes.ts"; |
| 37 | 37 | import { usePagesReader } from "../src/flow/pages.ts"; |
| 38 | 38 | import { pagesConfig, readPagePicture, type Picture } from "../src/flow/pages_read.ts"; |
| 39 | import { perfRecord } from "../src/perf/live.ts"; | |
| 40 | 39 | import { buildApiRouter, buildRouter, serve, type Servers } from "../src/http/server.ts"; |
| 41 | 40 | import { quietLogs } from "./helpers/globals.ts"; |
| 42 | 41 | import { promote } from "./helpers/members.ts"; |
| @@ -171,8 +170,6 @@ beforeAll(async () => { | ||
| 171 | 170 | cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; |
| 172 | 171 | }, 60_000); |
| 173 | 172 | afterAll(() => { |
| 174 | // The walk fills the shared recorder to its pattern cap; leave it empty for the next file in this process. | |
| 175 | perfRecord.clear(); | |
| 176 | 173 | servers.stop(); |
| 177 | 174 | app.close(); |
| 178 | 175 | rmSync(root, { recursive: true, force: true }); |
test/perf-lag-minutes.test.ts +6 −6
| @@ -13,7 +13,7 @@ import { createToken } from "../src/core/tokens.ts"; | ||
| 13 | 13 | import { createUser } from "../src/core/users.ts"; |
| 14 | 14 | import { serve, type Servers } from "../src/http/server.ts"; |
| 15 | 15 | import { createLagMinutes, LAG_BAR_MS, LAG_MINUTE_MS, LAG_READINGS, overBar } from "../src/perf/lag_minutes.ts"; |
| 16 | import { lagMinutes } from "../src/perf/live.ts"; | |
| 16 | import { perfOf } from "../src/perf/live.ts"; | |
| 17 | 17 | import { lagSection } from "../src/web/pages/system.ts"; |
| 18 | 18 | import { quietLogs } from "./helpers/globals.ts"; |
| 19 | 19 | |
| @@ -122,18 +122,18 @@ describe("the MCP system tool", () => { | ||
| 122 | 122 | }, 30_000); |
| 123 | 123 | |
| 124 | 124 | afterAll(async () => { |
| 125 | lagMinutes.clear(); | |
| 125 | perfOf(app).lagMinutes.clear(); | |
| 126 | 126 | servers.stop(); |
| 127 | 127 | app.close(); |
| 128 | 128 | await rm(dataDir, { recursive: true, force: true }); |
| 129 | 129 | }); |
| 130 | 130 | |
| 131 | 131 | test("answers the same readings as {at, ms, what}, with the bar and the count over it", async () => { |
| 132 | lagMinutes.clear(); | |
| 132 | perfOf(app).lagMinutes.clear(); | |
| 133 | 133 | const base = 1_000 * MIN; |
| 134 | lagMinutes.note(20, base); | |
| 135 | lagMinutes.note(180.04, base + MIN); | |
| 136 | lagMinutes.note(1, base + 2 * MIN); | |
| 134 | perfOf(app).lagMinutes.note(20, base); | |
| 135 | perfOf(app).lagMinutes.note(180.04, base + MIN); | |
| 136 | perfOf(app).lagMinutes.note(1, base + 2 * MIN); | |
| 137 | 137 | const res = await fetch(`${servers.url}/mcp`, { |
| 138 | 138 | method: "POST", |
| 139 | 139 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, |
test/perf-loop-jobs.test.ts +3 −2
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | import { expect, test } from "bun:test"; |
| 4 | 4 | import { createJobWatch, during } from "../src/perf/jobs.ts"; |
| 5 | import { loopWatch } from "../src/perf/live.ts"; | |
| 5 | import { createPerf } from "../src/perf/live.ts"; | |
| 6 | 6 | import { createLoopWatch, NO_REQUEST, stallLine } from "../src/perf/loop.ts"; |
| 7 | 7 | import { freshStallState } from "./helpers/stall-state.ts"; |
| 8 | 8 | |
| @@ -66,7 +66,8 @@ test("a loop stall while a job awaits and another blocks is tallied to the block | ||
| 66 | 66 | await lane; |
| 67 | 67 | }); |
| 68 | 68 | |
| 69 | test("the process's loop watch reads the process's job watch", () => { | |
| 69 | test("a forge's loop watch reads the process's job watch", () => { | |
| 70 | const { loopWatch } = createPerf(); | |
| 70 | 71 | const before = loopWatch.tally(performance.now()).total.find((r) => r.route === "job:wired-test")?.count ?? 0; |
| 71 | 72 | during("wired-test", () => block(300)); |
| 72 | 73 | const at = performance.now(); |
test/perf-per-forge.test.ts added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | /** | |
| 2 | * Each forge in a process keeps its own readings (#12615): a slow request on | |
| 3 | * one is in its record, loop watch, stall blame and lag minutes, and none of | |
| 4 | * the other's. One set for the process made page-budget and perf-server-hook | |
| 5 | * read a shard-mate's requests in a whole gate. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 13 | import { noteWake, perfOf } from "../src/perf/live.ts"; | |
| 14 | import { quietLogs } from "./helpers/globals.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | ||
| 18 | const HOUR_MS = 60 * 60_000; | |
| 19 | let root = ""; | |
| 20 | let slow: App; | |
| 21 | let quiet: App; | |
| 22 | let slowServers: Servers; | |
| 23 | let quietServers: Servers; | |
| 24 | beforeAll(async () => { | |
| 25 | root = mkdtempSync(join(tmpdir(), "forge-perf-per-forge-")); | |
| 26 | slow = createApp({ dataDir: join(root, "slow"), port: 0 }); | |
| 27 | quiet = createApp({ dataDir: join(root, "quiet"), port: 0 }); | |
| 28 | slowServers = await serve(slow); | |
| 29 | quietServers = await serve(quiet); | |
| 30 | }); | |
| 31 | afterAll(() => { | |
| 32 | slowServers.stop(); | |
| 33 | quietServers.stop(); | |
| 34 | slow.close(); | |
| 35 | quiet.close(); | |
| 36 | rmSync(root, { recursive: true, force: true }); | |
| 37 | }); | |
| 38 | ||
| 39 | test("a slow request on one forge leaves the other's record, loop watch, stall blame and lag minutes empty", async () => { | |
| 40 | expect((await fetch(`${slowServers.url}/api/v1/health`)).ok).toBe(true); | |
| 41 | // The loop watcher wakes late across that request, as `main.ts` wires it; | |
| 42 | // a wake a minute on closes the lag reading. | |
| 43 | const at = performance.now(); | |
| 44 | noteWake(perfOf(slow), 5_000, at); | |
| 45 | noteWake(perfOf(slow), 0, at + 60_000); | |
| 46 | ||
| 47 | const mine = perfOf(slow); | |
| 48 | expect(mine.perfRecord.traffic("/api/v1/health", 0).length).toBe(1); | |
| 49 | expect(mine.loopWatch.tally(at).total.length).toBe(1); | |
| 50 | expect(mine.stallBlame.worst(at, HOUR_MS, 10).length).toBe(1); | |
| 51 | expect(mine.lagMinutes.readings().length).toBeGreaterThan(0); | |
| 52 | ||
| 53 | const theirs = perfOf(quiet); | |
| 54 | expect(theirs.perfRecord.traffic("/api/v1/health", 0)).toEqual([]); | |
| 55 | expect(theirs.loopWatch.tally(at).total).toEqual([]); | |
| 56 | expect(theirs.stallBlame.worst(at, HOUR_MS, 10)).toEqual([]); | |
| 57 | expect(theirs.lagMinutes.readings()).toEqual([]); | |
| 58 | }); |
test/perf-server-hook.test.ts +3 −3
| @@ -5,7 +5,7 @@ import { join } from "node:path"; | ||
| 5 | 5 | import { createApp, type App } from "../src/app.ts"; |
| 6 | 6 | import { serve, type Servers } from "../src/http/server.ts"; |
| 7 | 7 | import { watchStalls } from "../src/kernel/loop_lag.ts"; |
| 8 | import { perfRecord } from "../src/perf/live.ts"; | |
| 8 | import { perfOf } from "../src/perf/live.ts"; | |
| 9 | 9 | import { quietLogs } from "./helpers/globals.ts"; |
| 10 | 10 | |
| 11 | 11 | quietLogs(); |
| @@ -25,10 +25,10 @@ afterAll(() => { | ||
| 25 | 25 | }); |
| 26 | 26 | |
| 27 | 27 | test("a served request lands in the record under its route pattern; a client label hides nothing", async () => { |
| 28 | const before = perfRecord.traffic("/api/v1/health", 0).length; | |
| 28 | const before = perfOf(app).perfRecord.traffic("/api/v1/health", 0).length; | |
| 29 | 29 | await fetch(`${servers.url}/api/v1/health`); |
| 30 | 30 | await fetch(`${servers.url}/api/v1/health`, { headers: { "x-forge-synthetic": "1" } }); |
| 31 | const after = perfRecord.traffic("/api/v1/health", 0); | |
| 31 | const after = perfOf(app).perfRecord.traffic("/api/v1/health", 0); | |
| 32 | 32 | expect(after.length).toBe(before + 2); |
| 33 | 33 | expect(after.at(-1)!.signedIn).toBe(false); |
| 34 | 34 | expect(after.at(-1)!.phases.some((p) => p.name === "app")).toBe(true); |
test/perf-stall-blame.test.ts +6 −6
| @@ -13,7 +13,7 @@ import { createApp, type App } from "../src/app.ts"; | ||
| 13 | 13 | import { serve, type Servers } from "../src/http/server.ts"; |
| 14 | 14 | import { startLoopLag } from "../src/kernel/loop_lag.ts"; |
| 15 | 15 | import { createJobWatch } from "../src/perf/jobs.ts"; |
| 16 | import { stallBlame } from "../src/perf/live.ts"; | |
| 16 | import { perfOf } from "../src/perf/live.ts"; | |
| 17 | 17 | import { createLoopWatch, HOUR_MS, LIMIT_MS, type Stall } from "../src/perf/loop.ts"; |
| 18 | 18 | import { createStallBlame, STALL_KEPT, UNKNOWN, whatOf } from "../src/perf/stall_blame.ts"; |
| 19 | 19 | import { stallSection, worstStallsTable } from "../src/web/pages/system.ts"; |
| @@ -139,14 +139,14 @@ describe("on /healthz", () => { | ||
| 139 | 139 | }); |
| 140 | 140 | |
| 141 | 141 | afterAll(async () => { |
| 142 | stallBlame.clear(); | |
| 142 | perfOf(app).stallBlame.clear(); | |
| 143 | 143 | servers.stop(); |
| 144 | 144 | app.close(); |
| 145 | 145 | await rm(dataDir, { recursive: true, force: true }); |
| 146 | 146 | }); |
| 147 | 147 | |
| 148 | 148 | test("loopWorstWhat is null with no stall, then names the last 10 s's worst", async () => { |
| 149 | stallBlame.clear(); | |
| 149 | perfOf(app).stallBlame.clear(); | |
| 150 | 150 | const ask = async () => |
| 151 | 151 | (await (await fetch(`${servers.url}/healthz`, { headers: { accept: "application/json" } })).json()) as { |
| 152 | 152 | ok: boolean; |
| @@ -159,9 +159,9 @@ describe("on /healthz", () => { | ||
| 159 | 159 | expect(before.loopWorstWhat).toBeNull(); |
| 160 | 160 | |
| 161 | 161 | const now = performance.now(); |
| 162 | stallBlame.note({ at: now - 60_000, lateMs: 900, routes: ["/too-old"], jobs: [] }); | |
| 163 | stallBlame.note({ at: now, lateMs: 300, routes: [], jobs: ["crew-tick"] }); | |
| 164 | stallBlame.note({ at: now, lateMs: 220, routes: ["/explore"], jobs: [] }); | |
| 162 | perfOf(app).stallBlame.note({ at: now - 60_000, lateMs: 900, routes: ["/too-old"], jobs: [] }); | |
| 163 | perfOf(app).stallBlame.note({ at: now, lateMs: 300, routes: [], jobs: ["crew-tick"] }); | |
| 164 | perfOf(app).stallBlame.note({ at: now, lateMs: 220, routes: ["/explore"], jobs: [] }); | |
| 165 | 165 | expect((await ask()).loopWorstWhat).toBe("job:crew-tick"); |
| 166 | 166 | // A probe asking for nothing still hears the plain `ok` (#2917). |
| 167 | 167 | expect(await (await fetch(`${servers.url}/healthz`)).text()).toBe("ok"); |
test/relay-stall-name.test.ts +3 −3
| @@ -30,7 +30,7 @@ import type { Repo } from "../src/core/repos.ts"; | ||
| 30 | 30 | import { createUser, type User } from "../src/core/users.ts"; |
| 31 | 31 | import { serve, type Servers } from "../src/http/server.ts"; |
| 32 | 32 | import { createJobWatch, jobWatch } from "../src/perf/jobs.ts"; |
| 33 | import { loopWatch } from "../src/perf/live.ts"; | |
| 33 | import { perfOf } from "../src/perf/live.ts"; | |
| 34 | 34 | import { createLoopWatch, type Stall } from "../src/perf/loop.ts"; |
| 35 | 35 | import { loopWorst } from "../src/perf/loop_worst.ts"; |
| 36 | 36 | import { deploySettled } from "./helpers/apps-forge.ts"; |
| @@ -176,8 +176,8 @@ describe("forge-stall on the relayed answer", () => { | ||
| 176 | 176 | loopWorst.note(ms, now); |
| 177 | 177 | // A job that ran for a moment was only across the stall (`WITNESS_SHARE`, `perf/loop.ts`): |
| 178 | 178 | // what is named is what covered it, so a request that did is noted as the server notes one. |
| 179 | loopWatch.finished("/api/v1/health", now - ms, now); | |
| 180 | const s = loopWatch.stalled(ms, now)!; | |
| 179 | perfOf(app).loopWatch.finished("/api/v1/health", now - ms, now); | |
| 180 | const s = perfOf(app).loopWatch.stalled(ms, now)!; | |
| 181 | 181 | // A route that ran across it outranks the job (setup's requests often do); |
| 182 | 182 | // the job's own naming is pinned above, with a stepped clock. |
| 183 | 183 | return heldBy([s])[0]!.name; |
test/spawn-in-process-visible.test.ts +7 −7
| @@ -20,7 +20,7 @@ import { | ||
| 20 | 20 | noteChildRequest, |
| 21 | 21 | } from "../src/perf/child_time.ts"; |
| 22 | 22 | import { during, jobWatch } from "../src/perf/jobs.ts"; |
| 23 | import { loopWatch } from "../src/perf/live.ts"; | |
| 23 | import { perfOf } from "../src/perf/live.ts"; | |
| 24 | 24 | import { stallLine } from "../src/perf/loop.ts"; |
| 25 | 25 | import { clientRows, servedWindow } from "../src/spawn/clients.ts"; |
| 26 | 26 | import { closeHostedChildren, openChildren, provideCellsForTest } from "../src/spawn/host.ts"; |
| @@ -226,12 +226,12 @@ describe("a parent holding a forge in its own process", () => { | ||
| 226 | 226 | }); |
| 227 | 227 | |
| 228 | 228 | test("a request it answers is the stall watcher's child:<name>, and is counted against it", async () => { |
| 229 | loopWatch.clear(); | |
| 229 | perfOf(parent).loopWatch.clear(); | |
| 230 | 230 | // Open it, so the next request is answered by a forge already in this process. |
| 231 | 231 | await (await fetch(`${servers.url}/healthz`, { headers: { host: "homie--forge.test" } })).text(); |
| 232 | 232 | const child = openChildren(parent).find((c) => c.name === "homie")!.app; |
| 233 | 233 | const had = childHours().find((r) => r.name === "homie")?.requests ?? 0; |
| 234 | loopWatch.clear(); | |
| 234 | perfOf(parent).loopWatch.clear(); | |
| 235 | 235 | // The child's own request holds the parent's thread: its first statement takes 80 ms of |
| 236 | 236 | // synchronous work, as a slow query or a git call of the child's would. A stall is named |
| 237 | 237 | // for what held the loop, not for what was only across it (`WITNESS_SHARE`, `perf/loop.ts`). |
| @@ -244,22 +244,22 @@ describe("a parent holding a forge in its own process", () => { | ||
| 244 | 244 | expect(release()).toBe(true); |
| 245 | 245 | // The stall that request made names the child: before, it named the parent's route of the |
| 246 | 246 | // same shape. |
| 247 | const stall = loopWatch.stalled(250, performance.now()); | |
| 247 | const stall = perfOf(parent).loopWatch.stalled(250, performance.now()); | |
| 248 | 248 | expect(stall?.routes).toContain("child:homie"); |
| 249 | 249 | expect(stallLine(stall!)).toContain("running then: child:homie"); |
| 250 | 250 | expect(childHours().find((r) => r.name === "homie")?.requests).toBe(had + 1); |
| 251 | 251 | // The parent's own request is still its route's. |
| 252 | loopWatch.clear(); | |
| 252 | perfOf(parent).loopWatch.clear(); | |
| 253 | 253 | const mine = holdOnce(parent, 80); |
| 254 | 254 | await ( |
| 255 | 255 | await fetch(`${servers.url}/healthz`, { headers: { host: "forge.test" } }) |
| 256 | 256 | ).text(); |
| 257 | 257 | expect(mine()).toBe(true); |
| 258 | const own = loopWatch.stalled(250, performance.now()); | |
| 258 | const own = perfOf(parent).loopWatch.stalled(250, performance.now()); | |
| 259 | 259 | expect(own?.routes).toEqual(["/healthz"]); |
| 260 | 260 | expect(own?.routes.some((r) => r.startsWith("child:"))).toBe(false); |
| 261 | 261 | // The watch is the process's: the two stalls this test made up are not left in its hour. |
| 262 | loopWatch.clear(); | |
| 262 | perfOf(parent).loopWatch.clear(); | |
| 263 | 263 | }); |
| 264 | 264 | |
| 265 | 265 | test("/forges says so on its row, in plain words", async () => { |
test/stall-in-flight.test.ts +4 −4
| @@ -10,7 +10,7 @@ import { createUser } from "../src/core/users.ts"; | ||
| 10 | 10 | import { serve, type Servers } from "../src/http/server.ts"; |
| 11 | 11 | import { stallFamilies } from "../src/metrics/stalls.ts"; |
| 12 | 12 | import { createInFlight } from "../src/perf/in_flight.ts"; |
| 13 | import { inFlight, loopWatch } from "../src/perf/live.ts"; | |
| 13 | import { inFlight, perfOf } from "../src/perf/live.ts"; | |
| 14 | 14 | import { blamed, createLoopWatch, LIMIT_MS, NO_REQUEST, stallLine } from "../src/perf/loop.ts"; |
| 15 | 15 | import { whatOf } from "../src/perf/stall_blame.ts"; |
| 16 | 16 | import { quietLogs } from "./helpers/globals.ts"; |
| @@ -142,7 +142,7 @@ function describe_server() { | ||
| 142 | 142 | }; |
| 143 | 143 | |
| 144 | 144 | test("a real request held open across a stall is in MCP `system` `stalls`; one begun after is not", async () => { |
| 145 | loopWatch.clear(); | |
| 145 | perfOf(app).loopWatch.clear(); | |
| 146 | 146 | // The suite shares one process: a request another file left open (or its own server's |
| 147 | 147 | // background traffic) is in the open set too, and comes and goes on its own. Watch only |
| 148 | 148 | // this test's `/mcp`, never the size of the whole set. |
| @@ -171,11 +171,11 @@ function describe_server() { | ||
| 171 | 171 | await Bun.sleep(LIMIT_MS + 60); // real-timer: the request must be older than the stall limit on the real clock |
| 172 | 172 | // The wake: the loop held for longer than the stall limit, beginning after the request did. |
| 173 | 173 | const now = performance.now(); |
| 174 | const stall = loopWatch.stalled(LIMIT_MS + 10, now)!; | |
| 174 | const stall = perfOf(app).loopWatch.stalled(LIMIT_MS + 10, now)!; | |
| 175 | 175 | const named = stall.inFlight ?? []; |
| 176 | 176 | expect(named).toContain("/mcp"); |
| 177 | 177 | // A stall shorter than the request's age is charged; one reaching back before it began is not. |
| 178 | expect(loopWatch.stalled(60_000, now)!.inFlight ?? []).toEqual(inFlight.since(now - 60_000)); | |
| 178 | expect(perfOf(app).loopWatch.stalled(60_000, now)!.inFlight ?? []).toEqual(inFlight.since(now - 60_000)); | |
| 179 | 179 | push!(new TextEncoder().encode('"id":9,"method":"tools/list"}')); |
| 180 | 180 | close!(); |
| 181 | 181 | await (await held).text(); |
test/traffic-history.test.ts +2 −2
| @@ -25,7 +25,7 @@ import { | ||
| 25 | 25 | signatureOf, |
| 26 | 26 | type TrafficClass, |
| 27 | 27 | } from "../src/perf/traffic.ts"; |
| 28 | import { perfRecord } from "../src/perf/live.ts"; | |
| 28 | import { perfOf } from "../src/perf/live.ts"; | |
| 29 | 29 | import { createTrafficWriter, HOUR_TOPS, trafficHistory, windowOf } from "../src/perf/traffic_store.ts"; |
| 30 | 30 | |
| 31 | 31 | const quiet = { warn: () => {} }; |
| @@ -171,7 +171,7 @@ test("what a request is sorted by: internal, country and signature", () => { | ||
| 171 | 171 | test("the page offers the three windows, for an admin alone, and a loopback request is the forge's own", async () => { |
| 172 | 172 | const cookie = `forge_session=${createSession(app.db, admin.id).token}`; |
| 173 | 173 | await fetch(`${servers.url}/`, { redirect: "manual" }); |
| 174 | const own = perfRecord.ledger.totals(Date.now()).classes.internal.requests; | |
| 174 | const own = perfOf(app).perfRecord.ledger.totals(Date.now()).classes.internal.requests; | |
| 175 | 175 | expect(own).toBeGreaterThan(0); |
| 176 | 176 | const page = await fetch(`${servers.url}/settings/traffic?window=24h`, { headers: { cookie } }); |
| 177 | 177 | expect(page.status).toBe(200); |
new above
offline