@atlaspack/profiler 2.14.35 → 2.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/dist/NativeProfiler.js +205 -0
- package/dist/index.js +3 -1
- package/lib/NativeProfiler.js +235 -0
- package/lib/index.js +7 -0
- package/lib/types/NativeProfiler.d.ts +7 -0
- package/lib/types/index.d.ts +2 -0
- package/package.json +5 -2
- package/src/NativeProfiler.ts +245 -0
- package/src/index.ts +2 -0
- package/tsconfig.json +6 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import {getTimeId} from '@atlaspack/utils';
|
|
2
|
+
import logger from '@atlaspack/logger';
|
|
3
|
+
import readline from 'readline';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import {exec} from 'child_process';
|
|
6
|
+
import {promisify} from 'util';
|
|
7
|
+
|
|
8
|
+
const execAsync = promisify(exec);
|
|
9
|
+
|
|
10
|
+
export type NativeProfilerType = 'instruments' | 'samply';
|
|
11
|
+
|
|
12
|
+
export default class NativeProfiler {
|
|
13
|
+
startProfiling(profilerType: NativeProfilerType): Promise<void> {
|
|
14
|
+
const pid = process.pid;
|
|
15
|
+
const timeId = getTimeId();
|
|
16
|
+
|
|
17
|
+
let filename: string;
|
|
18
|
+
let command: string;
|
|
19
|
+
|
|
20
|
+
logger.info({
|
|
21
|
+
origin: '@atlaspack/profiler',
|
|
22
|
+
message: 'Starting native profiling...',
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (profilerType === 'instruments') {
|
|
26
|
+
filename = `native-profile-${timeId}.trace`;
|
|
27
|
+
command = `xcrun xctrace record --template "CPU Profiler" --output ${filename} --attach ${pid}`;
|
|
28
|
+
} else {
|
|
29
|
+
filename = `native-profile-${timeId}.json`;
|
|
30
|
+
command = `samply record --save-only --output ${filename} --pid ${pid}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Display banner with PID and command
|
|
34
|
+
// Strip ANSI codes for length calculation
|
|
35
|
+
// eslint-disable-next-line no-control-regex
|
|
36
|
+
const stripAnsi = (str: string) => str.replace(/\u001b\[[0-9;]*m/g, '');
|
|
37
|
+
const boxWidth = Math.max(60, stripAnsi(command).length + 6);
|
|
38
|
+
const title = 'Native Profiling';
|
|
39
|
+
const titlePadding = Math.floor((boxWidth - title.length - 2) / 2);
|
|
40
|
+
const isTTY = process.stdin.isTTY;
|
|
41
|
+
const maxWaitTime = 30; // seconds
|
|
42
|
+
|
|
43
|
+
const padLine = (content: string) => {
|
|
44
|
+
const contentLength = stripAnsi(content).length;
|
|
45
|
+
const padding = Math.max(0, boxWidth - contentLength - 2);
|
|
46
|
+
return (
|
|
47
|
+
chalk.blue('│') +
|
|
48
|
+
' ' +
|
|
49
|
+
content +
|
|
50
|
+
' '.repeat(padding) +
|
|
51
|
+
' ' +
|
|
52
|
+
chalk.blue('│')
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Make the command visually distinct and easy to copy
|
|
57
|
+
// Note: Hyperlinks can cause issues with commands (words become separate links)
|
|
58
|
+
// So we just make it visually prominent with colors
|
|
59
|
+
const makeCommandDisplay = (cmd: string) => {
|
|
60
|
+
return chalk.cyan.bold(cmd);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Contextual message based on TTY
|
|
64
|
+
const continueMessage = isTTY
|
|
65
|
+
? 'Press Enter or start the profiler to continue'
|
|
66
|
+
: `Build will continue when profiler has started, or after ${maxWaitTime}s`;
|
|
67
|
+
|
|
68
|
+
const banner = [
|
|
69
|
+
'',
|
|
70
|
+
chalk.blue('┌' + '─'.repeat(boxWidth) + '┐'),
|
|
71
|
+
chalk.blue('│') +
|
|
72
|
+
' '.repeat(titlePadding) +
|
|
73
|
+
chalk.blue.bold(title) +
|
|
74
|
+
' '.repeat(boxWidth - title.length - titlePadding) +
|
|
75
|
+
chalk.blue('│'),
|
|
76
|
+
chalk.blue('├' + '─'.repeat(boxWidth) + '┤'),
|
|
77
|
+
padLine(`${chalk.gray('PID:')} ${chalk.white.bold(String(pid))}`),
|
|
78
|
+
padLine(''),
|
|
79
|
+
padLine(chalk.gray('Command:')),
|
|
80
|
+
padLine(makeCommandDisplay(command)),
|
|
81
|
+
padLine(''),
|
|
82
|
+
padLine(chalk.gray('Run the command above to start profiling.')),
|
|
83
|
+
padLine(chalk.gray(continueMessage)),
|
|
84
|
+
chalk.blue('└' + '─'.repeat(boxWidth) + '┘'),
|
|
85
|
+
'',
|
|
86
|
+
].join('\n');
|
|
87
|
+
|
|
88
|
+
// eslint-disable-next-line no-console
|
|
89
|
+
console.log(banner);
|
|
90
|
+
|
|
91
|
+
// In both interactive and non-interactive mode, detect when profiler is running
|
|
92
|
+
// In interactive mode, also allow user to press Enter to continue
|
|
93
|
+
if (!process.stdin.isTTY) {
|
|
94
|
+
return this.waitForProfiler(profilerType, pid);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Interactive mode: wait for either user to press Enter OR profiler to be detected
|
|
98
|
+
return new Promise<void>((resolve) => {
|
|
99
|
+
let resolved = false;
|
|
100
|
+
const doResolve = () => {
|
|
101
|
+
if (resolved) return;
|
|
102
|
+
resolved = true;
|
|
103
|
+
logger.info({
|
|
104
|
+
origin: '@atlaspack/profiler',
|
|
105
|
+
message: 'Native profiling setup complete',
|
|
106
|
+
});
|
|
107
|
+
resolve();
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const rl = readline.createInterface({
|
|
111
|
+
input: process.stdin,
|
|
112
|
+
output: process.stdout,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// User presses Enter
|
|
116
|
+
rl.on('line', () => {
|
|
117
|
+
rl.close();
|
|
118
|
+
doResolve();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Also poll for profiler in the background
|
|
122
|
+
this.pollForProfiler(profilerType, pid, doResolve);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private waitForProfiler(
|
|
127
|
+
profilerType: NativeProfilerType,
|
|
128
|
+
pid: number,
|
|
129
|
+
): Promise<void> {
|
|
130
|
+
logger.info({
|
|
131
|
+
origin: '@atlaspack/profiler',
|
|
132
|
+
message: 'Non-interactive mode: waiting for profiler to attach...',
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return new Promise<void>((resolve) => {
|
|
136
|
+
this.pollForProfiler(profilerType, pid, () => {
|
|
137
|
+
logger.info({
|
|
138
|
+
origin: '@atlaspack/profiler',
|
|
139
|
+
message: 'Native profiling setup complete',
|
|
140
|
+
});
|
|
141
|
+
resolve();
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private async pollForProfiler(
|
|
147
|
+
profilerType: NativeProfilerType,
|
|
148
|
+
pid: number,
|
|
149
|
+
onDetected: () => void,
|
|
150
|
+
): Promise<void> {
|
|
151
|
+
const maxAttempts = 60; // 60 attempts * 500ms = 30 seconds max
|
|
152
|
+
const pollInterval = 500; // 500ms between checks
|
|
153
|
+
|
|
154
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
155
|
+
const isRunning = await this.checkProfilerRunning(profilerType, pid);
|
|
156
|
+
|
|
157
|
+
if (isRunning) {
|
|
158
|
+
// Instruments takes longer to start up (~5s), samply needs ~1s
|
|
159
|
+
const waitTime = profilerType === 'instruments' ? 5000 : 1000;
|
|
160
|
+
logger.info({
|
|
161
|
+
origin: '@atlaspack/profiler',
|
|
162
|
+
message: `Profiler detected, waiting ${waitTime}ms before continuing...`,
|
|
163
|
+
});
|
|
164
|
+
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
165
|
+
onDetected();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// If we couldn't detect the profiler after 30 seconds, log a warning and continue anyway
|
|
173
|
+
logger.warn({
|
|
174
|
+
origin: '@atlaspack/profiler',
|
|
175
|
+
message:
|
|
176
|
+
'Could not detect profiler after 30 seconds, continuing anyway...',
|
|
177
|
+
});
|
|
178
|
+
onDetected();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private async checkProfilerRunning(
|
|
182
|
+
profilerType: NativeProfilerType,
|
|
183
|
+
pid: number,
|
|
184
|
+
): Promise<boolean> {
|
|
185
|
+
try {
|
|
186
|
+
// Get all processes and filter in JavaScript
|
|
187
|
+
const {stdout} = await execAsync('ps aux');
|
|
188
|
+
const lines = stdout.split('\n').filter((line) => line.trim().length > 0);
|
|
189
|
+
|
|
190
|
+
// Use word boundaries to match the PID as a complete number
|
|
191
|
+
const pidRegex = new RegExp(`\\b${pid}\\b`);
|
|
192
|
+
|
|
193
|
+
// Determine the profiler process name to look for
|
|
194
|
+
const profilerName =
|
|
195
|
+
profilerType === 'instruments' ? 'xctrace' : 'samply';
|
|
196
|
+
|
|
197
|
+
for (const line of lines) {
|
|
198
|
+
const lowerLine = line.toLowerCase();
|
|
199
|
+
|
|
200
|
+
// Skip lines that are part of our own process checking (avoid false positives)
|
|
201
|
+
// Skip lines containing "ps aux" or "grep" to avoid matching our own commands
|
|
202
|
+
if (lowerLine.includes('ps aux') || lowerLine.includes(' grep ')) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Skip our own process (the Atlaspack process itself)
|
|
207
|
+
// The PID column is the second field in ps aux output
|
|
208
|
+
const fields = line.trim().split(/\s+/);
|
|
209
|
+
if (fields.length >= 2 && fields[1] === String(pid)) {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Check if this line contains the profiler name as a command
|
|
214
|
+
const profilerRegex = new RegExp(`\\b${profilerName}\\b`);
|
|
215
|
+
if (!profilerRegex.test(lowerLine)) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Now check if our PID appears in the command arguments (not in the PID column)
|
|
220
|
+
// The PID should appear after the profiler command, typically as --pid <pid> or --attach <pid>
|
|
221
|
+
// We need to check the command portion, which starts around column 11 in ps aux
|
|
222
|
+
// For safety, check if PID appears after the profiler name in the line
|
|
223
|
+
const profilerIndex = lowerLine.indexOf(profilerName);
|
|
224
|
+
if (profilerIndex === -1) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Check if PID appears in the command portion (after the profiler name)
|
|
229
|
+
const commandPortion = line.substring(profilerIndex);
|
|
230
|
+
if (pidRegex.test(commandPortion)) {
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return false;
|
|
236
|
+
} catch (error: any) {
|
|
237
|
+
// If the command fails, log and return false
|
|
238
|
+
logger.warn({
|
|
239
|
+
origin: '@atlaspack/profiler',
|
|
240
|
+
message: `Error checking profiler status: ${error.message}`,
|
|
241
|
+
});
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,3 +2,5 @@ export {default as SamplingProfiler} from './SamplingProfiler';
|
|
|
2
2
|
export {default as Trace} from './Trace';
|
|
3
3
|
export {tracer, PluginTracer} from './Tracer';
|
|
4
4
|
export type {TraceMeasurement, TraceMeasurementData} from './types';
|
|
5
|
+
export {default as NativeProfiler} from './NativeProfiler';
|
|
6
|
+
export type {NativeProfilerType} from './NativeProfiler';
|