forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
202 lines (187 loc) · 6.69 KB
/
Copy pathindex.ts
File metadata and controls
202 lines (187 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* CLI entry point for profiler-cli (Profiler CLI).
*
* Usage:
* profiler-cli load <PATH> [--session <id>] Start a new daemon and load a profile
* profiler-cli profile info [--session <id>] Print profile summary
* profiler-cli thread info [--thread <handle>] Print thread information
* profiler-cli thread samples [--thread <handle>] Show thread call tree and top functions
* profiler-cli stop [<id>] [--all] Stop the daemon
* profiler-cli session list List all running sessions
* profiler-cli session use <id> Switch the current session
*
* Build:
* yarn build-profiler-cli
*
* Run:
* profiler-cli <command> (if profiler-cli is in PATH)
* ./profiler-cli/dist/profiler-cli.js <command> (direct invocation)
*/
import * as path from 'path';
import * as os from 'os';
import { Command } from 'commander';
import guideText from '../guide.txt';
import schemasText from '../schemas.txt';
import { startDaemon } from './daemon';
import { startNewDaemon, stopDaemon, sendCommand } from './client';
import { listSessions } from './session';
import { formatOutput } from './output';
import { addGlobalOptions } from './commands/shared';
import { VERSION } from './constants';
import { registerProfileCommand } from './commands/profile';
import { registerThreadCommand } from './commands/thread';
import { registerMarkerCommand } from './commands/marker';
import { registerFunctionCommand } from './commands/function';
import { registerCounterCommand } from './commands/counter';
import { registerZoomCommand } from './commands/zoom';
import { registerFilterCommand } from './commands/filter';
import { registerSessionCommand } from './commands/session';
// Read session directory from environment (only place this is read)
const SESSION_DIR =
process.env.PROFILER_CLI_SESSION_DIR ||
path.join(os.homedir(), '.profiler-cli');
async function main(): Promise<void> {
const rawArgs = process.argv.slice(2);
// Daemon escape hatch: spawned internally by startNewDaemon(), never shown in --help
if (rawArgs.includes('--daemon')) {
const daemonIdx = rawArgs.indexOf('--daemon');
const profilePath = rawArgs.find(
(a, i) => i > daemonIdx && !a.startsWith('-')
);
const sessionIdx = rawArgs.indexOf('--session');
const sessionId = sessionIdx !== -1 ? rawArgs[sessionIdx + 1] : undefined;
const symbolServerIdx = rawArgs.indexOf('--symbol-server');
const symbolServerUrl =
symbolServerIdx !== -1 ? rawArgs[symbolServerIdx + 1] : undefined;
if (!profilePath) {
console.error('Error: Profile path required for daemon mode');
process.exit(1);
}
await startDaemon(SESSION_DIR, profilePath, sessionId, symbolServerUrl);
return;
}
const program = new Command();
program
.name('profiler-cli')
.description('Profiler CLI — query Firefox profiles from the terminal')
.version(VERSION, '-V, --version', 'Print the version number')
.helpOption('-h, --help', 'Show help')
.addHelpCommand('help [command]', 'Show help for a command')
.addHelpText(
'after',
`
Examples:
profiler-cli load profile.json.gz
profiler-cli profile info
profiler-cli thread info
profiler-cli thread samples
profiler-cli thread functions --search GC --min-self 1
profiler-cli thread markers --search DOMEvent --category Graphics
profiler-cli counter list
profiler-cli counter info c-0
profiler-cli zoom push 2.7,3.1
profiler-cli filter push --excludes-function f-184
profiler-cli status
profiler-cli stop --all`
);
// Unknown commands
program.on('command:*', (operands: string[]) => {
console.error(`Error: Unknown command '${operands[0]}'\n`);
program.outputHelp();
process.exit(1);
});
// profiler-cli load <path>
addGlobalOptions(
program
.command('load <path>')
.description('Load a profile and start a daemon session')
.option(
'--symbol-server <url>',
'Symbol server URL for symbolication (overrides URL param and default Mozilla server)'
)
).action(async (profilePath: string, opts) => {
console.log(`Loading profile from ${profilePath}...`);
const sessionId = await startNewDaemon(
SESSION_DIR,
profilePath,
opts.session,
opts.symbolServer
);
console.log(`Session started: ${sessionId}`);
const status = await sendCommand(
SESSION_DIR,
{ command: 'status' },
sessionId
);
console.log(formatOutput(status, opts.json ?? false));
});
// profiler-cli status
addGlobalOptions(
program
.command('status')
.description(
'Show session status (selected thread, zoom ranges, filters)'
)
).action(async (opts) => {
const result = await sendCommand(
SESSION_DIR,
{ command: 'status' },
opts.session
);
console.log(formatOutput(result, opts.json ?? false));
});
// profiler-cli stop [id]
addGlobalOptions(
program
.command('stop [id]')
.description(
'Stop the current session, a specific session, or all with --all'
)
.option('--all', 'Stop all running sessions')
).action(async (idArg: string | undefined, opts) => {
if (opts.all) {
const sessionIds = listSessions(SESSION_DIR);
await Promise.all(
sessionIds.map((id: string) => stopDaemon(SESSION_DIR, id))
);
} else {
const sessionId = idArg ?? opts.session;
await stopDaemon(SESSION_DIR, sessionId);
}
});
// profiler-cli guide
program
.command('guide')
.description('Show detailed usage guide (commands, patterns, tips)')
.action(() => {
console.log(guideText);
});
// profiler-cli schemas
program
.command('schemas')
.description('Show JSON output schemas for all commands')
.action(() => {
console.log(schemasText);
});
registerProfileCommand(program, SESSION_DIR);
registerThreadCommand(program, SESSION_DIR);
registerMarkerCommand(program, SESSION_DIR);
registerFunctionCommand(program, SESSION_DIR);
registerCounterCommand(program, SESSION_DIR);
registerZoomCommand(program, SESSION_DIR);
registerFilterCommand(program, SESSION_DIR);
registerSessionCommand(program, SESSION_DIR);
try {
await program.parseAsync(process.argv);
} catch (error) {
console.error(`Error: ${error instanceof Error ? error.message : error}`);
process.exit(1);
}
}
main().catch((error) => {
console.error(`Fatal error: ${error}`);
process.exit(1);
});