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

Main's full check prints each step's seconds and share (#16671) #16720

Merged opened this

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

Merged

fullcheck-timing → main 0 commits

  • Merged. fullcheck-timing was merged into main as 0bb2460.

Review

Sign in to review this change.
5 files changed +230 −5
scripts/gate-summary.ts +7
@@ -12,6 +12,7 @@
1212
1313 import { GATE_SUMMARY_HEADING } from "../src/checks/summary.ts";
1414 import { FILE_BUDGET_SECONDS } from "./test-budget.ts";
15 import { stepLines, type Step } from "./step-times.ts";
1516
1617 /** A file over this many seconds is named on its own warning line. */
1718 export const SLOW_SECONDS = 45;
@@ -88,6 +89,11 @@ export type GateSummary = {
8889 * Files `SLOW` in scripts/gate.ts lets run over the budget: still marked, and said to be in it.
8990 */
9091 slow?: ReadonlySet<string>;
92 /**
93 * Each step of the check in the order it ran, the whole check's seconds and where they were
94 * counted from (`scripts/step-times.ts`, #16671); no step lines when absent.
95 */
96 steps?: { list: readonly Step[]; seconds: number; from: string };
9197 };
9298
9399 const gib = (bytes: number): string => `${(bytes / GIB).toFixed(2)} GiB`;
@@ -124,6 +130,7 @@ export function summaryLines(s: GateSummary): string[] {
124130 GATE_SUMMARY_HEADING,
125131 `test-shards: shards: ${why(s)}`,
126132 `test-shards: wall: ${s.wallSeconds.toFixed(1)}s`,
133 ...(s.steps ? stepLines(s.steps.list, s.steps.seconds, s.steps.from) : []),
127134 `test-shards: peak memory: ${peak}`,
128135 `test-shards: slowest ${top.length} of ${s.times.length} files:`,
129136 ...top.map((t) => ` ${t.seconds.toFixed(1).padStart(6)}s ${t.file}${mark(t)}`),
scripts/gate.ts +17
@@ -1046,5 +1046,22 @@ if (import.meta.main) {
10461046 for (const line of notes) console.error(`gate: ${line}`);
10471047 for (const line of refusal) console.error(`gate: ${line}`);
10481048 if (refused.length > 1) console.error(`gate: ${refusedSummary(refused)}`);
1049 await leaveGateStep();
10491050 process.exit(refusal.length === 0 ? 0 : 2);
10501051 }
1052
1053 /**
1054 * The gate's own times, left for the timed gate's summary to count as its first step (#16671):
1055 * this process, from its start to now. A note, so a disk that refuses it never fails the gate.
1056 * Imported here, not at the top: an older tree's copy of this file is imported alone (`treeGate`).
1057 */
1058 async function leaveGateStep(): Promise<void> {
1059 try {
1060 const { GATE_STEP_FILE, gateStepDir, gateStepText } = await import("./step-times.ts");
1061 mkdirSync(gateStepDir, { recursive: true });
1062 const endedAt = Date.now();
1063 writeFileSync(GATE_STEP_FILE, gateStepText({ startedAt: endedAt - performance.now(), endedAt }));
1064 } catch {
1065 // no times this run: the summary counts from test-shards' start and says so
1066 }
1067 }
scripts/step-times.ts added +65
@@ -0,0 +1,65 @@
1 /**
2 * Where a timed full gate's wall clock went, step by step (#16671): the
3 * gate, test-shards' own setup, covering the suite's loading, each shard,
4 * the re-timing alone, and the records written after. One line a step, in
5 * the order they ran, with its seconds and its share of the whole check.
6 *
7 * The gate is its own process, ahead of `scripts/test-shards.ts` in
8 * `forge.json`'s command, so it leaves its times in {@link GATE_STEP_FILE};
9 * test-shards counts them only when the gate ended just before it began, so
10 * a file a cached `.forge-cache` carried from an earlier check never is.
11 * Plain data in, lines out; nothing here changes an exit code.
12 */
13
14 import { CHECK_CACHE } from "../src/checks/cache.ts";
15
16 /** One step and how long it took. */
17 export type Step = { name: string; seconds: number };
18
19 /** Where the gate's times are left: the check's cache directory. */
20 export const gateStepDir = CHECK_CACHE;
21
22 /** The gate's times, for the summary (#16671). */
23 export const GATE_STEP_FILE = `${CHECK_CACHE}/gate-step.json`;
24
25 /** How long, in ms, after the gate ended test-shards may begin and still count it. */
26 export const GATE_STEP_FRESH_MS = 60_000;
27
28 /** When the gate's process began and ended, in ms since the epoch. */
29 export type GateStep = { startedAt: number; endedAt: number };
30
31 export const gateStepText = (step: GateStep): string => `${JSON.stringify(step)}\n`;
32
33 /**
34 * The gate's times from its file, or undefined: no file, not its shape, or
35 * not the gate that ran just before test-shards began at `shardsStartedAt`.
36 */
37 export function readGateStep(text: string | undefined, shardsStartedAt: number): GateStep | undefined {
38 if (text === undefined) return undefined;
39 let parsed: unknown;
40 try {
41 parsed = JSON.parse(text);
42 } catch {
43 return undefined;
44 }
45 const { startedAt, endedAt } = (parsed ?? {}) as Partial<Record<keyof GateStep, unknown>>;
46 if (typeof startedAt !== "number" || typeof endedAt !== "number") return undefined;
47 if (!Number.isFinite(startedAt) || !Number.isFinite(endedAt) || startedAt > endedAt) return undefined;
48 // A second of clock between two processes is not a stale file; a minute is.
49 const gap = shardsStartedAt - endedAt;
50 if (gap < -1000 || gap > GATE_STEP_FRESH_MS) return undefined;
51 return { startedAt, endedAt };
52 }
53
54 /**
55 * The step lines: a heading with the whole check's seconds and where they
56 * were counted from, then each step as it ran. Shards run side by side, so
57 * their shares add up past 100%.
58 */
59 export function stepLines(steps: readonly Step[], totalSeconds: number, from: string): string[] {
60 const share = (seconds: number): string => (totalSeconds > 0 ? ((seconds / totalSeconds) * 100).toFixed(1) : "0.0");
61 return [
62 `test-shards: steps, ${totalSeconds.toFixed(1)}s from ${from} (shards run side by side, so their shares add past 100%):`,
63 ...steps.map((s) => ` ${s.seconds.toFixed(1).padStart(7)}s ${share(s.seconds).padStart(5)}% ${s.name}`),
64 ];
65 }
scripts/test-shards.ts +51 −5
@@ -111,6 +111,7 @@ import { findChromium } from "../src/looks/capture.ts";
111111 import { chromiumRuns } from "../test/helpers/chrome.ts";
112112 import { breaches, breachReport, budgetsFrom, retimeVerdict, breachClosing, breachWarnings, formatTestTimes, loadVerdict, retimeFiles, RETIME_SECONDS, slowUnderLines, parseJunit, slowestTestLines, underLoadLines, unverifiedLines, type AloneRun, type Breach, type TestTime, type UnderLoad, type Unverified } from "./test-budget.ts";
113113 import { cgroupMemory, summaryLines } from "./gate-summary.ts";
114 import { GATE_STEP_FILE, readGateStep, type Step } from "./step-times.ts";
114115 import { GATE_FILE, LOADS_ONLY, coverOf, executedFiles, formatImpact, gateListAdds, handedImpact, mergeImpact, packImpact, usedFiles, type Cover } from "../src/checks/impact.ts";
115116 import { importsReader, readImpact, scansIn } from "./tier-input.ts";
116117 import { changedLine, startChanged } from "./changed-pick.ts";
@@ -1000,14 +1001,26 @@ async function judgeUnderLoad(results: Timed[]): Promise<{ underLoad: UnderLoad[
10001001 return { underLoad, unverified };
10011002 }
10021003
1003 /** Runs slices `from` up to `to`, at most `width` at once, in the slices' order. */
1004 async function pool(width: number, from: number, to: number): Promise<Timed[]> {
1004 /** One worker of a pool: the seconds it spent running slices, and how many files they held. */
1005 type Busy = { seconds: number; files: number };
1006
1007 /**
1008 * Runs slices `from` up to `to`, at most `width` at once, in the slices' order; each worker's
1009 * time is added to its row of `busy` when one is handed in (#16671).
1010 */
1011 async function pool(width: number, from: number, to: number, busy?: Busy[]): Promise<Timed[]> {
10051012 const results: Timed[] = [];
10061013 let next = from;
1007 async function worker(): Promise<void> {
1014 async function worker(_: unknown, w: number): Promise<void> {
10081015 while (next < to) {
10091016 const index = next++;
1010 results[index - from] = await runShard(slices[index]!, index, width === 1);
1017 const result = await runShard(slices[index]!, index, width === 1);
1018 results[index - from] = result;
1019 const row = busy ? (busy[w] ??= { seconds: 0, files: 0 }) : undefined;
1020 if (row) {
1021 row.seconds += result.seconds;
1022 row.files += result.files.length;
1023 }
10111024 }
10121025 }
10131026 await Promise.all(Array.from({ length: width }, worker));
@@ -1055,9 +1068,12 @@ async function loadsOnly(): Promise<Cover | undefined> {
10551068 rmSync(home, { recursive: true, force: true });
10561069 }
10571070 }
1071 // Where the check's time went, step by step, for the timed gate's summary (#16671).
1072 const setupSeconds = performance.now() / 1000;
10581073 if (measure) loaded = await loadsOnly();
10591074
10601075 const started = performance.now();
1076 const busy: Busy[] = [];
10611077 // Serial one-file processes when SHARDS=1; record keeps its worker width;
10621078 // multi-shard still runs every slice side by side.
10631079 const results = [
@@ -1066,13 +1082,16 @@ const results = [
10661082 serialFiles ? 1 : Math.max(1, Math.min(count.value, slices.length)),
10671083 0,
10681084 slices.length,
1085 busy,
10691086 )),
10701087 // Budget files, one at a time, once the rest are done and green.
10711088 ...(await pool(1, slices.length, slices.length)),
10721089 ];
10731090 // What breached beside other files is timed again alone, now, before the tally.
1091 const pooled = performance.now();
10741092 const { underLoad, unverified } = await judgeUnderLoad(results);
1075 const wall = (performance.now() - started) / 1000;
1093 const judged = performance.now();
1094 const wall = (judged - started) / 1000;
10761095 const ending = tally(results, wall);
10771096 for (const line of auditLines(audited, results)) console.log(line);
10781097 const spread = results.map((result) => result.seconds);
@@ -1162,6 +1181,32 @@ if (
11621181 console.log(`test-shards: wrote ${back.files} times to ${HANDED_TIMES}`);
11631182 }
11641183 }
1184 /**
1185 * The check's steps as they ran, from the gate's start when the gate left its times just before
1186 * this process began, else from this process's start (#16671).
1187 */
1188 function stepTimes(): { list: Step[]; seconds: number; from: string } {
1189 const now = performance.now();
1190 let text: string | undefined;
1191 try {
1192 text = readFileSync(GATE_STEP_FILE, "utf8");
1193 } catch {
1194 text = undefined;
1195 }
1196 const gateStep = readGateStep(text, Date.now() - now);
1197 const list: Step[] = [
1198 ...(gateStep ? [{ name: "gate", seconds: (gateStep.endedAt - gateStep.startedAt) / 1000 }] : []),
1199 { name: "test-shards setup: listing, weighing and slicing the files", seconds: setupSeconds },
1200 ...(measure ? [{ name: "covering the suite's loading alone", seconds: started / 1000 - setupSeconds }] : []),
1201 ...busy.map((row, i) => ({ name: `shard ${i + 1}/${busy.length}: ${row.files} file${row.files === 1 ? "" : "s"}`, seconds: row.seconds })),
1202 { name: "re-timing over-budget files alone", seconds: (judged - pooled) / 1000 },
1203 { name: "tally and records: times, impacts, kept results", seconds: (now - judged) / 1000 },
1204 ];
1205 return gateStep
1206 ? { list, seconds: (Date.now() - gateStep.startedAt) / 1000, from: "the gate's start" }
1207 : { list, seconds: now / 1000, from: "test-shards' start (the gate left no times)" };
1208 }
1209
11651210 // The timed full gate is the check on the cell itself: its log ends with
11661211 // what the cell did, so nobody has to measure it by hand (#2065).
11671212 if (timed)
@@ -1179,6 +1224,7 @@ if (timed)
11791224 times: results.map((result) => ({ file: result.files[0]!, seconds: result.seconds })),
11801225 budget: BUDGETS.file,
11811226 slow: SLOW_FILES,
1227 steps: stepTimes(),
11821228 }).join("\n")}\n${slowestTestLines(
11831229 results.flatMap((result) => result.tests),
11841230 results.reduce((sum, result) => sum + result.seconds, 0),
test/step-times.test.ts added +90
@@ -0,0 +1,90 @@
1 /**
2 * Where a timed full gate's time went (#16671): the summary carries one line a step, in the
3 * order the steps ran, each with its seconds and its share of the whole check, beside the
4 * slowest-5 files and tests lines it already had; the gate's own times count only when the gate
5 * ran just before the shards.
6 */
7 import { describe, expect, test } from "bun:test";
8 import { GATE_STEP_FRESH_MS, gateStepText, readGateStep, stepLines, type Step } from "../scripts/step-times.ts";
9 import { summaryLines, type GateSummary } from "../scripts/gate-summary.ts";
10
11 const steps: Step[] = [
12 { name: "gate", seconds: 12.34 },
13 { name: "test-shards setup: listing, weighing and slicing the files", seconds: 3 },
14 { name: "shard 1/2: 3 files", seconds: 150 },
15 { name: "shard 2/2: 2 files", seconds: 140.4 },
16 { name: "re-timing over-budget files alone", seconds: 0 },
17 ];
18
19 describe("stepLines", () => {
20 test("a heading, then each step in the order given, with seconds and share of the total", () => {
21 expect(stepLines(steps, 200, "the gate's start")).toEqual([
22 "test-shards: steps, 200.0s from the gate's start (shards run side by side, so their shares add past 100%):",
23 " 12.3s 6.2% gate",
24 " 3.0s 1.5% test-shards setup: listing, weighing and slicing the files",
25 " 150.0s 75.0% shard 1/2: 3 files",
26 " 140.4s 70.2% shard 2/2: 2 files",
27 " 0.0s 0.0% re-timing over-budget files alone",
28 ]);
29 });
30
31 test("a total of nothing gives every share as 0.0%, never NaN or Infinity", () => {
32 expect(stepLines([{ name: "gate", seconds: 1 }], 0, "x")[1]).toBe(" 1.0s 0.0% gate");
33 });
34 });
35
36 describe("readGateStep", () => {
37 const ended = 1_800_000_000_000;
38 const text = gateStepText({ startedAt: ended - 9000, endedAt: ended });
39
40 test("the gate that ended just before the shards began is counted", () => {
41 expect(readGateStep(text, ended + 400)).toEqual({ startedAt: ended - 9000, endedAt: ended });
42 });
43
44 test("a file left from an earlier check is not", () => {
45 expect(readGateStep(text, ended + GATE_STEP_FRESH_MS + 1)).toBeUndefined();
46 });
47
48 test("no file, junk, a wrong shape or an end before its start is no step", () => {
49 expect(readGateStep(undefined, ended)).toBeUndefined();
50 expect(readGateStep("not json", ended)).toBeUndefined();
51 expect(readGateStep("null", ended)).toBeUndefined();
52 expect(readGateStep(JSON.stringify({ startedAt: "a", endedAt: ended }), ended)).toBeUndefined();
53 expect(readGateStep(gateStepText({ startedAt: ended + 1, endedAt: ended }), ended)).toBeUndefined();
54 });
55
56 test("a gate that ended well after the shards began is no step of theirs", () => {
57 expect(readGateStep(text, ended - 5000)).toBeUndefined();
58 });
59 });
60
61 describe("summaryLines with steps", () => {
62 const base: GateSummary = {
63 shards: 2,
64 cpus: 16,
65 shardsMax: 32,
66 files: 5,
67 override: "2",
68 memoryBytes: 8 * 1024 ** 3,
69 workerBytes: 512 * 1024 ** 2,
70 memory: { limit: undefined, peak: undefined },
71 wallSeconds: 290.5,
72 times: Array.from({ length: 5 }, (_, i) => ({ file: `test/f${i}.test.ts`, seconds: 10 + i })),
73 };
74
75 test("the step lines follow the wall line, in order, and peak memory and the slowest files still follow them", () => {
76 const lines = summaryLines({ ...base, steps: { list: steps, seconds: 200, from: "the gate's start" } });
77 const wall = lines.indexOf("test-shards: wall: 290.5s");
78 const heading = lines.findIndex((line) => line.startsWith("test-shards: steps, 200.0s"));
79 const slowest = lines.indexOf("test-shards: slowest 5 of 5 files:");
80 expect(heading).toBe(wall + 1);
81 expect(lines.slice(heading, heading + 1 + steps.length)).toEqual(stepLines(steps, 200, "the gate's start"));
82 expect(lines[heading + 1 + steps.length]).toBe("test-shards: peak memory: unknown here");
83 expect(slowest).toBe(heading + 2 + steps.length);
84 expect(lines[slowest + 1]).toBe(" 14.0s test/f4.test.ts");
85 });
86
87 test("without steps the block is as it was", () => {
88 expect(summaryLines(base).some((line) => line.startsWith("test-shards: steps"))).toBe(false);
89 });
90 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs