rn-startup-audit 0.1.0 → 0.2.0

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.
Files changed (3) hide show
  1. package/README.md +2 -2
  2. package/cli.mjs +34 -13
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -5,12 +5,12 @@ Find React Native modules that load eagerly at startup, and time Android cold st
5
5
  ```
6
6
  npx rn-startup-audit scan <srcDir> [--json]
7
7
  npx rn-startup-audit check-lazy <projectRoot> <file...> [--import-support]
8
- npx rn-startup-audit coldstart <applicationId> [runs=10]
8
+ npx rn-startup-audit coldstart <applicationId> [runs=10] [--marker <text>]
9
9
  ```
10
10
 
11
11
  - `scan` lists re-exports Metro's inline requires can't make lazy: `export *`, `export * as ns`, `export { default as X }`.
12
12
  - `check-lazy` compiles files with the project's Babel config and then Metro's inline-requires plugin, and lists `require()` calls left at module scope.
13
- - `coldstart` force-stops and launches an installed app over adb, and reports median first native frame and time to React Native's `Running "<app>"` log line.
13
+ - `coldstart` force-stops and launches an installed app over adb, and reports median first native frame, time to React Native's `Running "<app>"` log line and, with `--marker`, to the first log line containing that text.
14
14
 
15
15
  Node 18+, no dependencies. `check-lazy` uses `@babel/core` and `metro-transform-plugins` from your project.
16
16
 
package/cli.mjs CHANGED
@@ -11,8 +11,9 @@ const USAGE = `usage:
11
11
  list re-exports Metro can't make lazy (export *, export * as, export { default })
12
12
  rn-startup-audit check-lazy <projectRoot> <file...> [--import-support]
13
13
  compile like Metro and list require() calls left at module scope
14
- rn-startup-audit coldstart <applicationId> [runs=10]
15
- cold-start an installed Android app over adb; median first frame and JS start
14
+ rn-startup-audit coldstart <applicationId> [runs=10] [--marker <text>]
15
+ cold-start an installed Android app over adb; median first frame, JS start,
16
+ and time to the first log line containing <text> (e.g. one logged by the first screen)
16
17
  rn-startup-audit self-test`;
17
18
 
18
19
  // ---------- scan ----------
@@ -128,20 +129,26 @@ export const median = (xs) => {
128
129
 
129
130
  export const parseTotalTime = (amOutput) => Number(amOutput.match(/TotalTime:\s*(\d+)/)?.[1] ?? NaN);
130
131
 
131
- // ms from the launch intent to RN's `Running "<app>"` log line, from `logcat -v epoch`
132
- export function parseJsStart(logcat, pkg) {
132
+ // ms from the launch intent to the first JS log line containing `needle`, from `logcat -v epoch`
133
+ export function parseLogTime(logcat, pkg, needle) {
133
134
  let start;
134
135
  for (const line of logcat.split(/\r?\n/)) {
135
136
  const t = Number(line.match(/^\s*(\d+\.\d+)\s/)?.[1]);
136
137
  if (!t) continue;
137
138
  if (start === undefined && /START u\d+/.test(line) && line.includes(pkg)) start = t;
138
- else if (start !== undefined && /Running "/.test(line)) return Math.round((t - start) * 1000);
139
+ // system lines like "Displayed <pkg>/..." would otherwise match a marker that overlaps the package name
140
+ else if (start !== undefined && /\sReactNativeJS:/.test(line) && line.includes(needle)) return Math.round((t - start) * 1000);
139
141
  }
140
142
  return NaN;
141
143
  }
142
144
 
143
- function coldstart([pkg, runsArg = '10']) {
144
- if (!pkg) usage();
145
+ function coldstart(args) {
146
+ const markerAt = args.indexOf('--marker');
147
+ const marker = markerAt >= 0 ? args[markerAt + 1] : undefined;
148
+ if (markerAt >= 0 && !marker) usage();
149
+ const [pkg, runsArg = '10'] = args.filter((_, i) => markerAt < 0 || (i !== markerAt && i !== markerAt + 1));
150
+ const runs = Number(runsArg);
151
+ if (!pkg || !Number.isInteger(runs) || runs < 1) usage();
145
152
  const adb = (...a) => execFileSync('adb', a, { encoding: 'utf8' });
146
153
  const sleep = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
147
154
  const activity = adb('shell', 'cmd', 'package', 'resolve-activity', '--brief', pkg).trim().split(/\r?\n/).pop();
@@ -152,24 +159,31 @@ function coldstart([pkg, runsArg = '10']) {
152
159
 
153
160
  const frame = [];
154
161
  const js = [];
155
- for (let i = 1; i <= Number(runsArg); i++) {
162
+ const marked = [];
163
+ const ms = (x) => (Number.isNaN(x) ? 'not seen' : `${x} ms`);
164
+ for (let i = 1; i <= runs; i++) {
156
165
  adb('shell', 'am', 'force-stop', pkg);
157
166
  sleep(1500);
158
167
  adb('logcat', '-c');
159
168
  const total = parseTotalTime(adb('shell', 'am', 'start', '-W', '-n', activity));
160
169
  let jsStart = NaN;
161
- for (let tries = 0; tries < 30 && Number.isNaN(jsStart); tries++) {
170
+ let markerTime = NaN;
171
+ for (let tries = 0; tries < 30 && Number.isNaN(marker ? markerTime : jsStart); tries++) {
162
172
  sleep(500);
163
- jsStart = parseJsStart(adb('logcat', '-d', '-v', 'epoch', '-s', 'ActivityTaskManager:I', 'ActivityManager:I', 'ReactNativeJS:I'), pkg);
173
+ const log = adb('logcat', '-d', '-v', 'epoch', '-s', 'ActivityTaskManager:I', 'ActivityManager:I', 'ReactNativeJS:I');
174
+ jsStart = parseLogTime(log, pkg, 'Running "');
175
+ if (marker) markerTime = parseLogTime(log, pkg, marker);
164
176
  }
165
177
  frame.push(total);
166
178
  if (!Number.isNaN(jsStart)) js.push(jsStart);
167
- console.log(`run ${i}: first frame ${total} ms, JS running ${Number.isNaN(jsStart) ? 'not seen' : `${jsStart} ms`}`);
179
+ if (!Number.isNaN(markerTime)) marked.push(markerTime);
180
+ console.log(`run ${i}: first frame ${total} ms, JS running ${ms(jsStart)}${marker ? `, marker ${ms(markerTime)}` : ''}`);
168
181
  }
169
182
 
170
183
  const summary = (xs) => `median ${median(xs)} ms (min ${Math.min(...xs)}, max ${Math.max(...xs)}, n=${xs.length})`;
171
184
  console.log(`\nfirst native frame: ${summary(frame)}`);
172
185
  console.log(js.length ? `JS "Running": ${summary(js)}` : 'JS "Running" line never appeared (console stripped in release?)');
186
+ if (marker) console.log(marked.length ? `marker: ${summary(marked)}` : `"${marker}" never appeared (console stripped in release?)`);
173
187
  }
174
188
 
175
189
  // ---------- self-test ----------
@@ -199,8 +213,15 @@ function selfTest() {
199
213
  ' 1732700000.100 1 1 I ActivityTaskManager: START u0 {flg=0x10000000 cmp=com.acme/.MainActivity}',
200
214
  ' 1732700001.804 2 2 I ReactNativeJS: Running "Acme"',
201
215
  ].join('\n');
202
- assert.equal(parseJsStart(log, 'com.acme'), 1704);
203
- assert.ok(Number.isNaN(parseJsStart(log, 'com.other')));
216
+ assert.equal(parseLogTime(log, 'com.acme', 'Running "'), 1704);
217
+ assert.ok(Number.isNaN(parseLogTime(log, 'com.other', 'Running "')));
218
+ assert.equal(parseLogTime(`${log}\n 1732700002.350 2 2 I ReactNativeJS: [startup] home ready`, 'com.acme', '[startup] home ready'), 2250);
219
+ const withSystem = [
220
+ ' 1732700000.100 1 1 I ActivityTaskManager: START u0 {cmp=com.acme/.MainActivity}',
221
+ ' 1732700001.000 1 1 I ActivityTaskManager: Displayed com.acme/.MainActivity: +900ms',
222
+ ' 1732700001.600 2 2 I ReactNativeJS: acme ready',
223
+ ].join('\n');
224
+ assert.equal(parseLogTime(withSystem, 'com.acme', 'acme'), 1500);
204
225
  console.log('self-test ok');
205
226
  }
206
227
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rn-startup-audit",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Find React Native modules that load eagerly at startup (barrel re-exports, default imports) and time Android cold starts.",
5
5
  "type": "module",
6
6
  "bin": {