Skip to content

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

TypeScript

test(interaction-harness): a traced long task carries its thread time (#9874)

Christopher Chedeau3b41e2c2e274
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.md
index eccdde353e..0f9e0381a9 100644
--- a/internal/interaction-harness/README.md
+++ b/internal/interaction-harness/README.md
@@ -288,7 +288,12 @@ Layout or Paint there; a layout-property animation does one per frame. Its
(`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.
+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`.
**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/trace.longTasks.test.ts b/internal/interaction-harness/src/trace.longTasks.test.ts
new file mode 100644
index 0000000000..173cfc1a8e
--- /dev/null
+++ b/internal/interaction-harness/src/trace.longTasks.test.ts
@@ -0,0 +1,73 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file trace.longTasks.test.ts
+ * @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.
+ * @position Unit test of ./trace's longTasksOf
+ */
+
+import {describe, expect, it} from 'vitest';
+import {HARNESS_READ, longTasksOf, type TraceEvent} from './trace';
+
+/** Trace time (µs) to epoch ms: a fixed origin, so the start is easy to read. */
+const toEpochMs = (ts: number) => 1_000_000 + ts / 1000;
+
+function task(ts: number, durMs: number, tdurMs?: number): TraceEvent {
+ return {
+ name: 'RunTask',
+ cat: 'toplevel',
+ ph: 'X',
+ ts,
+ dur: durMs * 1000,
+ ...(tdurMs == null ? {} : {tdur: tdurMs * 1000}),
+ pid: 1,
+ tid: 1,
+ };
+}
+
+function inside(ts: number, name: string, args?: TraceEvent['args']) {
+ return {name, cat: 'devtools.timeline', ph: 'X', ts, pid: 1, tid: 1, 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')];
+ expect(longTasksOf(events, toEpochMs)).toEqual({
+ longTasks: [{start: 1_000_001, durationMs: 80, threadMs: 15}],
+ devtoolsTasks: [],
+ });
+ });
+
+ it('reads a task with no thread clock at its wall time', () => {
+ const events = [task(0, 60), inside(500, 'EventDispatch')];
+ expect(longTasksOf(events, toEpochMs).longTasks).toEqual([
+ {start: 1_000_000, durationMs: 60, threadMs: 60},
+ ]);
+ });
+
+ it("puts the harness's own read in devtoolsTasks, with its thread time", () => {
+ const events = [
+ task(0, 120, 118),
+ inside(100, 'Receive mojo message', {
+ chrome_mojo_event_info: {
+ mojo_interface_tag: 'blink.mojom.DevToolsSession',
+ },
+ }),
+ inside(200, 'TimeStamp', {data: {message: HARNESS_READ}}),
+ ];
+ expect(longTasksOf(events, toEpochMs)).toEqual({
+ longTasks: [],
+ devtoolsTasks: [{start: 1_000_000, durationMs: 120, threadMs: 118}],
+ });
+ });
+
+ it('counts no task of 50 ms or less', () => {
+ const events = [task(0, 50, 50), inside(100, 'FunctionCall')];
+ expect(longTasksOf(events, toEpochMs).longTasks).toEqual([]);
+ });
+});
diff --git a/internal/interaction-harness/src/trace.ts b/internal/interaction-harness/src/trace.ts
index 533a32e06b..c3756fd5d7 100644
--- a/internal/interaction-harness/src/trace.ts
+++ b/internal/interaction-harness/src/trace.ts
@@ -6,7 +6,8 @@
* PerformanceObserver for long tasks
* @output `traceRendering` — counts of the main thread's rendering work
* (Layout, Paint, UpdateLayoutTree, PrePaint, …) between two marks — and
- * `installLongTaskLog` / `readLongTasks`.
+ * `installLongTaskLog` / `readLongTasks`. `longTasksOf` gives each long task
+ * its wall and thread time.
* @position The "no Layout/Paint during a transform/opacity animation" proof.
* A compositor animation costs the main thread nothing per frame; one that
* animates a layout property, or writes styles from JavaScript each frame,
@@ -51,6 +52,19 @@ export const RENDERING_EVENTS = [
'HitTest',
] as const;
+/** A main-thread task longer than 50 ms, epoch ms. */
+export interface TracedTask {
+ readonly start: number;
+ /** Wall time, ms. */
+ readonly durationMs: number;
+ /**
+ * The main thread's own run time in the task, from the trace's thread clock,
+ * ms; at most `durationMs`. A busy machine stretches a task's wall time but
+ * not its thread time. Without a thread clock it is the wall time.
+ */
+ readonly threadMs: number;
+}
+
export interface RenderingTrace {
/** Count per trace event name inside the window, on the page's main thread. */
readonly counts: Readonly<Record<string, number>>;
@@ -67,20 +81,14 @@ export interface RenderingTrace {
* `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 {
- readonly start: number;
- readonly durationMs: number;
- }[];
+ readonly longTasks: 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
* script, epoch ms: `waitForAnimation` describing every animation's
* keyframes, or `blockMainThread(page, ms)` without `during`.
*/
- readonly devtoolsTasks: readonly {
- readonly start: number;
- readonly durationMs: number;
- }[];
+ readonly devtoolsTasks: readonly TracedTask[];
/** Every counted rendering event with its epoch-ms time, for sub-window counts. */
readonly events: readonly {readonly name: string; readonly t: number}[];
/** The window, epoch ms on the page's clock. */
@@ -102,12 +110,14 @@ export function countBetween(
return counts;
}
-interface TraceEvent {
+export interface TraceEvent {
name: string;
cat: string;
ph: string;
ts: number;
dur?: number;
+ /** Thread-clock duration, µs. */
+ tdur?: number;
pid: number;
tid: number;
args?: Record<string, unknown>;
@@ -178,12 +188,38 @@ export async function traceRendering(
counted.push({name: e.name, t: toEpochMs(e.ts)});
}
}
+ const {longTasks, devtoolsTasks} = longTasksOf(
+ events.filter(e => onMain(e) && e.ph !== 'E'),
+ toEpochMs,
+ );
+ return {
+ counts,
+ layout: counts.Layout ?? 0,
+ paint: counts.Paint ?? 0,
+ styleRecalc: counts.UpdateLayoutTree ?? 0,
+ prePaint: counts.PrePaint ?? 0,
+ durationMs: (end.ts - start.ts) / 1000,
+ longTasks,
+ devtoolsTasks,
+ events: counted,
+ window: {from: fromEpoch, to: toEpoch},
+ };
+}
+
+/**
+ * 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;
+ * `toEpochMs` turns trace time into epoch ms.
+ */
+export function longTasksOf(
+ main: readonly TraceEvent[],
+ toEpochMs: (ts: number) => number,
+): {longTasks: TracedTask[]; devtoolsTasks: 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 main = events.filter(e => onMain(e) && e.ph !== 'E');
- const longTasks: {start: number; durationMs: number}[] = [];
- const devtoolsTasks: {start: number; durationMs: number}[] = [];
+ const longTasks: TracedTask[] = [];
+ const devtoolsTasks: TracedTask[] = [];
for (const task of main) {
if (task.name !== 'RunTask' || (task.dur ?? 0) <= 50_000) {
continue;
@@ -209,23 +245,14 @@ export async function traceRendering(
HARNESS_READ,
);
const pageScript = inside.some(e => PAGE_SCRIPT_EVENTS.has(e.name));
+ const wall = task.dur ?? 0;
(devtools && harnessRead && !pageScript ? devtoolsTasks : longTasks).push({
start: toEpochMs(task.ts),
- durationMs: (task.dur ?? 0) / 1000,
+ durationMs: wall / 1000,
+ threadMs: Math.min(task.tdur ?? wall, wall) / 1000,
});
}
- return {
- counts,
- layout: counts.Layout ?? 0,
- paint: counts.Paint ?? 0,
- styleRecalc: counts.UpdateLayoutTree ?? 0,
- prePaint: counts.PrePaint ?? 0,
- durationMs: (end.ts - start.ts) / 1000,
- longTasks,
- devtoolsTasks,
- events: counted,
- window: {from: fromEpoch, to: toEpoch},
- };
+ return {longTasks, devtoolsTasks};
}
/**