Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Draw each run before a retry in the timeline on the thread it records (`threadId` on `retryAttempts` entries, from CTRF 0.3.0), falling back to its test's thread.

## 1.4.0 - 2026-11-21

- Add the `timeline-report` input: a Mermaid Gantt chart of when each test ran on each thread or worker, with per-thread busy and idle time and a table of test timings.
Expand Down
154 changes: 154 additions & 0 deletions __tests__/ctrf/timeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2356,6 +2356,160 @@ describe("runs in one report", () => {
});
});

describe("runs before a retry that record their thread", () => {
type Attempt = NonNullable<Test["retryAttempts"]>[number];

/** A failed `retryAttempts` entry with the thread that ran it (CTRF 0.3.0). */
function failedOn(
threadId: unknown,
attempt: number,
start: number,
stop: number,
): Attempt {
return {
attempt,
status: "failed",
start: T0 + start,
stop: T0 + stop,
threadId,
} as Attempt;
}

/** Each lane's name and bars: the test's name, with "#n" for attempt n. */
function bars(data: ReturnType<typeof collectTimeline>) {
return data.lanes.map((lane) => [
lane.name,
lane.executions.map((e) =>
e.attempt === undefined ? e.name : `${e.name} #${e.attempt}`,
),
]);
}

it("draws a run before a retry on its thread, not in a gap on its test's thread", () => {
const data = collectTimeline([
makeTest("search", 0, 1000, { threadId: "w1" }),
makeTest("flaky", 2000, 500, {
threadId: "w2",
retryAttempts: [failedOn("w1", 1, 1200, 1800)],
}),
]);
expect(bars(data)).toEqual([
["w1", ["search", "flaky #1"]],
["w2", ["flaky"]],
]);
expect(data.strayAttempts).toBe(0);
});

it("gives a thread that only ran runs before a retry a lane, and counts it", () => {
const data = collectTimeline([
makeTest("a", 0, 1000, { threadId: "w1" }),
makeTest("flaky", 1500, 500, {
threadId: "w1",
retryAttempts: [failedOn("w2", 1, 0, 1000)],
}),
]);
expect(bars(data)).toEqual([
["w1", ["a", "flaky"]],
["w2", ["flaky #1"]],
]);
expect(data.lanes.map((lane) => lane.origin)).toEqual(["thread", "thread"]);
expect(data.threads).toBe(2);
});

it("splits its thread when it overlaps a test there, like a final result", () => {
// Some frameworks run tests concurrently within one worker.
const data = collectTimeline([
makeTest("search", 0, 1000, { threadId: "w1" }),
makeTest("flaky", 2000, 500, {
threadId: "w2",
retryAttempts: [failedOn("w1", 1, 500, 900)],
}),
]);
expect(bars(data)).toEqual([
["w1", ["search"]],
["w1 (2)", ["flaky #1"]],
["w2", ["flaky"]],
]);
expect(data.splitThreads).toEqual([{ threadId: "w1", lanes: 2 }]);
expect(data.strayAttempts).toBe(0);
});

it("still places runs that record no thread on their test's thread where it was free", () => {
const data = collectTimeline([
makeTest("other", 0, 1000, { threadId: "w1" }),
makeTest("flaky", 3000, 500, {
threadId: "w1",
retryAttempts: [
failedOn("w2", 1, 200, 600),
{ attempt: 2, status: "failed", start: T0 + 700, stop: T0 + 950 },
{ attempt: 3, status: "failed", start: T0 + 1200, stop: T0 + 1900 },
],
}),
]);
expect(bars(data)).toEqual([
["w1", ["other", "flaky #3", "flaky"]],
["w2", ["flaky #1"]],
["Runs before a retry", ["flaky #2"]],
]);
expect(data.strayAttempts).toBe(1);
});

it("reads the thread like a test's: trimmed, numbers as text, blank as none", () => {
const data = collectTimeline([
makeTest("other", 0, 1000, { threadId: "w1" }),
makeTest("flaky", 3000, 500, {
threadId: "w1",
retryAttempts: [
failedOn(" w2 ", 1, 1100, 1500),
failedOn(7, 2, 1600, 2000),
failedOn("", 3, 2100, 2900),
],
}),
]);
expect(bars(data)).toEqual([
["7", ["flaky #2"]],
["w1", ["other", "flaky #3", "flaky"]],
["w2", ["flaky #1"]],
]);
});

it("keeps an earlier result of a test that ran again on the thread it records", () => {
// Gradle forks share the thread name "Test worker", so the earlier
// run's results overlap on it.
const data = collectTimeline([
makeTest("flaky", 0, 1000, { threadId: "Test worker", status: "failed" }),
makeTest("other", 500, 1000, { threadId: "Test worker" }),
makeTest("flaky", 3 * HOUR, 1000, { threadId: "Test worker" }),
]);
expect(bars(data)).toEqual([
["Test worker", ["flaky #0"]],
["Test worker (2)", ["other", "flaky"]],
]);
expect(data.strayAttempts).toBe(0);
});

it("explains only the runs that record no thread as moved to a lane of their own", () => {
const report = buildTimelineReport([
makeTest("other", 0, 1000, { threadId: "w1" }),
makeTest("flaky", 2000, 500, {
threadId: "w1",
retryAttempts: [
failedOn("w2", 1, 100, 400),
{ attempt: 2, status: "failed", start: T0 + 500, stop: T0 + 900 },
],
}),
]);
expect(report.notes).toContain(
"1 run before a retry overlaps other tests on its thread (such runs do not record where they ran), so it is shown in a lane of its own.",
);
expect(report.lanes.map((lane) => lane.name)).toEqual([
"w1",
"w2",
"Runs before a retry",
]);
});
});

describe("large reports", () => {
it("draws a stretch of 130,000 tests that follows an idle moment", () => {
const tests: Test[] = [makeTest("smoke", 0, 10, { threadId: "w0" })];
Expand Down
3 changes: 2 additions & 1 deletion ctrf-reports/timeline/ctrf-report.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"reportFormat": "CTRF",
"specVersion": "0.2.0",
"specVersion": "0.3.0",
"results": {
"tool": {
"name": "playwright"
Expand Down Expand Up @@ -51,6 +51,7 @@
"duration": 2600,
"start": 1767254410500,
"stop": 1767254413100,
"threadId": "worker-3",
"message": "Timed out waiting for the reset email"
}
]
Expand Down
18 changes: 10 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions docs/report-showcase.md
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,10 @@ retries happened.
was retried (`retryAttempts`), gray any other status, including a skipped
test that took time. A failure too short to see is drawn as a diamond, or as
a thin sliver on rows with many of them.
- A test that was retried shows its runs before the retry too. Those runs do
not record a thread, so they are drawn on their test's thread where it was
free, and in a row of their own otherwise.
- A test that was retried shows its runs before the retry too, each on the
thread it records (`threadId` on a `retryAttempts` entry, from CTRF 0.3.0).
A run that records no thread is drawn on its test's thread where it was
free, and in a row of its own otherwise.
- Below the headline, the slowest test and the test that finished last, the
one that kept the run going, are named.
- The time axis starts at 0:00 with the first test, and the test timings table
Expand Down
53 changes: 33 additions & 20 deletions src/ctrf/timeline/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ interface Entry {
>;
status: TestStatus;
times: Times;
attempts: Array<{ status: TestStatus; times: Times; attempt: number }>;
attempts: Array<{
status: TestStatus;
times: Times;
attempt: number;
threadId: string | undefined;
}>;
}

/** How much an execution may overlap the next one in its lane, in milliseconds. */
Expand All @@ -102,12 +107,13 @@ type Tolerance = (execution: TimelineExecution) => number;
* arranges them into lanes that never run two things at once.
*
* Lanes are thread IDs (or pytest-xdist workers in `extra.worker`). A thread
* whose tests overlap in time is split into several lanes. Earlier attempts do
* not record a thread, so they go on their test's thread where it was free,
* and in a lane of their own otherwise. Without thread IDs, or with more of
* them than fit, lanes are inferred by packing executions by start time, which
* shows how many tests ran at once. If even that needs too many lanes, the
* lanes with failures and the busiest ones are drawn and the rest are counted.
* whose tests overlap in time is split into several lanes. An earlier attempt
* goes on the thread it records; one that records none goes on its test's
* thread where it was free, and in a lane of its own otherwise. Without thread
* IDs, or with more of them than fit, lanes are inferred by packing executions
* by start time, which shows how many tests ran at once. If even that needs
* too many lanes, the lanes with failures and the busiest ones are drawn and
* the rest are counted.
*
* Start and stop times in seconds, microseconds or nanoseconds are converted
* to milliseconds and results listed twice are counted once. When a report
Expand Down Expand Up @@ -152,6 +158,9 @@ export function collectTimeline(
units.add(attemptTiming.unit);
attempts.push({
...base,
...(attempt.threadId === undefined
? { inheritedThread: true }
: { threadId: attempt.threadId }),
status: attempt.status,
kind: kindOf(attempt.status, true),
start: attemptTiming.start,
Expand Down Expand Up @@ -360,6 +369,7 @@ function readTests(tests: Test[]): { entries: Entry[]; coarse: boolean } {
times: read(attempt.start, attempt.stop, attempt.duration),
attempt:
typeof attempt.attempt === "number" ? attempt.attempt : index + 1,
threadId: normalizeThreadId(attempt.threadId),
});
});
}
Expand Down Expand Up @@ -900,10 +910,11 @@ function cutBetweenRuns(lanes: TimelineLane[]): {
}

/**
* Puts final results in lanes by thread, then each earlier attempt on its
* test's thread where that thread was free, and the attempts that would
* overlap another test there in lanes of their own. With times in whole
* seconds, a thread's attempts are laid out with its other tests.
* Puts final results and the earlier attempts that record their thread in
* lanes by thread, then each attempt that records none on its test's thread
* where that thread was free, and the attempts that would overlap another
* test there in lanes of their own. With times in whole seconds, a thread's
* attempts are laid out with its other tests.
*/
function arrange(
executions: TimelineExecution[],
Expand Down Expand Up @@ -932,8 +943,10 @@ function arrange(

interface Group {
all: TimelineExecution[];
finals: TimelineExecution[];
attempts: TimelineExecution[];
/** Executions that record this thread. */
recorded: TimelineExecution[];
/** Runs before a retry that take this thread from their test. */
inherited: TimelineExecution[];
}
const groups = new Map<string, Group>();
const unthreaded: TimelineExecution[] = [];
Expand All @@ -944,11 +957,11 @@ function arrange(
}
let group = groups.get(execution.threadId);
if (!group) {
group = { all: [], finals: [], attempts: [] };
group = { all: [], recorded: [], inherited: [] };
groups.set(execution.threadId, group);
}
group.all.push(execution);
(execution.attempt === undefined ? group.finals : group.attempts).push(
(execution.inheritedThread ? group.inherited : group.recorded).push(
execution,
);
}
Expand All @@ -959,17 +972,17 @@ function arrange(
const stray: TimelineExecution[] = [];
const threadIds = [...groups.keys()].sort(threadOrder.compare);
for (const threadId of threadIds) {
const { all, finals, attempts } = groups.get(threadId) as Group;
const { all, recorded, inherited } = groups.get(threadId) as Group;
let threadLanes: TimelineExecution[][];
if (coarse) {
threadLanes = packCoarse(all);
} else if (finals.length === 0) {
threadLanes = pack(attempts, threadTolerance);
} else if (recorded.length === 0) {
threadLanes = pack(inherited, threadTolerance);
} else {
threadLanes = pack(finals, threadTolerance);
threadLanes = pack(recorded, threadTolerance);
for (const attempt of placeAttempts(
threadLanes,
attempts,
inherited,
threadTolerance,
)) {
stray.push(attempt);
Expand Down
10 changes: 9 additions & 1 deletion src/ctrf/timeline/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export interface TimelineExecution {
/** Epoch milliseconds, never before `start`. */
stop: number;
threadId?: string;
/**
* True for a `retryAttempts` entry that does not record a thread: its
* `threadId` is its test's, the thread of the final attempt.
*/
inheritedThread?: boolean;
/**
* Attempt number of a `retryAttempts` entry, 0 for an earlier result of a
* test that ran again in a later run, undefined for the final result.
Expand Down Expand Up @@ -103,7 +108,10 @@ export interface TimelineData {
crowdedThreads: number;
/** Thread IDs whose tests overlap in time and are drawn as several lanes. */
splitThreads: Array<{ threadId: string; lanes: number }>;
/** Runs before a retry drawn in a lane of their own because they overlap tests on their thread. */
/**
* Runs before a retry that do not record a thread, drawn in a lane of their
* own because they overlap tests on their test's thread.
*/
strayAttempts: number;
/** Final test results with usable times, in drawn and hidden lanes. */
tests: number;
Expand Down
Loading