@rn-perf-kit/analyzers 0.0.0-stage → 0.1.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.
- package/package.json +15 -4
- package/src/cascade-analyzer.ts +50 -0
- package/src/frame-attribution-analyzer.ts +60 -0
- package/src/index.ts +8 -0
- package/src/navigation-analyzer.ts +131 -0
- package/src/render-cause-analyzer.ts +78 -0
- package/src/types.ts +22 -0
- package/tsconfig.json +5 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rn-perf-kit/analyzers",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Event analyzers for rn-perf-kit: navigation, render-cause, frame attribution",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"lint": "eslint src/"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"typescript": "^5.3.0"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@rn-perf-kit/core": "^0.1.0"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cascade & Interruption Analyzer
|
|
3
|
+
* Detects cascading updates and discarded renders.
|
|
4
|
+
*/
|
|
5
|
+
import type { PerfEvent } from '@rn-perf-kit/core';
|
|
6
|
+
import type { Finding, AnalyzerContext } from './types';
|
|
7
|
+
|
|
8
|
+
export function analyzeCascades(events: PerfEvent[], ctx: AnalyzerContext): Finding[] {
|
|
9
|
+
const findings: Finding[] = [];
|
|
10
|
+
const commits = events.filter((e) => e.type === 'commit') as Extract<PerfEvent, { type: 'commit' }>[];
|
|
11
|
+
const discarded = events.filter((e) => e.type === 'render-discarded') as Extract<PerfEvent, { type: 'render-discarded' }>[];
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < commits.length - 1; i++) {
|
|
14
|
+
const a = commits[i];
|
|
15
|
+
const b = commits[i + 1];
|
|
16
|
+
if (b.t - a.t < ctx.frameBudgetMs && b.phase === 'nested-update') {
|
|
17
|
+
findings.push({
|
|
18
|
+
id: `cascade:nested-update:${a.t}`,
|
|
19
|
+
severity: 'warning',
|
|
20
|
+
category: 'cascade',
|
|
21
|
+
title: 'Cascading update detected (setState in useLayoutEffect)',
|
|
22
|
+
detail: `Two commits within ${(b.t - a.t).toFixed(1)}ms. Second commit is nested-update.`,
|
|
23
|
+
suggestion: 'Move state update to useEffect or derive value during render instead.',
|
|
24
|
+
t: a.t,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const discardedByComponent = new Map<string, number>();
|
|
30
|
+
for (const ev of discarded) {
|
|
31
|
+
discardedByComponent.set(ev.component, (discardedByComponent.get(ev.component) ?? 0) + ev.renderCount);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const [component, count] of discardedByComponent) {
|
|
35
|
+
if (count >= 3) {
|
|
36
|
+
findings.push({
|
|
37
|
+
id: `cascade:discarded:${component}`,
|
|
38
|
+
severity: 'warning',
|
|
39
|
+
category: 'cascade',
|
|
40
|
+
title: `${component}: ${count} discarded renders`,
|
|
41
|
+
detail: `React started ${count} renders of ${component} that were thrown away without committing.`,
|
|
42
|
+
suggestion: 'If using startTransition, check render cost. If not, check for state thrash.',
|
|
43
|
+
component,
|
|
44
|
+
t: discarded.find((e) => e.component === component)?.t ?? 0,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return findings;
|
|
50
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frame Attribution Analyzer
|
|
3
|
+
* Links dropped frames to JS commits, stalls, GC, or labels them native-side.
|
|
4
|
+
*/
|
|
5
|
+
import type { PerfEvent } from '@rn-perf-kit/core';
|
|
6
|
+
import type { Finding, AnalyzerContext } from './types';
|
|
7
|
+
|
|
8
|
+
export interface FrameAttribution {
|
|
9
|
+
frameT: number;
|
|
10
|
+
frameDur: number;
|
|
11
|
+
cause: 'commit' | 'stall' | 'gc' | 'native-side';
|
|
12
|
+
commitDur?: number;
|
|
13
|
+
stallDur?: number;
|
|
14
|
+
detail: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function analyzeFrameAttribution(
|
|
18
|
+
events: PerfEvent[],
|
|
19
|
+
ctx: AnalyzerContext
|
|
20
|
+
): { attributions: FrameAttribution[]; findings: Finding[] } {
|
|
21
|
+
const jankFrames = events.filter((e) => e.type === 'frame' && e.janky) as Extract<PerfEvent, { type: 'frame' }>[];
|
|
22
|
+
const commits = events.filter((e) => e.type === 'commit') as Extract<PerfEvent, { type: 'commit' }>[];
|
|
23
|
+
const stalls = events.filter((e) => e.type === 'stall') as Extract<PerfEvent, { type: 'stall' }>[];
|
|
24
|
+
const gcs = events.filter((e) => e.type === 'gc') as Extract<PerfEvent, { type: 'gc' }>[];
|
|
25
|
+
|
|
26
|
+
const attributions: FrameAttribution[] = [];
|
|
27
|
+
const findings: Finding[] = [];
|
|
28
|
+
|
|
29
|
+
for (const frame of jankFrames) {
|
|
30
|
+
const fEnd = frame.t + frame.dur;
|
|
31
|
+
const commit = commits.find((c) => c.t <= fEnd && c.t + c.dur >= frame.t);
|
|
32
|
+
const stall = stalls.find((s) => s.t <= fEnd && s.t + s.dur >= frame.t);
|
|
33
|
+
const gc = gcs.find((g) => g.t <= fEnd && g.t + g.dur >= frame.t);
|
|
34
|
+
|
|
35
|
+
let attr: FrameAttribution;
|
|
36
|
+
|
|
37
|
+
if (commit && commit.dur >= (stall?.dur ?? 0)) {
|
|
38
|
+
attr = { frameT: frame.t, frameDur: frame.dur, cause: 'commit', commitDur: commit.dur, detail: `Commit (${commit.dur.toFixed(0)}ms) overlapped janky frame (${frame.dur.toFixed(0)}ms > budget ${ctx.frameBudgetMs}ms)` };
|
|
39
|
+
} else if (stall) {
|
|
40
|
+
attr = { frameT: frame.t, frameDur: frame.dur, cause: 'stall', stallDur: stall.dur, detail: `JS stall (${stall.dur.toFixed(0)}ms) caused janky frame` };
|
|
41
|
+
} else if (gc) {
|
|
42
|
+
attr = { frameT: frame.t, frameDur: frame.dur, cause: 'gc', detail: `GC pause (${gc.dur.toFixed(0)}ms) caused janky frame` };
|
|
43
|
+
} else {
|
|
44
|
+
attr = { frameT: frame.t, frameDur: frame.dur, cause: 'native-side', detail: `Janky frame (${frame.dur.toFixed(0)}ms) with no overlapping JS work — likely native-side (layout, images, overdraw)` };
|
|
45
|
+
findings.push({
|
|
46
|
+
id: `frame:native-side:${frame.t}`,
|
|
47
|
+
severity: 'info',
|
|
48
|
+
category: 'frame',
|
|
49
|
+
title: 'Native-side jank (no JS cause)',
|
|
50
|
+
detail: attr.detail,
|
|
51
|
+
suggestion: 'Profile with Android Studio GPU profiler. Check for overdraw, large images, expensive layouts.',
|
|
52
|
+
t: frame.t,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
attributions.push(attr);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return { attributions, findings };
|
|
60
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { analyzeNavigation } from './navigation-analyzer';
|
|
2
|
+
export { analyzeRenderCauses } from './render-cause-analyzer';
|
|
3
|
+
export { analyzeFrameAttribution } from './frame-attribution-analyzer';
|
|
4
|
+
export { analyzeCascades } from './cascade-analyzer';
|
|
5
|
+
export type { Finding, FindingSeverity, AnalyzerContext } from './types';
|
|
6
|
+
export type { NavigationReport } from './navigation-analyzer';
|
|
7
|
+
export type { RenderCauseReport } from './render-cause-analyzer';
|
|
8
|
+
export type { FrameAttribution } from './frame-attribution-analyzer';
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Navigation Analyzer
|
|
3
|
+
* Correlates nav phase events, commits, frames, stalls into per-navigation reports.
|
|
4
|
+
*/
|
|
5
|
+
import type { PerfEvent } from '@rn-perf-kit/core';
|
|
6
|
+
import type { Finding, AnalyzerContext } from './types';
|
|
7
|
+
|
|
8
|
+
interface NavPhaseTimings {
|
|
9
|
+
navId: string;
|
|
10
|
+
fromRoute: string;
|
|
11
|
+
toRoute: string;
|
|
12
|
+
tapAt?: number;
|
|
13
|
+
dispatchAt?: number;
|
|
14
|
+
firstCommitAt?: number;
|
|
15
|
+
transitionStartAt?: number;
|
|
16
|
+
transitionEndAt?: number;
|
|
17
|
+
contentReadyAt?: number;
|
|
18
|
+
interactiveAt?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface NavigationReport {
|
|
22
|
+
navId: string;
|
|
23
|
+
route: string;
|
|
24
|
+
fromRoute: string;
|
|
25
|
+
phases: Partial<Record<string, number>>;
|
|
26
|
+
durations: {
|
|
27
|
+
tapToDispatch?: number;
|
|
28
|
+
dispatchToFirstCommit?: number;
|
|
29
|
+
transition?: number;
|
|
30
|
+
toContentReady?: number;
|
|
31
|
+
toInteractive?: number;
|
|
32
|
+
};
|
|
33
|
+
droppedFrames: number;
|
|
34
|
+
totalFrames: number;
|
|
35
|
+
droppedPct: number;
|
|
36
|
+
commitsDuringTransition: Array<{ dur: number; source: string }>;
|
|
37
|
+
jsDuringTransition: number;
|
|
38
|
+
findings: Finding[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function analyzeNavigation(events: PerfEvent[], ctx: AnalyzerContext): NavigationReport[] {
|
|
42
|
+
const reports: NavigationReport[] = [];
|
|
43
|
+
const navSessions = new Map<string, NavPhaseTimings>();
|
|
44
|
+
|
|
45
|
+
for (const ev of events) {
|
|
46
|
+
if (ev.type !== 'nav') continue;
|
|
47
|
+
if (!navSessions.has(ev.navId)) {
|
|
48
|
+
navSessions.set(ev.navId, { navId: ev.navId, fromRoute: ev.fromRoute ?? '', toRoute: ev.route });
|
|
49
|
+
}
|
|
50
|
+
const s = navSessions.get(ev.navId)!;
|
|
51
|
+
switch (ev.phase) {
|
|
52
|
+
case 'tap': s.tapAt = ev.t; break;
|
|
53
|
+
case 'dispatch': s.dispatchAt = ev.t; break;
|
|
54
|
+
case 'first-commit': s.firstCommitAt = ev.t; break;
|
|
55
|
+
case 'transition-start': s.transitionStartAt = ev.t; break;
|
|
56
|
+
case 'transition-end': s.transitionEndAt = ev.t; break;
|
|
57
|
+
case 'content-ready': s.contentReadyAt = ev.t; break;
|
|
58
|
+
case 'interactive': s.interactiveAt = ev.t; break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const [_navId, s] of navSessions) {
|
|
63
|
+
const { fromRoute, toRoute, tapAt, dispatchAt, firstCommitAt, transitionStartAt, transitionEndAt, contentReadyAt, interactiveAt } = s;
|
|
64
|
+
const tsStart = transitionStartAt ?? firstCommitAt ?? dispatchAt ?? 0;
|
|
65
|
+
const tsEnd = transitionEndAt ?? contentReadyAt ?? 0;
|
|
66
|
+
|
|
67
|
+
let dropped = 0, total = 0, jsStall = 0;
|
|
68
|
+
const commitsDuring: Array<{ dur: number; source: string }> = [];
|
|
69
|
+
|
|
70
|
+
for (const ev of events) {
|
|
71
|
+
if (!tsStart || !tsEnd) break;
|
|
72
|
+
if (ev.t < tsStart || ev.t > tsEnd) continue;
|
|
73
|
+
if (ev.type === 'frame') { total++; if (ev.janky) dropped++; }
|
|
74
|
+
if (ev.type === 'commit') commitsDuring.push({ dur: ev.dur, source: 'react' });
|
|
75
|
+
if (ev.type === 'stall') jsStall += ev.dur;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const droppedPct = total > 0 ? (dropped / total) * 100 : 0;
|
|
79
|
+
const findings: Finding[] = [];
|
|
80
|
+
|
|
81
|
+
if (droppedPct > (ctx.tier === 'LOW' ? 15 : ctx.tier === 'MID' ? 10 : 5)) {
|
|
82
|
+
findings.push({
|
|
83
|
+
id: `nav:dropped-frames:${s.navId}`,
|
|
84
|
+
severity: 'critical',
|
|
85
|
+
category: 'navigation',
|
|
86
|
+
title: `Dropped ${dropped}/${total} frames during ${fromRoute} → ${toRoute}`,
|
|
87
|
+
detail: `${droppedPct.toFixed(0)}% drop rate. Commit time: ${commitsDuring.reduce((a, c) => a + c.dur, 0).toFixed(0)}ms. JS stall: ${jsStall.toFixed(0)}ms.`,
|
|
88
|
+
suggestion: 'Defer heavy component mounts until after transitionEnd. Use native-stack.',
|
|
89
|
+
route: `${fromRoute} → ${toRoute}`,
|
|
90
|
+
t: tsStart,
|
|
91
|
+
meta: { dropped, total, commitsDuring },
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const heavy = commitsDuring.filter((c) => c.dur > ctx.frameBudgetMs);
|
|
96
|
+
if (heavy.length > 0) {
|
|
97
|
+
findings.push({
|
|
98
|
+
id: `nav:heavy-commit:${s.navId}`,
|
|
99
|
+
severity: 'warning',
|
|
100
|
+
category: 'navigation',
|
|
101
|
+
title: `${heavy.length} commit(s) over frame budget during ${fromRoute} → ${toRoute}`,
|
|
102
|
+
detail: `Commits: ${heavy.map((c) => `${c.dur.toFixed(0)}ms`).join(', ')}. Budget: ${ctx.frameBudgetMs}ms.`,
|
|
103
|
+
suggestion: 'Wrap expensive screen content in React.lazy() or defer with startTransition.',
|
|
104
|
+
route: `${fromRoute} → ${toRoute}`,
|
|
105
|
+
t: tsStart,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
reports.push({
|
|
110
|
+
navId: s.navId,
|
|
111
|
+
route: toRoute,
|
|
112
|
+
fromRoute,
|
|
113
|
+
phases: { tap: tapAt, dispatch: dispatchAt, firstCommit: firstCommitAt, transitionStart: transitionStartAt, transitionEnd: transitionEndAt, contentReady: contentReadyAt, interactive: interactiveAt },
|
|
114
|
+
durations: {
|
|
115
|
+
tapToDispatch: tapAt && dispatchAt ? dispatchAt - tapAt : undefined,
|
|
116
|
+
dispatchToFirstCommit: dispatchAt && firstCommitAt ? firstCommitAt - dispatchAt : undefined,
|
|
117
|
+
transition: tsStart && tsEnd ? tsEnd - tsStart : undefined,
|
|
118
|
+
toContentReady: dispatchAt && contentReadyAt ? contentReadyAt - dispatchAt : undefined,
|
|
119
|
+
toInteractive: dispatchAt && interactiveAt ? interactiveAt - dispatchAt : undefined,
|
|
120
|
+
},
|
|
121
|
+
droppedFrames: dropped,
|
|
122
|
+
totalFrames: total,
|
|
123
|
+
droppedPct,
|
|
124
|
+
commitsDuringTransition: commitsDuring,
|
|
125
|
+
jsDuringTransition: jsStall,
|
|
126
|
+
findings,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return reports;
|
|
131
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render-Cause Analyzer
|
|
3
|
+
* Detects render storms, fan-outs, selector instability.
|
|
4
|
+
*/
|
|
5
|
+
import type { PerfEvent } from '@rn-perf-kit/core';
|
|
6
|
+
import type { Finding, AnalyzerContext } from './types';
|
|
7
|
+
|
|
8
|
+
const STORM_THRESHOLD_PER_SEC = 30;
|
|
9
|
+
const FAN_OUT_THRESHOLD = 15;
|
|
10
|
+
const WINDOW_MS = 2000;
|
|
11
|
+
|
|
12
|
+
export interface RenderCauseReport {
|
|
13
|
+
findings: Finding[];
|
|
14
|
+
stats: { totalCommits: number; totalFibers: number; wastedFibers: number; wastedPct: number };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function analyzeRenderCauses(events: PerfEvent[], _ctx: AnalyzerContext): RenderCauseReport {
|
|
18
|
+
const findings: Finding[] = [];
|
|
19
|
+
const commitEvents = events.filter((e) => e.type === 'commit') as Extract<PerfEvent, { type: 'commit' }>[];
|
|
20
|
+
const storeEvents = events.filter((e) => e.type === 'store') as Extract<PerfEvent, { type: 'store' }>[];
|
|
21
|
+
|
|
22
|
+
let totalFibers = 0, wastedFibers = 0;
|
|
23
|
+
const componentRenders = new Map<string, number[]>();
|
|
24
|
+
|
|
25
|
+
for (const ev of commitEvents) {
|
|
26
|
+
for (const fiber of ev.fibers) {
|
|
27
|
+
totalFibers++;
|
|
28
|
+
if (fiber.wasted) wastedFibers++;
|
|
29
|
+
if (!componentRenders.has(fiber.name)) componentRenders.set(fiber.name, []);
|
|
30
|
+
componentRenders.get(fiber.name)!.push(ev.t);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const [name, times] of componentRenders) {
|
|
35
|
+
for (let i = 0; i < times.length; i++) {
|
|
36
|
+
const windowEnd = times[i] + WINDOW_MS;
|
|
37
|
+
const count = times.filter((t) => t >= times[i] && t <= windowEnd).length;
|
|
38
|
+
if ((count / (WINDOW_MS / 1000)) > STORM_THRESHOLD_PER_SEC) {
|
|
39
|
+
findings.push({
|
|
40
|
+
id: `render:storm:${name}:${times[i]}`,
|
|
41
|
+
severity: 'critical',
|
|
42
|
+
category: 'render',
|
|
43
|
+
title: `Render storm: ${name} rendered ${count}x in ${WINDOW_MS / 1000}s`,
|
|
44
|
+
detail: `${(count / (WINDOW_MS / 1000)).toFixed(0)} renders/second.`,
|
|
45
|
+
suggestion: 'Check for unstable prop references. Wrap with React.memo() and useCallback().',
|
|
46
|
+
component: name,
|
|
47
|
+
t: times[i],
|
|
48
|
+
});
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
for (const storeEv of storeEvents) {
|
|
55
|
+
const windowEnd = storeEv.t + 50;
|
|
56
|
+
const affected = new Set<string>();
|
|
57
|
+
for (const commitEv of commitEvents) {
|
|
58
|
+
if (commitEv.t < storeEv.t || commitEv.t > windowEnd) continue;
|
|
59
|
+
commitEv.fibers.forEach((f) => affected.add(f.name));
|
|
60
|
+
}
|
|
61
|
+
if (affected.size > FAN_OUT_THRESHOLD) {
|
|
62
|
+
findings.push({
|
|
63
|
+
id: `render:fan-out:${storeEv.key}:${storeEv.t}`,
|
|
64
|
+
severity: 'warning',
|
|
65
|
+
category: 'render',
|
|
66
|
+
title: `Fan-out: ${storeEv.key} update caused ${affected.size} component renders`,
|
|
67
|
+
detail: `Store: ${storeEv.source}/${storeEv.key}. Components: ${[...affected].slice(0, 5).join(', ')}${affected.size > 5 ? ` +${affected.size - 5} more` : ''}.`,
|
|
68
|
+
suggestion: 'Split large stores. Use fine-grained selectors. Consider Context splitting.',
|
|
69
|
+
t: storeEv.t,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
findings,
|
|
76
|
+
stats: { totalCommits: commitEvents.length, totalFibers, wastedFibers, wastedPct: totalFibers > 0 ? (wastedFibers / totalFibers) * 100 : 0 },
|
|
77
|
+
};
|
|
78
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { DeviceTier } from '@rn-perf-kit/core';
|
|
2
|
+
|
|
3
|
+
export type FindingSeverity = 'critical' | 'warning' | 'info';
|
|
4
|
+
|
|
5
|
+
export interface Finding {
|
|
6
|
+
id: string;
|
|
7
|
+
severity: FindingSeverity;
|
|
8
|
+
category: 'navigation' | 'render' | 'frame' | 'memory' | 'cascade';
|
|
9
|
+
title: string;
|
|
10
|
+
detail: string;
|
|
11
|
+
suggestion: string;
|
|
12
|
+
route?: string;
|
|
13
|
+
component?: string;
|
|
14
|
+
t: number;
|
|
15
|
+
meta?: Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface AnalyzerContext {
|
|
19
|
+
tier: DeviceTier;
|
|
20
|
+
refreshRateHz: number;
|
|
21
|
+
frameBudgetMs: number;
|
|
22
|
+
}
|
package/tsconfig.json
ADDED
package/README.md
DELETED