From 98e8e64041d2a83209bdd0a41b7819fd8f66fc6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20Gro=C3=9F?= Date: Mon, 23 Nov 2026 18:51:08 +0100 Subject: [PATCH 1/2] Split overview time by render work and interrupts --- README.md | 14 +- src/engine/extractors/overview.ts | 98 +++++++------ src/engine/types.ts | 5 +- src/formatters/markdown.ts | 5 +- test/extractors/overview.test.ts | 59 +++++++- test/fixtures/render-kinds-trace.json | 195 ++++++++++++++++++++++++++ test/formatters/markdown.test.ts | 14 +- 7 files changed, 335 insertions(+), 55 deletions(-) create mode 100644 test/fixtures/render-kinds-trace.json diff --git a/README.md b/README.md index 4802c86..91841bf 100644 --- a/README.md +++ b/README.md @@ -297,11 +297,12 @@ All time-range inputs (`--time-range`, `--window-a`, `--window-b`, `--bucket`) s ## Subcommands ### 1. `overview` (default) -High-level trace metadata, Core Web Vitals (FCP, LCP, CLS, INP), non-overlapping main-thread category breakdown (Scripting, Layout, Rendering, GC, System, Idle), and diagnostic warnings. +High-level trace metadata, Core Web Vitals (FCP, LCP, CLS, INP), a non-overlapping breakdown of main-thread time, and diagnostic warnings. The breakdown uses the same instrumented timeline events with and without CPU samples. With `--thread PID:TID`, the breakdown describes that renderer thread and Web Vitals still describe the page. +Style recalculation appears in `styleMs`, and `interruptMs` tracks V8 API interrupt callbacks used by the tracer and CPU throttling. ```bash # Default invocation (includes the drill-down commands footer) @@ -334,11 +335,14 @@ coreWebVitals: inp: null mainThreadBreakdown: scriptingMs: 1.69 - layoutMs: 80.13 - renderingMs: 17.9 + styleMs: 30 + layoutMs: 50.13 + paintMs: 14.9 + compositeMs: 3 gcMs: 0.07 - systemMs: 233.52 - idleMs: 5057.64 + interruptMs: 100 + systemMs: 133.52 + idleMs: 5057.65 totalMs: 5390.96 warnings[1]: 1 render-blocking resource(s) detected. diff --git a/src/engine/extractors/overview.ts b/src/engine/extractors/overview.ts index 55b322c..62e5b87 100644 --- a/src/engine/extractors/overview.ts +++ b/src/engine/extractors/overview.ts @@ -172,14 +172,15 @@ export function extractOverview( } // 4. Main Thread Breakdown (Derived from TraceTree self-times to eliminate double-counting) - const breakdown = { + const work = { scriptingMs: 0, + styleMs: 0, layoutMs: 0, - renderingMs: 0, + paintMs: 0, + compositeMs: 0, gcMs: 0, + interruptMs: 0, systemMs: 0, - idleMs: 0, - totalMs: 0, }; const mainThread = getRendererThread(data, options?.threadId); @@ -200,61 +201,78 @@ export function extractOverview( endTime: engine.Types.Timing.Milli(maxMs), }); + const { Name } = engine.Types.Events; const children = bottomUp.children(); for (const node of children.values()) { if (node.selfTime <= 0) continue; const name = node.event?.name || String(node.id) || ''; if ( - name === 'MajorGC' || - name === 'MinorGC' || - name === 'GCEvent' || + name === Name.MAJOR_GC || + name === Name.MINOR_GC || + name === Name.GC || name.toLowerCase().startsWith('v8.gc') ) { - breakdown.gcMs += node.selfTime; - } else if (name === 'Layout' || name === 'UpdateLayoutTree' || name === 'InvalidateLayout') { - breakdown.layoutMs += node.selfTime; + work.gcMs += node.selfTime; + } else if (name === Name.RECALC_STYLE) { + work.styleMs += node.selfTime; + } else if (name === Name.LAYOUT || name === Name.INVALIDATE_LAYOUT) { + work.layoutMs += node.selfTime; + } else if ( + name === Name.PRE_PAINT || + name === Name.PAINT || + name === Name.PAINT_IMAGE || + name === Name.RASTER_TASK || + name === Name.DECODE_IMAGE || + name === Name.UPDATE_LAYER + ) { + work.paintMs += node.selfTime; } else if ( - name === 'UpdateLayerTree' || - name === 'Layerize' || - name === 'Paint' || - name === 'CompositeLayers' || - name === 'RasterTask' || - name === 'DecodeImage' + name === Name.UPDATE_LAYER_TREE || + name === Name.LAYERIZE || + name === Name.COMPOSITE_LAYERS || + name === Name.COMMIT ) { - breakdown.renderingMs += node.selfTime; + work.compositeMs += node.selfTime; + } else if (name === 'V8.InvokeApiInterruptCallbacks') { + work.interruptMs += node.selfTime; } else if ( - name === 'EvaluateScript' || - name === 'FunctionCall' || - name.startsWith('v8.compile') || + name === Name.EVALUATE_SCRIPT || + name === Name.FUNCTION_CALL || + name.startsWith(Name.COMPILE) || name === 'v8.callFunction' || - name === 'RunMicrotasks' || + name === Name.RUN_MICROTASKS || name === 'v8.run' || - name === 'TimerFire' || - name === 'FireAnimationFrame' || - name === 'EventDispatch' + name === Name.TIMER_FIRE || + name === Name.FIRE_ANIMATION_FRAME || + name === Name.EVENT_DISPATCH ) { - breakdown.scriptingMs += node.selfTime; + work.scriptingMs += node.selfTime; } else { - breakdown.systemMs += node.selfTime; + work.systemMs += node.selfTime; } } } - const activeWork = - breakdown.scriptingMs + - breakdown.layoutMs + - breakdown.renderingMs + - breakdown.gcMs + - breakdown.systemMs; - - breakdown.idleMs = round(Math.max(0, durationMs - activeWork)); - breakdown.scriptingMs = round(breakdown.scriptingMs); - breakdown.layoutMs = round(breakdown.layoutMs); - breakdown.renderingMs = round(breakdown.renderingMs); - breakdown.gcMs = round(breakdown.gcMs); - breakdown.systemMs = round(breakdown.systemMs); - breakdown.totalMs = durationMs; + let activeWork = 0; + // Round cumulative time so category rounding cannot add or remove time from the total. + function roundKind(ms: number): number { + const previous = round(activeWork); + activeWork += ms; + return round(round(activeWork) - previous); + } + const breakdown = { + scriptingMs: roundKind(work.scriptingMs), + styleMs: roundKind(work.styleMs), + layoutMs: roundKind(work.layoutMs), + paintMs: roundKind(work.paintMs), + compositeMs: roundKind(work.compositeMs), + gcMs: roundKind(work.gcMs), + interruptMs: roundKind(work.interruptMs), + systemMs: roundKind(work.systemMs), + idleMs: round(Math.max(0, durationMs - round(activeWork))), + totalMs: durationMs, + }; // 5. Warnings const warnings: string[] = []; diff --git a/src/engine/types.ts b/src/engine/types.ts index 5d36706..52d7454 100644 --- a/src/engine/types.ts +++ b/src/engine/types.ts @@ -51,9 +51,12 @@ export interface OverviewResult { }; mainThreadBreakdown: { scriptingMs: number; + styleMs: number; layoutMs: number; - renderingMs: number; + paintMs: number; + compositeMs: number; gcMs: number; + interruptMs: number; systemMs: number; idleMs: number; totalMs: number; diff --git a/src/formatters/markdown.ts b/src/formatters/markdown.ts index 3c73edd..556f55c 100644 --- a/src/formatters/markdown.ts +++ b/src/formatters/markdown.ts @@ -74,9 +74,12 @@ export function formatOverviewMarkdown(overview: OverviewResult): string { const total = b.totalMs > 0 ? b.totalMs : 1; const rows = [ { name: 'Scripting', ms: b.scriptingMs }, + { name: 'Style', ms: b.styleMs }, { name: 'Layout', ms: b.layoutMs }, - { name: 'Rendering', ms: b.renderingMs }, + { name: 'Paint', ms: b.paintMs }, + { name: 'Composite', ms: b.compositeMs }, { name: 'Garbage Collection', ms: b.gcMs }, + { name: 'Interrupts (tracer, throttling)', ms: b.interruptMs }, { name: 'System', ms: b.systemMs }, { name: 'Idle', ms: b.idleMs }, ]; diff --git a/test/extractors/overview.test.ts b/test/extractors/overview.test.ts index 24d8ff3..5b992a3 100644 --- a/test/extractors/overview.test.ts +++ b/test/extractors/overview.test.ts @@ -36,12 +36,15 @@ describe('Overview Extractor', () => { assert.equal(overview.mainUrl, 'https://example.com/test'); assert.equal(overview.navigationStartMs, 1050); assert.deepEqual(overview.mainThreadBreakdown, { - systemMs: 60, - idleMs: 100, scriptingMs: 0, + styleMs: 0, layoutMs: 0, - renderingMs: 0, + paintMs: 0, + compositeMs: 0, gcMs: 0, + interruptMs: 0, + systemMs: 60, + idleMs: 100, totalMs: 160, }); }); @@ -67,23 +70,69 @@ describe('Overview Extractor', () => { assert.ok(overview.warnings.includes('1 render-blocking resource(s) detected.')); assert.deepEqual(overview.mainThreadBreakdown, { scriptingMs: 70, + styleMs: 0, layoutMs: 20, - renderingMs: 10, + paintMs: 10, + compositeMs: 0, gcMs: 25, + interruptMs: 0, systemMs: 65, idleMs: 4010, totalMs: 4200, }); const activeAndIdleSum = overview.mainThreadBreakdown.scriptingMs + + overview.mainThreadBreakdown.styleMs + overview.mainThreadBreakdown.layoutMs + - overview.mainThreadBreakdown.renderingMs + + overview.mainThreadBreakdown.paintMs + + overview.mainThreadBreakdown.compositeMs + overview.mainThreadBreakdown.gcMs + + overview.mainThreadBreakdown.interruptMs + overview.mainThreadBreakdown.systemMs + overview.mainThreadBreakdown.idleMs; assert.equal(activeAndIdleSum, overview.mainThreadBreakdown.totalMs); }); + test('splits render work and V8 API interrupts into exclusive categories', async () => { + const fixture = path.resolve(import.meta.dirname, '../fixtures/render-kinds-trace.json'); + const loaded = await loadTraceFromFile(fixture); + const b = extractOverview(loaded, { threadId: '100:1' }).mainThreadBreakdown; + + assert.equal(b.scriptingMs, 6); + assert.equal(b.styleMs, 4); + assert.equal(b.layoutMs, 3); + assert.equal(b.paintMs, 4); + assert.equal(b.compositeMs, 2); + assert.equal(b.gcMs, 0); + assert.equal(b.interruptMs, 5); + assert.equal(b.systemMs, 6); + assert.equal(b.idleMs, 10); + assert.equal( + b.scriptingMs + + b.styleMs + + b.layoutMs + + b.paintMs + + b.compositeMs + + b.gcMs + + b.interruptMs + + b.systemMs + + b.idleMs, + b.totalMs, + ); + }); + + test('keeps a fractional window fully accounted for after rounding', async () => { + const fixture = path.resolve(import.meta.dirname, '../fixtures/render-kinds-trace.json'); + const loaded = await loadTraceFromFile(fixture); + const b = extractOverview(loaded, { + timeRange: { startMs: 1017.994, endMs: 1018.006 }, + }).mainThreadBreakdown; + + assert.equal(b.idleMs, 0); + assert.equal(b.totalMs, 0.01); + assert.equal(b.layoutMs + b.paintMs, b.totalMs); + }); + test('keeps timeline categories stable when CPU samples add nested calls', async (t) => { const fixture = path.resolve(import.meta.dirname, '../fixtures/navigation-cwv-trace.json'); const events = parseTraceJson(await readFile(fixture, 'utf8')); diff --git a/test/fixtures/render-kinds-trace.json b/test/fixtures/render-kinds-trace.json new file mode 100644 index 0000000..ee528e1 --- /dev/null +++ b/test/fixtures/render-kinds-trace.json @@ -0,0 +1,195 @@ +[ + { + "args": { + "name": "Browser" + }, + "cat": "__metadata", + "name": "process_name", + "ph": "M", + "pid": 1, + "tid": 1, + "ts": 0 + }, + { + "args": { + "name": "Renderer" + }, + "cat": "__metadata", + "name": "process_name", + "ph": "M", + "pid": 100, + "tid": 1, + "ts": 0 + }, + { + "args": { + "name": "CrBrowserMain" + }, + "cat": "__metadata", + "name": "thread_name", + "ph": "M", + "pid": 1, + "tid": 1, + "ts": 0 + }, + { + "args": { + "name": "CrRendererMain" + }, + "cat": "__metadata", + "name": "thread_name", + "ph": "M", + "pid": 100, + "tid": 1, + "ts": 0 + }, + { + "args": { + "data": { + "frames": [ + { + "frame": "FRAME_0", + "url": "https://example.com/test", + "isOutermostMainFrame": true, + "isInPrimaryMainFrame": true, + "processId": 100 + } + ] + } + }, + "cat": "disabled-by-default-devtools.timeline", + "name": "TracingStartedInBrowser", + "ph": "I", + "pid": 1, + "tid": 1, + "ts": 1000000 + }, + { + "args": {}, + "cat": "devtools.timeline", + "name": "RunTask", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1010000, + "dur": 30000 + }, + { + "args": { + "beginData": { + "frame": "FRAME_0" + } + }, + "cat": "devtools.timeline", + "name": "UpdateLayoutTree", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1011000, + "dur": 4000 + }, + { + "args": { + "beginData": { + "frame": "FRAME_0", + "dirtyObjects": 1, + "partialLayout": false, + "totalObjects": 1 + }, + "endData": { + "layoutRoots": [] + } + }, + "cat": "devtools.timeline", + "name": "Layout", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1015000, + "dur": 3000 + }, + { + "args": {}, + "cat": "devtools.timeline", + "name": "PrePaint", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1018000, + "dur": 1000 + }, + { + "args": { + "data": { + "frame": "FRAME_0", + "layerId": 1 + } + }, + "cat": "devtools.timeline", + "name": "Paint", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1019000, + "dur": 2000 + }, + { + "args": {}, + "cat": "devtools.timeline", + "name": "Layerize", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1021000, + "dur": 1000 + }, + { + "args": {}, + "cat": "devtools.timeline", + "name": "Commit", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1022000, + "dur": 1000 + }, + { + "args": {}, + "cat": "devtools.timeline", + "name": "Decode Image", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1023000, + "dur": 1000 + }, + { + "args": {}, + "cat": "v8", + "name": "V8.InvokeApiInterruptCallbacks", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1024000, + "dur": 5000 + }, + { + "args": { + "data": { + "frame": "FRAME_0", + "functionName": "render", + "scriptId": "1", + "url": "https://example.com/app.js", + "lineNumber": 1, + "columnNumber": 1 + } + }, + "cat": "devtools.timeline", + "name": "FunctionCall", + "ph": "X", + "pid": 100, + "tid": 1, + "ts": 1029000, + "dur": 6000 + } +] diff --git a/test/formatters/markdown.test.ts b/test/formatters/markdown.test.ts index 2697c16..b656868 100644 --- a/test/formatters/markdown.test.ts +++ b/test/formatters/markdown.test.ts @@ -60,10 +60,13 @@ describe('Markdown Formatter', () => { }, mainThreadBreakdown: { scriptingMs: 200, - layoutMs: 100, - renderingMs: 50, + styleMs: 40, + layoutMs: 60, + paintMs: 35, + compositeMs: 15, gcMs: 20, - systemMs: 50, + interruptMs: 20, + systemMs: 30, idleMs: 580, totalMs: 1000, }, @@ -77,6 +80,11 @@ describe('Markdown Formatter', () => { assert.ok(output.includes('| FCP | 200ms | good | - |')); assert.ok(output.includes('## Main Thread Time Breakdown')); assert.ok(output.includes('| Scripting | 200ms | 20.0% |')); + assert.ok(output.includes('| Style | 40ms | 4.0% |')); + assert.ok(output.includes('| Paint | 35ms | 3.5% |')); + assert.ok(output.includes('| Composite | 15ms | 1.5% |')); + assert.ok(output.includes('| Interrupts (tracer, throttling) | 20ms | 2.0% |')); + assert.ok(!output.includes('| Rendering |')); assert.ok(output.includes('## Warnings & Diagnostics')); }); From 03ba449012be9a503b43253579e7f8e0c2543323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20Gro=C3=9F?= Date: Mon, 23 Nov 2026 18:54:00 +0100 Subject: [PATCH 2/2] Align the overview command summary with the new breakdown --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 91841bf..c5fdb00 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ Use `trace-engine-cli` to inspect, profile, and diagnose performance recordings ## 3. Command Reference ### `trace-engine-cli ` / `trace-engine-cli overview ` -High-level trace metadata, duration, Core Web Vitals (LCP, INP, CLS), and main-thread category breakdown (Scripting, Layout, Rendering, GC, System, Idle). Calling `trace-engine-cli ` without `overview` also prints the `# Drill-down commands:` reference footer. +High-level trace metadata, duration, Core Web Vitals (LCP, INP, CLS), and a breakdown of main-thread time by category. Calling `trace-engine-cli ` without `overview` also prints the `# Drill-down commands:` reference footer. ```bash trace-engine-cli trace.json.gz ```