From 27334a57b6f3086577f5346587e47305c2539327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20Gro=C3=9F?= Date: Mon, 23 Nov 2026 18:53:23 +0100 Subject: [PATCH] Expose task tree and section limits in report --- README.md | 7 ++- src/cli.ts | 20 +++++- src/engine/extractors/report.ts | 11 ++-- src/formatters/index.ts | 4 +- test/cli.test.ts | 104 ++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4802c86..79d2593 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,8 @@ available sections. ```bash trace-engine-cli report trace.json.gz --sections overview,shifts,network +trace-engine-cli report trace.json.gz --sections overview,tasks,network \ + --thread PID:TID --tasks-tree bottom-up --tasks-top 1000000 --network-top 1000000 -f json ``` ### `trace-engine-cli insights ` @@ -343,7 +345,7 @@ mainThreadBreakdown: warnings[1]: 1 render-blocking resource(s) detected. # Drill-down commands: -# - trace-engine-cli report [--sections ] +# - trace-engine-cli report [--sections ] [--tasks-tree ] [--tasks-top ] [--network-top ] # - trace-engine-cli insights [--category ] [--navigation ] # - trace-engine-cli tasks [--min-duration ] [--tree ] [--url-only] [--top ] # - trace-engine-cli find [--function ] [--url ] [--time-range ] @@ -569,6 +571,9 @@ command's defaults. | `--sections ` | Comma-separated section names. Defaults to `overview,tasks,shifts,network`. Use `report --help` to list the available sections. | | `--time-range ` | Shared time window. Each section interprets it as its standalone command does. | | `--thread ` | Renderer thread for `overview`, `tasks`, and `profile`. | +| `--tasks-tree ` | Show `bottom-up` or `top-down` call rows in `tasks`. Defaults to the long-task list. | +| `--tasks-top ` | Limit the `tasks` section to the top N rows. Defaults to 20. | +| `--network-top ` | Limit the `network` section to the N slowest requests. Defaults to 20. | | `--strip-query` | Remove query strings from URLs in the `network` section. | ```bash diff --git a/src/cli.ts b/src/cli.ts index c9238da..e1e2d48 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -32,7 +32,7 @@ import { import { scanTraces } from './engine/extractors/scan.js'; import { extractScheduling } from './engine/extractors/scheduling.js'; import { extractShifts } from './engine/extractors/shifts.js'; -import { extractTasks } from './engine/extractors/tasks.js'; +import { type ExtractTasksOptions, extractTasks } from './engine/extractors/tasks.js'; import { listRendererThreads } from './engine/helpers.js'; import { loadTraceFromFile } from './engine/loader.js'; import type { ThreadsResult } from './engine/types.js'; @@ -155,6 +155,21 @@ export async function runCli(argv: string[]): Promise { describe: `Comma-separated analyses: ${REPORT_SECTIONS.join(', ')} (default: ${DEFAULT_REPORT_SECTIONS.join(',')})`, }) .option('thread', THREAD_OPTION) + .option('tasks-tree', { + type: 'string', + choices: ['bottom-up', 'top-down'] satisfies NonNullable[], + describe: 'Show the tasks section as an aggregated call tree (default: list)', + }) + .option('tasks-top', { + type: 'number', + coerce: positiveInteger, + describe: 'Limit the tasks section to the top N rows (default: 20)', + }) + .option('network-top', { + type: 'number', + coerce: positiveInteger, + describe: 'Limit the network section to the N slowest requests (default: 20)', + }) .option('strip-query', { type: 'boolean', describe: 'Remove URL query strings from the network section', @@ -171,6 +186,9 @@ export async function runCli(argv: string[]): Promise { sections, timeRange, threadId: args.thread, + tasksTree: args.tasksTree, + tasksTop: args.tasksTop, + networkTop: args.networkTop, stripQuery: args.stripQuery, }); const output = diff --git a/src/engine/extractors/report.ts b/src/engine/extractors/report.ts index 8adaea7..9f3645f 100644 --- a/src/engine/extractors/report.ts +++ b/src/engine/extractors/report.ts @@ -22,7 +22,7 @@ import { extractOverview } from './overview.js'; import { extractProfile } from './profile.js'; import { extractScheduling } from './scheduling.js'; import { extractShifts } from './shifts.js'; -import { extractTasks } from './tasks.js'; +import { type ExtractTasksOptions, extractTasks } from './tasks.js'; export const REPORT_SECTIONS = [ 'overview', @@ -44,6 +44,9 @@ interface ExtractReportOptions { sections: readonly ReportSection[]; timeRange?: TimeRange; threadId?: string; + tasksTree?: ExtractTasksOptions['tree']; + tasksTop?: number; + networkTop?: number; stripQuery?: boolean; } @@ -51,14 +54,14 @@ export function extractReport( loaded: LoadedTrace, options: ExtractReportOptions, ): Array<[ReportSection, unknown]> { - const { timeRange, threadId, stripQuery } = options; + const { timeRange, threadId, tasksTree, tasksTop, networkTop, stripQuery } = options; const extract: Record unknown> = { overview: () => extractOverview(loaded, { timeRange, threadId }), insights: () => extractInsights(loaded, { timeRange }), - tasks: () => extractTasks(loaded, { timeRange, threadId }), + tasks: () => extractTasks(loaded, { timeRange, threadId, tree: tasksTree, top: tasksTop }), profile: () => extractProfile(loaded, { timeRange, threadId }), scheduling: () => extractScheduling(loaded, { timeRange }), - network: () => extractNetwork(loaded, { timeRange, stripQuery }), + network: () => extractNetwork(loaded, { timeRange, stripQuery, slowest: networkTop }), interactions: () => extractInteractions(loaded, { timeRange }), shifts: () => extractShifts(loaded, { timeRange }), navigations: () => extractNavigations(loaded, timeRange), diff --git a/src/formatters/index.ts b/src/formatters/index.ts index 8b7f874..f35530e 100644 --- a/src/formatters/index.ts +++ b/src/formatters/index.ts @@ -37,7 +37,7 @@ export interface FormatOptions { const NEXT_STEPS_HINT_TOON = ` # Drill-down commands: -# - trace-engine-cli report [--sections ] +# - trace-engine-cli report [--sections ] [--tasks-tree ] [--tasks-top ] [--network-top ] # - trace-engine-cli insights [--category ] [--navigation ] # - trace-engine-cli tasks [--min-duration ] [--tree ] [--url-only] [--top ] # - trace-engine-cli find [--function ] [--url ] [--time-range ] @@ -58,7 +58,7 @@ const NEXT_STEPS_HINT_TOON = ` const NEXT_STEPS_HINT_MD = ` --- ### Drill-down Commands -- \`trace-engine-cli report [--sections ]\` +- \`trace-engine-cli report [--sections ] [--tasks-tree ] [--tasks-top ] [--network-top ]\` - \`trace-engine-cli insights [--category ] [--navigation ]\` - \`trace-engine-cli tasks [--min-duration ] [--tree ] [--url-only] [--top ]\` - \`trace-engine-cli find [--function ] [--url ] [--time-range ]\` diff --git a/test/cli.test.ts b/test/cli.test.ts index 142515c..3e88e16 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -104,6 +104,110 @@ describe('CLI Dispatcher', () => { await assert.rejects(() => captureCliOutput(args), /Unknown report section "unknown"/); }); + test('applies report task and network limits like the standalone commands', async () => { + const fixture = path.resolve(import.meta.dirname, 'fixtures/navigation-cwv-trace.json'); + const cli = ['node', 'trace-engine-cli']; + const format = ['-f', 'json']; + const json = JSON.parse( + await captureCliOutput([ + ...cli, + 'report', + fixture, + '--sections', + 'tasks,network', + '--tasks-tree', + 'bottom-up', + '--tasks-top', + '1', + '--network-top', + '1', + ...format, + ]), + ); + const tasks = await captureCliOutput([ + ...cli, + 'tasks', + fixture, + '--tree', + 'bottom-up', + '--top', + '1', + ...format, + ]); + const network = await captureCliOutput([ + ...cli, + 'network', + fixture, + '--slowest', + '1', + ...format, + ]); + assert.deepEqual(json, { tasks: JSON.parse(tasks), network: JSON.parse(network) }); + assert.equal(json.tasks.mode, 'bottom-up'); + assert.equal(json.tasks.tasks.length, 1); + assert.equal(json.network.requests.length, 1); + }); + + test('returns every request when the report network limit exceeds the request count', async () => { + const fixture = path.resolve(import.meta.dirname, 'fixtures/navigation-cwv-trace.json'); + const json = JSON.parse( + await captureCliOutput([ + 'node', + 'trace-engine-cli', + 'report', + fixture, + '--sections', + 'network', + '--network-top', + '1000000', + '-f', + 'json', + ]), + ); + assert.equal(json.network.requests.length, 4); + }); + + test('applies the selected renderer thread to the report tasks tree', async () => { + const fixture = path.resolve(import.meta.dirname, 'fixtures/navigation-cwv-trace.json'); + const cli = ['node', 'trace-engine-cli']; + const options = ['--thread', '100:1', '-f', 'json']; + const report = await captureCliOutput([ + ...cli, + 'report', + fixture, + '--sections', + 'tasks', + '--tasks-tree', + 'bottom-up', + ...options, + ]); + const tasks = await captureCliOutput([ + ...cli, + 'tasks', + fixture, + '--tree', + 'bottom-up', + ...options, + ]); + assert.deepEqual(JSON.parse(report), { tasks: JSON.parse(tasks) }); + }); + + test('rejects invalid report task and network options before parsing the trace', async () => { + const cli = ['node', 'trace-engine-cli', 'report', 'unavailable.json']; + await assert.rejects( + () => captureCliOutput([...cli, '--tasks-top', '0']), + /Result limits must be positive integers/, + ); + await assert.rejects( + () => captureCliOutput([...cli, '--network-top', '1.5']), + /Result limits must be positive integers/, + ); + await assert.rejects( + () => captureCliOutput([...cli, '--tasks-tree', 'sideways']), + /Invalid values:[\s\S]*tasks-tree[\s\S]*sideways/, + ); + }); + test('honors relative time ranges for navigations and insight selection', async () => { const fixture = path.resolve(import.meta.dirname, 'fixtures/navigation-cwv-trace.json'); const args = ['node', 'trace-engine-cli'];