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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file>`
Expand Down Expand Up @@ -343,7 +345,7 @@ mainThreadBreakdown:
warnings[1]: 1 render-blocking resource(s) detected.

# Drill-down commands:
# - trace-engine-cli report <file> [--sections <list>]
# - trace-engine-cli report <file> [--sections <list>] [--tasks-tree <bottom-up|top-down>] [--tasks-top <n>] [--network-top <n>]
# - trace-engine-cli insights <file> [--category <lcp|inp|cls|render-blocking|third-parties>] [--navigation <id>]
# - trace-engine-cli tasks <file> [--min-duration <ms>] [--tree <bottom-up|top-down>] [--url-only] [--top <n>]
# - trace-engine-cli find <file> [--function <name>] [--url <pattern>] [--time-range <start,end>]
Expand Down Expand Up @@ -569,6 +571,9 @@ command's defaults.
| `--sections <list>` | Comma-separated section names. Defaults to `overview,tasks,shifts,network`. Use `report --help` to list the available sections. |
| `--time-range <range>` | Shared time window. Each section interprets it as its standalone command does. |
| `--thread <PID:TID>` | Renderer thread for `overview`, `tasks`, and `profile`. |
| `--tasks-tree <mode>` | Show `bottom-up` or `top-down` call rows in `tasks`. Defaults to the long-task list. |
| `--tasks-top <n>` | Limit the `tasks` section to the top N rows. Defaults to 20. |
| `--network-top <n>` | Limit the `network` section to the N slowest requests. Defaults to 20. |
| `--strip-query` | Remove query strings from URLs in the `network` section. |

```bash
Expand Down
20 changes: 19 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -155,6 +155,21 @@ export async function runCli(argv: string[]): Promise<void> {
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<ExtractTasksOptions['tree']>[],
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',
Expand All @@ -171,6 +186,9 @@ export async function runCli(argv: string[]): Promise<void> {
sections,
timeRange,
threadId: args.thread,
tasksTree: args.tasksTree,
tasksTop: args.tasksTop,
networkTop: args.networkTop,
stripQuery: args.stripQuery,
});
const output =
Expand Down
11 changes: 7 additions & 4 deletions src/engine/extractors/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -44,21 +44,24 @@ interface ExtractReportOptions {
sections: readonly ReportSection[];
timeRange?: TimeRange;
threadId?: string;
tasksTree?: ExtractTasksOptions['tree'];
tasksTop?: number;
networkTop?: number;
stripQuery?: boolean;
}

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<ReportSection, () => 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),
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface FormatOptions {

const NEXT_STEPS_HINT_TOON = `
# Drill-down commands:
# - trace-engine-cli report <file> [--sections <list>]
# - trace-engine-cli report <file> [--sections <list>] [--tasks-tree <bottom-up|top-down>] [--tasks-top <n>] [--network-top <n>]
# - trace-engine-cli insights <file> [--category <lcp|inp|cls|render-blocking|third-parties>] [--navigation <id>]
# - trace-engine-cli tasks <file> [--min-duration <ms>] [--tree <bottom-up|top-down>] [--url-only] [--top <n>]
# - trace-engine-cli find <file> [--function <name>] [--url <pattern>] [--time-range <start,end>]
Expand All @@ -58,7 +58,7 @@ const NEXT_STEPS_HINT_TOON = `
const NEXT_STEPS_HINT_MD = `
---
### Drill-down Commands
- \`trace-engine-cli report <file> [--sections <list>]\`
- \`trace-engine-cli report <file> [--sections <list>] [--tasks-tree <bottom-up|top-down>] [--tasks-top <n>] [--network-top <n>]\`
- \`trace-engine-cli insights <file> [--category <lcp|inp|cls|render-blocking|third-parties>] [--navigation <id>]\`
- \`trace-engine-cli tasks <file> [--min-duration <ms>] [--tree <bottom-up|top-down>] [--url-only] [--top <n>]\`
- \`trace-engine-cli find <file> [--function <name>] [--url <pattern>] [--time-range <start,end>]\`
Expand Down
104 changes: 104 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down