Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(interaction-harness): a long task is the page's own work (#10510)

Christopher Chedeau10acb8c1c0fb
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.md
index f2176327e7..fffff384f9 100644
--- a/internal/interaction-harness/README.md
+++ b/internal/interaction-harness/README.md
@@ -406,13 +406,15 @@ out an anchored one), so a proof takes its traced watch before the window it
counts. The trace's `longTasks` are the page's own: only a task the harness marked as its own read
(`waitForAnimation` describing keyframes, `blockMainThread` without `during`)
is in `devtoolsTasks` instead, and only when no page script ran inside it. A
-test's own `page.evaluate` stays a long task, whether it calls page code
-directly or sets it off. Each long task carries its wall time (`durationMs`)
+test's own `page.evaluate` stays the page's own task, long or stretched,
+whether it calls page code directly or sets it off. Each long task carries its wall time (`durationMs`)
and the main thread's own run time (`threadMs`, from the trace's thread
-clock). A busy machine stretches the first but not the second, so a claim
-about the page's own work judges `threadMs`. A thread that waits (a
-synchronous request, `Atomics.wait`) gains wall time but no thread time, so a
-claim about a blocking wait judges `durationMs`.
+clock). A busy machine stretches the first but not the second, so a task is
+long only when its thread ran longer than 50 ms: the page's own work. A task
+only the wall clock stretched past 50 ms is in `stretchedTasks`, with both
+times. A thread that waits (a synchronous request, `Atomics.wait`) gains wall
+time but no thread time, so a claim about a blocking wait judges
+`stretchedTasks` too.
**Storyboards.** Deterministic: frames come from seeking held animations or
from stepping scripted input, never from wall-clock capture. The JSON trace
diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
index 14e84f65e6..e0a4d4f07a 100644
--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
@@ -2737,17 +2737,20 @@ test.describe('frame evidence', () => {
const within = (w: {from: number; to: number}) => (t: {start: number}) =>
t.start >= w.from - 20 && t.start < w.to;
// The read itself is set apart. The rows' frames around it may be long
- // tasks too, the page's own: they stay in longTasks.
+ // tasks too, the page's own: they stay with the page's tasks. A loaded
+ // machine can keep a 60 ms spin's thread under 50 ms, so the page's own
+ // tasks are its long and its stretched ones.
expect(trace.devtoolsTasks.some(within(read))).toBe(true);
- expect(
- trace.longTasks.some(t => within(timer)(t) && t.durationMs >= 55),
- ).toBe(true);
- expect(
- trace.longTasks.some(t => within(click)(t) && t.durationMs >= 55),
- ).toBe(true);
- expect(
- trace.longTasks.some(t => within(direct)(t) && t.durationMs >= 55),
- ).toBe(true);
+ const pageTasks = [...trace.longTasks, ...trace.stretchedTasks];
+ expect(pageTasks.some(t => within(timer)(t) && t.durationMs >= 55)).toBe(
+ true,
+ );
+ expect(pageTasks.some(t => within(click)(t) && t.durationMs >= 55)).toBe(
+ true,
+ );
+ expect(pageTasks.some(t => within(direct)(t) && t.durationMs >= 55)).toBe(
+ true,
+ );
expect(trace.devtoolsTasks.filter(within(direct))).toEqual([]);
});
});
diff --git a/internal/interaction-harness/src/trace.longTasks.test.ts b/internal/interaction-harness/src/trace.longTasks.test.ts
index 173cfc1a8e..ffbf3aa980 100644
--- a/internal/interaction-harness/src/trace.longTasks.test.ts
+++ b/internal/interaction-harness/src/trace.longTasks.test.ts
@@ -5,8 +5,9 @@
* @input Synthetic main-thread trace events (no browser)
* @output A long task keeps its wall time and its thread time apart. A task
* without a thread clock reads at its wall time. The harness's own read goes
- * to `devtoolsTasks`, a page script's task to `longTasks`. A task of 50 ms or
- * less is no long task.
+ * to `devtoolsTasks`, a page script's task to `longTasks` when its thread ran
+ * longer than 50 ms, else to `stretchedTasks`. A task of 50 ms or less is no
+ * long task.
* @position Unit test of ./trace's longTasksOf
*/
@@ -35,11 +36,22 @@ function inside(ts: number, name: string, args?: TraceEvent['args']) {
describe('longTasksOf', () => {
it("keeps a page task's thread time apart from its wall time", () => {
- // A busy machine kept the main thread off the CPU for most of the task.
- const events = [task(1000, 80, 15), inside(2000, 'FunctionCall')];
+ const events = [task(1000, 80, 60), inside(2000, 'FunctionCall')];
expect(longTasksOf(events, toEpochMs)).toEqual({
- longTasks: [{start: 1_000_001, durationMs: 80, threadMs: 15}],
+ longTasks: [{start: 1_000_001, durationMs: 80, threadMs: 60}],
devtoolsTasks: [],
+ stretchedTasks: [],
+ });
+ });
+
+ it('sets apart a task only the wall clock stretched past 50 ms', () => {
+ // A busy machine kept the main thread off the CPU for most of the task:
+ // 51 ms of wall time, 11.5 ms of its own.
+ const events = [task(1000, 51, 11.5), inside(2000, 'FunctionCall')];
+ expect(longTasksOf(events, toEpochMs)).toEqual({
+ longTasks: [],
+ devtoolsTasks: [],
+ stretchedTasks: [{start: 1_000_001, durationMs: 51, threadMs: 11.5}],
});
});
@@ -63,6 +75,7 @@ describe('longTasksOf', () => {
expect(longTasksOf(events, toEpochMs)).toEqual({
longTasks: [],
devtoolsTasks: [{start: 1_000_000, durationMs: 120, threadMs: 118}],
+ stretchedTasks: [],
});
});
diff --git a/internal/interaction-harness/src/trace.ts b/internal/interaction-harness/src/trace.ts
index 5ca17350f9..a77ee7d411 100644
--- a/internal/interaction-harness/src/trace.ts
+++ b/internal/interaction-harness/src/trace.ts
@@ -87,12 +87,20 @@ export interface RenderingTrace {
/** Window length, ms. */
readonly durationMs: number;
/**
- * The page's own main-thread tasks longer than 50 ms inside the window (long
- * tasks), epoch ms. A task that ran a test's own DevTools command (a
- * `page.evaluate`) is one, whether it calls page code directly or sets it
- * off; only the harness's own marked reads are in `devtoolsTasks`.
+ * The page's own main-thread tasks inside the window whose main thread ran
+ * longer than 50 ms (long tasks), epoch ms. A task that ran a test's own
+ * DevTools command (a `page.evaluate`) is one, whether it calls page code
+ * directly or sets it off. Only the harness's own marked reads are in
+ * `devtoolsTasks`.
*/
readonly longTasks: readonly TracedTask[];
+ /**
+ * The page's own tasks longer than 50 ms of wall time whose main thread ran
+ * 50 ms or less, epoch ms. A busy machine kept the renderer off the CPU, or
+ * the thread waited (a synchronous request, `Atomics.wait`). A claim about
+ * a blocking wait judges these too.
+ */
+ readonly stretchedTasks: readonly TracedTask[];
/**
* Main-thread tasks longer than 50 ms inside the window that ran a DevTools
* command the harness marked as its own read (`HARNESS_READ`) and no page
@@ -248,7 +256,7 @@ export async function traceRendering(
});
}
}
- const {longTasks, devtoolsTasks} = longTasksOf(
+ const {longTasks, devtoolsTasks, stretchedTasks} = longTasksOf(
events.filter(e => onMain(e) && e.ph !== 'E'),
toEpochMs,
);
@@ -260,6 +268,7 @@ export async function traceRendering(
prePaint: counts.PrePaint ?? 0,
durationMs: (end.ts - start.ts) / 1000,
longTasks,
+ stretchedTasks,
devtoolsTasks,
events: counted,
window: {from: fromEpoch, to: toEpoch},
@@ -268,18 +277,25 @@ export async function traceRendering(
/**
* The main thread's tasks longer than 50 ms, split into the page's own and the
- * harness's marked reads. `main` holds the main thread's events in the window;
+ * harness's marked reads. A page task is long when its main thread ran longer
+ * than 50 ms; one only the wall clock stretched past that is apart
+ * (`stretchedTasks`). `main` holds the main thread's events in the window;
* `toEpochMs` turns trace time into epoch ms.
*/
export function longTasksOf(
main: readonly TraceEvent[],
toEpochMs: (ts: number) => number,
-): {longTasks: TracedTask[]; devtoolsTasks: TracedTask[]} {
+): {
+ longTasks: TracedTask[];
+ devtoolsTasks: TracedTask[];
+ stretchedTasks: TracedTask[];
+} {
// A task that ran a DevTools command received it over the DevTools
// session's pipe; page script it set off shows as its listener, timer or
// frame callback.
const longTasks: TracedTask[] = [];
const devtoolsTasks: TracedTask[] = [];
+ const stretchedTasks: TracedTask[] = [];
for (const task of main) {
if (task.name !== 'RunTask' || (task.dur ?? 0) <= 50_000) {
continue;
@@ -306,13 +322,20 @@ export function longTasksOf(
);
const pageScript = inside.some(e => PAGE_SCRIPT_EVENTS.has(e.name));
const wall = task.dur ?? 0;
- (devtools && harnessRead && !pageScript ? devtoolsTasks : longTasks).push({
+ const traced = {
start: toEpochMs(task.ts),
durationMs: wall / 1000,
threadMs: Math.min(task.tdur ?? wall, wall) / 1000,
- });
+ };
+ if (devtools && harnessRead && !pageScript) {
+ devtoolsTasks.push(traced);
+ } else if (traced.threadMs > 50) {
+ longTasks.push(traced);
+ } else {
+ stretchedTasks.push(traced);
+ }
}
- return {longTasks, devtoolsTasks};
+ return {longTasks, devtoolsTasks, stretchedTasks};
}
/**