react-native-inapp-inspector 2.2.2 → 2.2.4
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/dist/commonjs/components/AnalyticsDetail.js +5 -1
- package/dist/commonjs/components/AnalyticsEventCard.js +2 -1
- package/dist/commonjs/components/DiffViewer.js +1 -1
- package/dist/commonjs/components/Inspector/BundleTab.js +22 -0
- package/dist/commonjs/components/Inspector/DeviceInfoTab.d.ts +3 -0
- package/dist/commonjs/components/Inspector/DeviceInfoTab.js +835 -0
- package/dist/commonjs/components/Inspector/InspectorHeader.js +12 -14
- package/dist/commonjs/components/Inspector/LogDetail.js +6 -3
- package/dist/commonjs/components/Inspector/MainScreen.js +7 -3
- package/dist/commonjs/components/Inspector/NetworkTab.js +92 -15
- package/dist/commonjs/components/Inspector/PerformanceTab.js +38 -12
- package/dist/commonjs/components/Inspector/SettingsPanel.js +440 -229
- package/dist/commonjs/components/Inspector/StorageTab.d.ts +3 -0
- package/dist/commonjs/components/Inspector/StorageTab.js +892 -0
- package/dist/commonjs/components/Inspector/TabBar.js +14 -0
- package/dist/commonjs/components/Inspector/TelemetryConsentModal.js +11 -2
- package/dist/commonjs/components/Inspector/UpdateAvailableModal.js +4 -1
- package/dist/commonjs/components/NetworkIcons.d.ts +6 -0
- package/dist/commonjs/components/NetworkIcons.js +30 -1
- package/dist/commonjs/constants/version.d.ts +1 -1
- package/dist/commonjs/constants/version.js +1 -1
- package/dist/commonjs/customHooks/bundleAnalyzer.js +44 -22
- package/dist/commonjs/customHooks/storageInspector.d.ts +67 -0
- package/dist/commonjs/customHooks/storageInspector.js +486 -0
- package/dist/commonjs/i18n/locales/en.json +8 -2
- package/dist/commonjs/index.d.ts +1 -0
- package/dist/commonjs/index.js +16 -1
- package/dist/commonjs/storage.d.ts +5 -0
- package/dist/commonjs/storage.js +18 -0
- package/dist/commonjs/types/enums.d.ts +5 -0
- package/dist/commonjs/types/enums.js +5 -0
- package/dist/esm/components/AnalyticsDetail.js +6 -2
- package/dist/esm/components/AnalyticsEventCard.js +2 -1
- package/dist/esm/components/DiffViewer.js +1 -1
- package/dist/esm/components/Inspector/BundleTab.js +22 -0
- package/dist/esm/components/Inspector/DeviceInfoTab.d.ts +3 -0
- package/dist/esm/components/Inspector/DeviceInfoTab.js +796 -0
- package/dist/esm/components/Inspector/InspectorHeader.js +13 -15
- package/dist/esm/components/Inspector/LogDetail.js +7 -4
- package/dist/esm/components/Inspector/MainScreen.js +7 -3
- package/dist/esm/components/Inspector/NetworkTab.js +93 -16
- package/dist/esm/components/Inspector/PerformanceTab.js +39 -13
- package/dist/esm/components/Inspector/SettingsPanel.js +441 -230
- package/dist/esm/components/Inspector/StorageTab.d.ts +3 -0
- package/dist/esm/components/Inspector/StorageTab.js +853 -0
- package/dist/esm/components/Inspector/TabBar.js +15 -1
- package/dist/esm/components/Inspector/TelemetryConsentModal.js +11 -2
- package/dist/esm/components/Inspector/UpdateAvailableModal.js +4 -1
- package/dist/esm/components/NetworkIcons.d.ts +6 -0
- package/dist/esm/components/NetworkIcons.js +23 -0
- package/dist/esm/constants/version.d.ts +1 -1
- package/dist/esm/constants/version.js +1 -1
- package/dist/esm/customHooks/bundleAnalyzer.js +44 -22
- package/dist/esm/customHooks/storageInspector.d.ts +67 -0
- package/dist/esm/customHooks/storageInspector.js +469 -0
- package/dist/esm/i18n/locales/en.json +8 -2
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/storage.d.ts +5 -0
- package/dist/esm/storage.js +5 -0
- package/dist/esm/types/enums.d.ts +5 -0
- package/dist/esm/types/enums.js +5 -0
- package/package.json +9 -1
- package/src/analytics.ts +35 -0
- package/src/bundle.ts +25 -0
- package/src/components/AnalyticsDetail.tsx +870 -0
- package/src/components/AnalyticsEventCard.tsx +442 -0
- package/src/components/AnalyticsGraph.tsx +583 -0
- package/src/components/AnimatedEntrance.tsx +64 -0
- package/src/components/AppHeaderLogo.tsx +109 -0
- package/src/components/BrandCircleIcon.tsx +144 -0
- package/src/components/BrandSquareIcon.tsx +144 -0
- package/src/components/CodeSnippet.tsx +725 -0
- package/src/components/ConsoleLogCard.tsx +628 -0
- package/src/components/CopyButton.tsx +87 -0
- package/src/components/DiffViewer.tsx +82 -0
- package/src/components/DomainHeader.tsx +237 -0
- package/src/components/EmptyState.tsx +73 -0
- package/src/components/EndOfListFooter.tsx +100 -0
- package/src/components/ErrorBoundary.tsx +688 -0
- package/src/components/HeadersSection.tsx +192 -0
- package/src/components/HighlightText.tsx +100 -0
- package/src/components/Inspector/AnalyticsFilterModal.tsx +1250 -0
- package/src/components/Inspector/AnalyticsTab.tsx +1336 -0
- package/src/components/Inspector/BundleTab.tsx +4760 -0
- package/src/components/Inspector/ConsoleTab.tsx +702 -0
- package/src/components/Inspector/CrashDetail.tsx +941 -0
- package/src/components/Inspector/CrashFilterModal.tsx +721 -0
- package/src/components/Inspector/CrashTab.tsx +871 -0
- package/src/components/Inspector/DeviceInfoTab.tsx +1204 -0
- package/src/components/Inspector/FabLauncher.tsx +80 -0
- package/src/components/Inspector/InspectorContext.tsx +28 -0
- package/src/components/Inspector/InspectorHeader.tsx +972 -0
- package/src/components/Inspector/LogDetail.tsx +1431 -0
- package/src/components/Inspector/MainScreen.tsx +262 -0
- package/src/components/Inspector/NavigationTracker.tsx +13 -0
- package/src/components/Inspector/NetworkDetail.tsx +794 -0
- package/src/components/Inspector/NetworkTab.tsx +1183 -0
- package/src/components/Inspector/NpmUpdateToast.tsx +392 -0
- package/src/components/Inspector/PerformanceTab.tsx +1928 -0
- package/src/components/Inspector/ReduxDetail.tsx +1651 -0
- package/src/components/Inspector/ReduxTab.tsx +954 -0
- package/src/components/Inspector/SettingsPanel.tsx +3447 -0
- package/src/components/Inspector/StorageTab.tsx +1048 -0
- package/src/components/Inspector/TabBar.tsx +207 -0
- package/src/components/Inspector/TelemetryConsentModal.tsx +416 -0
- package/src/components/Inspector/UpdateAvailableModal.tsx +557 -0
- package/src/components/JsonViewer.tsx +486 -0
- package/src/components/LogCard.tsx +491 -0
- package/src/components/LogSyntaxHighlighter.tsx +178 -0
- package/src/components/MetaAccordion.tsx +340 -0
- package/src/components/MiniBarChart.tsx +42 -0
- package/src/components/MiniLineChart.tsx +33 -0
- package/src/components/NetworkIcons.tsx +2221 -0
- package/src/components/SectionHeader.tsx +113 -0
- package/src/components/SegmentedTabs.tsx +83 -0
- package/src/components/Slider.tsx +300 -0
- package/src/components/SourcePageCard.tsx +148 -0
- package/src/components/Toast.tsx +131 -0
- package/src/components/TouchableScale.tsx +91 -0
- package/src/components/TreeNode.tsx +186 -0
- package/src/console.ts +24 -0
- package/src/constants/index.ts +38 -0
- package/src/constants/version.ts +3 -0
- package/src/crash.ts +32 -0
- package/src/customHooks/analyticsLogger.ts +336 -0
- package/src/customHooks/bundleAnalyzer.ts +1256 -0
- package/src/customHooks/consoleLogger.ts +497 -0
- package/src/customHooks/crashHandler.ts +944 -0
- package/src/customHooks/logFilters.ts +32 -0
- package/src/customHooks/networkLogger.ts +419 -0
- package/src/customHooks/performanceTracker.ts +1014 -0
- package/src/customHooks/reduxLogger.ts +406 -0
- package/src/customHooks/storageInspector.ts +509 -0
- package/src/customHooks/useAccordion.tsx +60 -0
- package/src/decorators/index.ts +184 -0
- package/src/helpers/gaAnalyticsRegistry.ts +204 -0
- package/src/helpers/index.ts +860 -0
- package/src/helpers/memoryManager.ts +138 -0
- package/src/helpers/searchQueryParser.ts +283 -0
- package/src/helpers/settingsStore.ts +171 -0
- package/src/helpers/telemetry.ts +505 -0
- package/src/helpers/toast.ts +17 -0
- package/src/i18n/index.ts +69 -0
- package/src/i18n/locales/en.json +1058 -0
- package/src/index.tsx +2356 -0
- package/src/native/NativeInspector.ts +397 -0
- package/src/native/NativeNetworkInspector.ts +24 -0
- package/src/network.ts +22 -0
- package/src/performance.ts +38 -0
- package/src/redux.ts +23 -0
- package/src/storage.ts +18 -0
- package/src/styles/AppColors.ts +408 -0
- package/src/styles/AppFonts.ts +8 -0
- package/src/styles/common.ts +209 -0
- package/src/styles/index.ts +1570 -0
- package/src/types/enums.ts +199 -0
- package/src/types/index.ts +34 -0
- package/src/types/interfaces.ts +515 -0
|
@@ -0,0 +1,1014 @@
|
|
|
1
|
+
import {AppColors} from '../styles/AppColors';
|
|
2
|
+
import {t} from '../i18n';
|
|
3
|
+
import {useState, useEffect, useRef, useMemo, useCallback} from 'react';
|
|
4
|
+
|
|
5
|
+
export interface PerformanceEvent {
|
|
6
|
+
id: string;
|
|
7
|
+
timestamp: number;
|
|
8
|
+
type:
|
|
9
|
+
| 'fps_drop'
|
|
10
|
+
| 'slow_render'
|
|
11
|
+
| 'transition'
|
|
12
|
+
| 'memory'
|
|
13
|
+
| 'network'
|
|
14
|
+
| 'bridge'
|
|
15
|
+
| 'touch';
|
|
16
|
+
category: 'render' | 'navigation' | 'memory' | 'io' | 'bridge';
|
|
17
|
+
fps: number;
|
|
18
|
+
durationMs: number;
|
|
19
|
+
droppedFrames?: number;
|
|
20
|
+
frameTimeMs?: number;
|
|
21
|
+
frameBudgetPct?: number;
|
|
22
|
+
bottleneckThread?: 'JS Thread' | 'UI Thread' | 'Bridge' | 'Balanced';
|
|
23
|
+
screenName?: string;
|
|
24
|
+
label: string;
|
|
25
|
+
detail: string;
|
|
26
|
+
source?: string;
|
|
27
|
+
breakdown?: {
|
|
28
|
+
jsTimeMs: number;
|
|
29
|
+
uiTimeMs: number;
|
|
30
|
+
bridgeLatencyMs?: number;
|
|
31
|
+
};
|
|
32
|
+
heapDeltaKb?: number;
|
|
33
|
+
advice?: string;
|
|
34
|
+
severity: 'optimal' | 'warning' | 'critical';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface LiveMemoryStats {
|
|
38
|
+
heapUsedMb: number;
|
|
39
|
+
heapTotalMb: number;
|
|
40
|
+
gcCount: number;
|
|
41
|
+
gcPauseMs: number;
|
|
42
|
+
allocationRateMbPerSec: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface CoreMobileVitals {
|
|
46
|
+
ttiMs: number; // Time to Interactive
|
|
47
|
+
fcpMs: number; // First Contentful Paint
|
|
48
|
+
inpMs: number; // Interaction to Next Paint
|
|
49
|
+
jankPercentage: number;
|
|
50
|
+
grade: 'Optimal' | 'Fair' | 'Poor';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface PerformanceFixKey {
|
|
54
|
+
keyName: string;
|
|
55
|
+
title: string;
|
|
56
|
+
explanation: string;
|
|
57
|
+
codeSnippet: string;
|
|
58
|
+
impact: 'High Impact' | 'Medium Impact' | 'Best Practice';
|
|
59
|
+
impactColor: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface ComponentRenderProfile {
|
|
63
|
+
id: string;
|
|
64
|
+
name: string;
|
|
65
|
+
type: 'screen' | 'component' | 'list_item' | 'modal';
|
|
66
|
+
sourceFile: string;
|
|
67
|
+
renderCount: number;
|
|
68
|
+
wastefulCount: number;
|
|
69
|
+
wastefulPercentage: number;
|
|
70
|
+
avgRenderTimeMs: number;
|
|
71
|
+
totalRenderTimeMs: number;
|
|
72
|
+
lastRenderedAt: number;
|
|
73
|
+
reasons: string[];
|
|
74
|
+
fixKeys: PerformanceFixKey[];
|
|
75
|
+
severity: 'optimal' | 'warning' | 'critical';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Dynamically generates tailored Before/After performance optimization code snippets
|
|
80
|
+
* based on the target component's real identifier, props, and detected anti-pattern.
|
|
81
|
+
*/
|
|
82
|
+
export const generateFixSnippet = (
|
|
83
|
+
fixKey: string,
|
|
84
|
+
componentName: string = 'Component',
|
|
85
|
+
context?: {
|
|
86
|
+
propName?: string;
|
|
87
|
+
actionName?: string;
|
|
88
|
+
itemHeight?: number;
|
|
89
|
+
},
|
|
90
|
+
): PerformanceFixKey => {
|
|
91
|
+
const comp = componentName || 'Component';
|
|
92
|
+
const prop = context?.propName || 'onPress';
|
|
93
|
+
const action = context?.actionName || 'handlePress';
|
|
94
|
+
const itemHeight = context?.itemHeight || 72;
|
|
95
|
+
|
|
96
|
+
switch (fixKey) {
|
|
97
|
+
case 'useCallback':
|
|
98
|
+
return {
|
|
99
|
+
keyName: 'useCallback',
|
|
100
|
+
title: t('performance.fixUseCallbackTitle'),
|
|
101
|
+
explanation: t('performance.fixUseCallbackDesc'),
|
|
102
|
+
codeSnippet: `${t('performance.beforeUseCallback', {comp})}\n<Button ${prop}={() => ${action}(item.id)} />\n\n${t('performance.afterUseCallback')}\nconst ${action}Memo = useCallback(() => {\n ${action}(item.id);\n}, [item.id]);\n<Button ${prop}={${action}Memo} />`,
|
|
103
|
+
impact: t('performance.highImpact') as any,
|
|
104
|
+
impactColor: AppColors.pink500,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
case 'createSelector':
|
|
108
|
+
case 'useMemo':
|
|
109
|
+
return {
|
|
110
|
+
keyName: 'createSelector',
|
|
111
|
+
title: t('performance.fixCreateSelectorTitle'),
|
|
112
|
+
explanation: t('performance.fixCreateSelectorDesc'),
|
|
113
|
+
codeSnippet: `${t('performance.beforeCreateSelector', {comp})}\nconst { items, total } = useSelector(state => ({\n items: state.cart.items,\n total: state.cart.total,\n}));\n\n${t('performance.afterCreateSelector')}\nimport { shallowEqual } from 'react-redux';\nconst { items, total } = useSelector(\n state => ({ items: state.cart.items, total: state.cart.total }),\n shallowEqual\n);`,
|
|
114
|
+
impact: t('performance.highImpact') as any,
|
|
115
|
+
impactColor: AppColors.pink500,
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
case 'memo':
|
|
119
|
+
case 'React.memo':
|
|
120
|
+
return {
|
|
121
|
+
keyName: 'memo',
|
|
122
|
+
title: t('performance.fixReactMemoTitle'),
|
|
123
|
+
explanation: t('performance.fixReactMemoDesc'),
|
|
124
|
+
codeSnippet: `${t('performance.beforeMemo', {comp})}\nexport const ${comp} = (props: ${comp}Props) => {\n return <View>...</View>;\n};\n\n${t('performance.afterMemo')}\nexport const ${comp} = React.memo((props: ${comp}Props) => {\n return <View>...</View>;\n}, (prev, next) => prev.id === next.id && prev.updatedAt === next.updatedAt);`,
|
|
125
|
+
impact: t('performance.highImpact') as any,
|
|
126
|
+
impactColor: AppColors.pink500,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
case 'getItemLayout':
|
|
130
|
+
return {
|
|
131
|
+
keyName: 'getItemLayout',
|
|
132
|
+
title: t('performance.fixGetItemLayoutTitle'),
|
|
133
|
+
explanation: t('performance.fixGetItemLayoutDesc'),
|
|
134
|
+
codeSnippet: `${t('performance.flatListOptimization', {comp})}\nconst ITEM_HEIGHT = ${itemHeight};\nconst getItemLayout = useCallback((_data: any, index: number) => ({\n length: ITEM_HEIGHT,\n offset: ITEM_HEIGHT * index,\n index,\n}), []);\n\n<FlatList\n data={items}\n getItemLayout={getItemLayout}\n renderItem={({item}) => <${comp} item={item} />}\n keyExtractor={item => String(item.id)}\n/>`,
|
|
135
|
+
impact: t('performance.highImpact') as any,
|
|
136
|
+
impactColor: AppColors.pink500,
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
case 'customComparator':
|
|
140
|
+
return {
|
|
141
|
+
keyName: 'customComparator',
|
|
142
|
+
title: t('performance.fixCustomComparatorTitle'),
|
|
143
|
+
explanation: t('performance.fixCustomComparatorDesc'),
|
|
144
|
+
codeSnippet: `${t('performance.customComparator', {comp})}\nexport const ${comp} = React.memo(\n ${comp}Component,\n (prevProps, nextProps) => {\n return prevProps.id === nextProps.id && prevProps.status === nextProps.status;\n }\n);`,
|
|
145
|
+
impact: t('performance.mediumImpact') as any,
|
|
146
|
+
impactColor: AppColors.amber500,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
case 'DebouncedInput':
|
|
150
|
+
return {
|
|
151
|
+
keyName: 'DebouncedInput',
|
|
152
|
+
title: t('performance.fixDebouncedInputTitle'),
|
|
153
|
+
explanation: t('performance.fixDebouncedInputDesc'),
|
|
154
|
+
codeSnippet: `${t('performance.debounceInput', {comp})}\nconst [localText, setLocalText] = useState('');\nconst debouncedSearch = useMemo(\n () => debounce((query: string) => onSearch(query), 250),\n [onSearch]\n);`,
|
|
155
|
+
impact: t('performance.highImpact') as any,
|
|
156
|
+
impactColor: AppColors.pink500,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
case 'ComponentSplitting':
|
|
160
|
+
return {
|
|
161
|
+
keyName: 'ComponentSplitting',
|
|
162
|
+
title: t('performance.fixComponentSplittingTitle'),
|
|
163
|
+
explanation: t('performance.fixComponentSplittingDesc'),
|
|
164
|
+
codeSnippet: `${t('performance.isolateState', {comp})}\nexport const StickyKeyboard = React.memo(KeyboardStickyView);`,
|
|
165
|
+
impact: t('performance.mediumImpact') as any,
|
|
166
|
+
impactColor: AppColors.purple500,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
case 'InteractionManager':
|
|
170
|
+
return {
|
|
171
|
+
keyName: 'InteractionManager',
|
|
172
|
+
title: t('performance.fixInteractionManagerTitle'),
|
|
173
|
+
explanation: t('performance.fixInteractionManagerDesc'),
|
|
174
|
+
codeSnippet: `${t('performance.beforeInteractionManager', {comp})}\n\n${t('performance.afterInteractionManager')}`,
|
|
175
|
+
impact: t('performance.highImpact') as any,
|
|
176
|
+
impactColor: AppColors.pink500,
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
case 'FlatListWindowing':
|
|
180
|
+
return {
|
|
181
|
+
keyName: 'FlatListWindowing',
|
|
182
|
+
title: t('performance.fixFlatListWindowingTitle'),
|
|
183
|
+
explanation: t('performance.fixFlatListWindowingDesc'),
|
|
184
|
+
codeSnippet: `${t('performance.beforeFlatListWindowing', {comp})}`,
|
|
185
|
+
impact: t('performance.mediumImpact') as any,
|
|
186
|
+
impactColor: AppColors.purple500,
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
case 'ImageCaching':
|
|
190
|
+
return {
|
|
191
|
+
keyName: 'ImageCaching',
|
|
192
|
+
title: t('performance.fixImageCachingTitle'),
|
|
193
|
+
explanation: t('performance.fixImageCachingDesc'),
|
|
194
|
+
codeSnippet: `${t('performance.beforeImageCaching', {comp})}`,
|
|
195
|
+
impact: t('performance.mediumImpact') as any,
|
|
196
|
+
impactColor: AppColors.purple500,
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
case 'ContextSplitting':
|
|
200
|
+
return {
|
|
201
|
+
keyName: 'ContextSplitting',
|
|
202
|
+
title: t('performance.fixContextSplittingTitle'),
|
|
203
|
+
explanation: t('performance.fixContextSplittingDesc'),
|
|
204
|
+
codeSnippet: `${t('performance.beforeContextSplitting', {comp})}\n\n${t('performance.afterContextSplitting')}`,
|
|
205
|
+
impact: t('performance.highImpact') as any,
|
|
206
|
+
impactColor: AppColors.pink500,
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
case 'InlineStylesHoist':
|
|
210
|
+
return {
|
|
211
|
+
keyName: 'InlineStylesHoist',
|
|
212
|
+
title: t('performance.fixInlineStylesHoistTitle'),
|
|
213
|
+
explanation: t('performance.fixInlineStylesHoistDesc'),
|
|
214
|
+
codeSnippet: `${t('performance.beforeInlineStyles', {comp})}\n\n${t('performance.afterInlineStyles')}`,
|
|
215
|
+
impact: t('performance.bestPractice') as any,
|
|
216
|
+
impactColor: AppColors.pink500,
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
case 'useRef':
|
|
220
|
+
case 'useRefForTracking':
|
|
221
|
+
default:
|
|
222
|
+
return {
|
|
223
|
+
keyName: 'useRefForTracking',
|
|
224
|
+
title: t('performance.fixUseRefForTrackingTitle'),
|
|
225
|
+
explanation: t('performance.fixUseRefForTrackingDesc'),
|
|
226
|
+
codeSnippet: `${t('performance.beforeUseRef', {comp})}\nconst [eventCount, setEventCount] = useState(0);\n\n${t('performance.afterUseRef')}\nconst eventCountRef = useRef(0);`,
|
|
227
|
+
impact: t('performance.bestPractice') as any,
|
|
228
|
+
impactColor: AppColors.pink500,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Dynamically resolves caller file path or runtime source from the active JavaScript stack trace.
|
|
235
|
+
* Does not assume fixed directory hierarchies (such as src/ or components/) and extracts
|
|
236
|
+
* whatever relative path or symbol the host project structure uses.
|
|
237
|
+
*/
|
|
238
|
+
export function getCallerSourceFile(fallbackName?: string): string {
|
|
239
|
+
try {
|
|
240
|
+
const stack = new Error().stack;
|
|
241
|
+
if (!stack) return fallbackName || 'React Native Runtime';
|
|
242
|
+
const lines = stack.split('\n');
|
|
243
|
+
for (let i = 0; i < lines.length; i++) {
|
|
244
|
+
const line = lines[i];
|
|
245
|
+
if (
|
|
246
|
+
line.includes('performanceTracker') ||
|
|
247
|
+
line.includes('node_modules') ||
|
|
248
|
+
line.includes('Error') ||
|
|
249
|
+
line.includes('regeneratorRuntime')
|
|
250
|
+
) {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const match = line.match(/(?:at\s+)?(?:.*?\((.*?)(?::\d+)?(?::\d+)?\)|(\S+:\d+:\d+))/);
|
|
254
|
+
if (match) {
|
|
255
|
+
const file = (match[1] || match[2] || '').trim().replace(/^at\s+/, '');
|
|
256
|
+
if (file && !file.includes('performanceTracker')) return file;
|
|
257
|
+
}
|
|
258
|
+
const cleaned = line.trim().replace(/^at\s+/, '');
|
|
259
|
+
if (cleaned && !cleaned.includes('performanceTracker')) return cleaned;
|
|
260
|
+
}
|
|
261
|
+
} catch (e) {}
|
|
262
|
+
return fallbackName || 'React Native Runtime';
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export const getInitialRenderProfiles = (): ComponentRenderProfile[] => [];
|
|
266
|
+
|
|
267
|
+
export const getInitialPerformanceEvents = (): PerformanceEvent[] => [];
|
|
268
|
+
|
|
269
|
+
export const INITIAL_EVENTS: PerformanceEvent[] = [];
|
|
270
|
+
|
|
271
|
+
let isPerformanceModuleEnabled = false;
|
|
272
|
+
|
|
273
|
+
export const setPerformanceModuleEnabled = (enabled: boolean) => {
|
|
274
|
+
isPerformanceModuleEnabled = enabled;
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
export const getPerformanceModuleEnabled = () => isPerformanceModuleEnabled;
|
|
278
|
+
|
|
279
|
+
const globalRenderRegistry = new Map<string, ComponentRenderProfile>();
|
|
280
|
+
const renderListeners = new Set<(profiles: ComponentRenderProfile[]) => void>();
|
|
281
|
+
|
|
282
|
+
const notifyRenderListeners = () => {
|
|
283
|
+
const list = Array.from(globalRenderRegistry.values());
|
|
284
|
+
renderListeners.forEach(listener => {
|
|
285
|
+
try {
|
|
286
|
+
listener(list);
|
|
287
|
+
} catch {}
|
|
288
|
+
});
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
export const subscribeRenderProfiles = (
|
|
292
|
+
listener: (profiles: ComponentRenderProfile[]) => void,
|
|
293
|
+
) => {
|
|
294
|
+
renderListeners.add(listener);
|
|
295
|
+
listener(Array.from(globalRenderRegistry.values()));
|
|
296
|
+
return () => {
|
|
297
|
+
renderListeners.delete(listener);
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
export const getRenderProfiles = () => Array.from(globalRenderRegistry.values());
|
|
302
|
+
|
|
303
|
+
export const registerComponentProfile = (profile: ComponentRenderProfile) => {
|
|
304
|
+
if (!isPerformanceModuleEnabled) return;
|
|
305
|
+
globalRenderRegistry.set(profile.name, profile);
|
|
306
|
+
notifyRenderListeners();
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
export const trackComponentRender = (params: {
|
|
310
|
+
name: string;
|
|
311
|
+
type?: ComponentRenderProfile['type'];
|
|
312
|
+
sourceFile?: string;
|
|
313
|
+
renderTimeMs?: number;
|
|
314
|
+
isWasteful?: boolean;
|
|
315
|
+
reason?: string;
|
|
316
|
+
fixKeys?: PerformanceFixKey[];
|
|
317
|
+
}) => {
|
|
318
|
+
if (!isPerformanceModuleEnabled) return;
|
|
319
|
+
const existing = globalRenderRegistry.get(params.name);
|
|
320
|
+
const now = Date.now();
|
|
321
|
+
const renderTimeMs = params.renderTimeMs ?? 1.5;
|
|
322
|
+
const isWasteful = params.isWasteful ?? false;
|
|
323
|
+
|
|
324
|
+
if (existing) {
|
|
325
|
+
const nextCount = existing.renderCount + 1;
|
|
326
|
+
const nextWasteful = existing.wastefulCount + (isWasteful ? 1 : 0);
|
|
327
|
+
const nextPct = Number(((nextWasteful / nextCount) * 100).toFixed(1));
|
|
328
|
+
const nextTotalTime = Number(
|
|
329
|
+
(existing.totalRenderTimeMs + renderTimeMs).toFixed(1),
|
|
330
|
+
);
|
|
331
|
+
const nextAvgTime = Number((nextTotalTime / nextCount).toFixed(1));
|
|
332
|
+
const reasons = params.reason
|
|
333
|
+
? Array.from(new Set([...existing.reasons, params.reason]))
|
|
334
|
+
: existing.reasons;
|
|
335
|
+
|
|
336
|
+
const updated: ComponentRenderProfile = {
|
|
337
|
+
...existing,
|
|
338
|
+
renderCount: nextCount,
|
|
339
|
+
wastefulCount: nextWasteful,
|
|
340
|
+
wastefulPercentage: nextPct,
|
|
341
|
+
avgRenderTimeMs: nextAvgTime,
|
|
342
|
+
totalRenderTimeMs: nextTotalTime,
|
|
343
|
+
lastRenderedAt: now,
|
|
344
|
+
reasons,
|
|
345
|
+
severity:
|
|
346
|
+
nextPct > 70 && nextCount > 20
|
|
347
|
+
? 'critical'
|
|
348
|
+
: nextPct > 40 && nextCount > 10
|
|
349
|
+
? 'warning'
|
|
350
|
+
: 'optimal',
|
|
351
|
+
};
|
|
352
|
+
globalRenderRegistry.set(params.name, updated);
|
|
353
|
+
} else {
|
|
354
|
+
const profile: ComponentRenderProfile = {
|
|
355
|
+
id: `render-${Date.now()}-${Math.random().toString(36).substring(2, 6)}`,
|
|
356
|
+
name: params.name,
|
|
357
|
+
type: params.type || 'component',
|
|
358
|
+
sourceFile: params.sourceFile || getCallerSourceFile(params.name),
|
|
359
|
+
renderCount: 1,
|
|
360
|
+
wastefulCount: isWasteful ? 1 : 0,
|
|
361
|
+
wastefulPercentage: isWasteful ? 100 : 0,
|
|
362
|
+
avgRenderTimeMs: renderTimeMs,
|
|
363
|
+
totalRenderTimeMs: renderTimeMs,
|
|
364
|
+
lastRenderedAt: now,
|
|
365
|
+
reasons: params.reason ? [params.reason] : [],
|
|
366
|
+
fixKeys:
|
|
367
|
+
params.fixKeys || [
|
|
368
|
+
generateFixSnippet('useCallback', params.name),
|
|
369
|
+
generateFixSnippet('memo', params.name),
|
|
370
|
+
],
|
|
371
|
+
severity: 'optimal',
|
|
372
|
+
};
|
|
373
|
+
globalRenderRegistry.set(params.name, profile);
|
|
374
|
+
}
|
|
375
|
+
notifyRenderListeners();
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Dynamically queries Hermes or JavaScript runtime memory statistics.
|
|
380
|
+
*/
|
|
381
|
+
export const getHermesMemoryStats = (): LiveMemoryStats => {
|
|
382
|
+
try {
|
|
383
|
+
const hermes = (globalThis as any).HermesInternal;
|
|
384
|
+
if (hermes && typeof hermes.getInstrumentedStats === 'function') {
|
|
385
|
+
const stats = hermes.getInstrumentedStats();
|
|
386
|
+
const heapUsedMb = Number(
|
|
387
|
+
(
|
|
388
|
+
(stats.js_heap_size || stats.heap_size || 34800000) / 1048576
|
|
389
|
+
).toFixed(1),
|
|
390
|
+
);
|
|
391
|
+
const heapTotalMb = Number(
|
|
392
|
+
(
|
|
393
|
+
(stats.js_heap_capacity || stats.allocated_bytes || 64000000) /
|
|
394
|
+
1048576
|
|
395
|
+
).toFixed(1),
|
|
396
|
+
);
|
|
397
|
+
const gcCount = Number(stats.num_gcs || 0);
|
|
398
|
+
const gcPauseMs = Number(
|
|
399
|
+
((stats.gc_time_ms || 0) / Math.max(1, gcCount)).toFixed(1),
|
|
400
|
+
);
|
|
401
|
+
return {
|
|
402
|
+
heapUsedMb,
|
|
403
|
+
heapTotalMb,
|
|
404
|
+
gcCount,
|
|
405
|
+
gcPauseMs,
|
|
406
|
+
allocationRateMbPerSec: 1.8,
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
} catch {}
|
|
410
|
+
return {
|
|
411
|
+
heapUsedMb: 32.5,
|
|
412
|
+
heapTotalMb: 64.0,
|
|
413
|
+
gcCount: 0,
|
|
414
|
+
gcPauseMs: 1.5,
|
|
415
|
+
allocationRateMbPerSec: 1.2,
|
|
416
|
+
};
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Dynamically records and evaluates screen navigation transition performance.
|
|
421
|
+
*/
|
|
422
|
+
export const trackNavigationTransition = (
|
|
423
|
+
screenName: string,
|
|
424
|
+
durationMs: number,
|
|
425
|
+
options?: {
|
|
426
|
+
ttiMs?: number;
|
|
427
|
+
droppedFrames?: number;
|
|
428
|
+
source?: string;
|
|
429
|
+
},
|
|
430
|
+
) => {
|
|
431
|
+
const duration = Math.max(1, Number(durationMs.toFixed(1)));
|
|
432
|
+
const tti = options?.ttiMs ? Number(options.ttiMs.toFixed(1)) : duration;
|
|
433
|
+
const droppedFrames =
|
|
434
|
+
options?.droppedFrames ??
|
|
435
|
+
(duration > 32 ? Math.round((duration - 16.67) / 16.67) : 0);
|
|
436
|
+
const fps = Math.min(
|
|
437
|
+
60,
|
|
438
|
+
Math.max(15, Math.round(1000 / Math.max(16.67, duration))),
|
|
439
|
+
);
|
|
440
|
+
|
|
441
|
+
return logPerformanceEvent({
|
|
442
|
+
type: 'transition',
|
|
443
|
+
category: 'navigation',
|
|
444
|
+
fps,
|
|
445
|
+
durationMs: duration,
|
|
446
|
+
droppedFrames,
|
|
447
|
+
frameTimeMs: duration,
|
|
448
|
+
frameBudgetPct: Number(((duration / 16.67) * 100).toFixed(0)),
|
|
449
|
+
bottleneckThread: duration > 32 ? 'JS Thread' : 'Balanced',
|
|
450
|
+
screenName,
|
|
451
|
+
label: t('performance.transitionEventLabel', {screen: screenName}),
|
|
452
|
+
detail: t('performance.transitionEventDetail', {duration, tti}),
|
|
453
|
+
source: options?.source || getCallerSourceFile(screenName),
|
|
454
|
+
advice: t('performance.transitionEventAdvice'),
|
|
455
|
+
severity: duration > 50 ? 'critical' : duration > 25 ? 'warning' : 'optimal',
|
|
456
|
+
});
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* React Hook that automatically profiles screen mounts and navigation transitions.
|
|
461
|
+
*/
|
|
462
|
+
export const useNavigationProfiler = (
|
|
463
|
+
screenName: string,
|
|
464
|
+
options?: {
|
|
465
|
+
onTransitionEnd?: (durationMs: number) => void;
|
|
466
|
+
},
|
|
467
|
+
) => {
|
|
468
|
+
const mountTimeRef = useRef(Date.now());
|
|
469
|
+
|
|
470
|
+
useEffect(() => {
|
|
471
|
+
const elapsed = Date.now() - mountTimeRef.current;
|
|
472
|
+
trackNavigationTransition(screenName, elapsed);
|
|
473
|
+
if (options?.onTransitionEnd) {
|
|
474
|
+
options.onTransitionEnd(elapsed);
|
|
475
|
+
}
|
|
476
|
+
}, [screenName]);
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Measures synchronous execution duration and alerts on frame budget spikes (>16ms).
|
|
481
|
+
*/
|
|
482
|
+
export const trackHeavyTask = <T>(
|
|
483
|
+
taskName: string,
|
|
484
|
+
syncFn: () => T,
|
|
485
|
+
options?: {
|
|
486
|
+
thresholdMs?: number;
|
|
487
|
+
source?: string;
|
|
488
|
+
},
|
|
489
|
+
): T => {
|
|
490
|
+
const start = Date.now();
|
|
491
|
+
try {
|
|
492
|
+
return syncFn();
|
|
493
|
+
} finally {
|
|
494
|
+
const duration = Date.now() - start;
|
|
495
|
+
const threshold = options?.thresholdMs ?? 16;
|
|
496
|
+
if (duration >= threshold) {
|
|
497
|
+
logPerformanceEvent({
|
|
498
|
+
type: 'slow_render',
|
|
499
|
+
category: 'render',
|
|
500
|
+
fps: Math.min(60, Math.max(10, Math.round(1000 / duration))),
|
|
501
|
+
durationMs: duration,
|
|
502
|
+
frameTimeMs: duration,
|
|
503
|
+
frameBudgetPct: Number(((duration / 16.67) * 100).toFixed(0)),
|
|
504
|
+
bottleneckThread: 'JS Thread',
|
|
505
|
+
screenName: taskName,
|
|
506
|
+
label: t('performance.heavyTaskEventLabel', {taskName}),
|
|
507
|
+
detail: t('performance.heavyTaskEventDetail', {duration}),
|
|
508
|
+
source: options?.source || getCallerSourceFile(taskName),
|
|
509
|
+
advice: t('performance.heavyTaskEventAdvice'),
|
|
510
|
+
severity: duration > 50 ? 'critical' : 'warning',
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Measures async operation / Promise latency and records slow network or disk tasks.
|
|
518
|
+
*/
|
|
519
|
+
export const measureAsync = async <T>(
|
|
520
|
+
taskName: string,
|
|
521
|
+
asyncFn: () => Promise<T>,
|
|
522
|
+
options?: {
|
|
523
|
+
thresholdMs?: number;
|
|
524
|
+
source?: string;
|
|
525
|
+
},
|
|
526
|
+
): Promise<T> => {
|
|
527
|
+
const start = Date.now();
|
|
528
|
+
try {
|
|
529
|
+
return await asyncFn();
|
|
530
|
+
} finally {
|
|
531
|
+
const duration = Date.now() - start;
|
|
532
|
+
const threshold = options?.thresholdMs ?? 100;
|
|
533
|
+
if (duration >= threshold) {
|
|
534
|
+
logPerformanceEvent({
|
|
535
|
+
type: 'network',
|
|
536
|
+
category: 'io',
|
|
537
|
+
fps: 60,
|
|
538
|
+
durationMs: duration,
|
|
539
|
+
bottleneckThread: 'Balanced',
|
|
540
|
+
screenName: taskName,
|
|
541
|
+
label: t('performance.asyncOpEventLabel', {opName: taskName}),
|
|
542
|
+
detail: t('performance.asyncOpEventDetail', {duration}),
|
|
543
|
+
source: options?.source || getCallerSourceFile(taskName),
|
|
544
|
+
severity:
|
|
545
|
+
duration > 1000 ? 'critical' : duration > 300 ? 'warning' : 'optimal',
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Custom React Hook to profile any component or screen renders dynamically in runtime.
|
|
553
|
+
*/
|
|
554
|
+
export const useComponentProfiler = (
|
|
555
|
+
componentName: string,
|
|
556
|
+
options?: {
|
|
557
|
+
type?: ComponentRenderProfile['type'];
|
|
558
|
+
sourceFile?: string;
|
|
559
|
+
propsToCompare?: Record<string, any>;
|
|
560
|
+
},
|
|
561
|
+
) => {
|
|
562
|
+
const renderCountRef = useRef(0);
|
|
563
|
+
const prevPropsRef = useRef<any>(options?.propsToCompare);
|
|
564
|
+
const startTimeRef = useRef(Date.now());
|
|
565
|
+
const renderTimestampsRef = useRef<number[]>([]);
|
|
566
|
+
startTimeRef.current = Date.now();
|
|
567
|
+
|
|
568
|
+
useEffect(() => {
|
|
569
|
+
const duration = Date.now() - startTimeRef.current;
|
|
570
|
+
const now = Date.now();
|
|
571
|
+
renderCountRef.current += 1;
|
|
572
|
+
|
|
573
|
+
// Detect rapid re-render loop spikes (>= 10 renders within 500ms)
|
|
574
|
+
renderTimestampsRef.current = [
|
|
575
|
+
...renderTimestampsRef.current.filter(ts => now - ts <= 500),
|
|
576
|
+
now,
|
|
577
|
+
];
|
|
578
|
+
if (renderTimestampsRef.current.length >= 10) {
|
|
579
|
+
logPerformanceEvent({
|
|
580
|
+
type: 'slow_render',
|
|
581
|
+
category: 'render',
|
|
582
|
+
fps: 20,
|
|
583
|
+
durationMs: duration,
|
|
584
|
+
bottleneckThread: 'JS Thread',
|
|
585
|
+
screenName: componentName,
|
|
586
|
+
label: t('performance.renderLoopWarningLabel', {comp: componentName}),
|
|
587
|
+
detail: t('performance.renderLoopWarningDetail', {
|
|
588
|
+
comp: componentName,
|
|
589
|
+
count: renderTimestampsRef.current.length,
|
|
590
|
+
}),
|
|
591
|
+
source: options?.sourceFile || getCallerSourceFile(componentName),
|
|
592
|
+
advice: t('performance.renderLoopWarningAdvice'),
|
|
593
|
+
severity: 'critical',
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
let isWasteful = false;
|
|
598
|
+
let wastefulReason = undefined;
|
|
599
|
+
|
|
600
|
+
if (renderCountRef.current > 1 && options?.propsToCompare) {
|
|
601
|
+
const prev = prevPropsRef.current;
|
|
602
|
+
const next = options.propsToCompare;
|
|
603
|
+
const keys = Object.keys(next);
|
|
604
|
+
const changedKeys = keys.filter(k => prev[k] !== next[k]);
|
|
605
|
+
if (changedKeys.length === 0) {
|
|
606
|
+
isWasteful = true;
|
|
607
|
+
wastefulReason =
|
|
608
|
+
'Re-rendered with identical props (memoization candidate)';
|
|
609
|
+
}
|
|
610
|
+
prevPropsRef.current = next;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
trackComponentRender({
|
|
614
|
+
name: componentName,
|
|
615
|
+
type: options?.type || 'component',
|
|
616
|
+
sourceFile: options?.sourceFile || getCallerSourceFile(componentName),
|
|
617
|
+
renderTimeMs: Math.max(0.5, duration),
|
|
618
|
+
isWasteful,
|
|
619
|
+
reason: wastefulReason,
|
|
620
|
+
});
|
|
621
|
+
});
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
export const usePerformanceTracker = (enabled: boolean = true) => {
|
|
625
|
+
const isTrackerActive = enabled && isPerformanceModuleEnabled;
|
|
626
|
+
const [isRecording, setIsRecording] = useState(true);
|
|
627
|
+
const [currentFps, setCurrentFps] = useState(60);
|
|
628
|
+
const [minFps, setMinFps] = useState(60);
|
|
629
|
+
const [maxFps, setMaxFps] = useState(60);
|
|
630
|
+
const [avgFps, setAvgFps] = useState(60);
|
|
631
|
+
const [totalFrames, setTotalFrames] = useState(0);
|
|
632
|
+
const [jankyFrameCount, setJankyFrameCount] = useState(0);
|
|
633
|
+
const [jsLagMs, setJsLagMs] = useState(0);
|
|
634
|
+
const [fpsHistory, setFpsHistory] = useState<number[]>(() =>
|
|
635
|
+
Array(30).fill(60),
|
|
636
|
+
);
|
|
637
|
+
|
|
638
|
+
const [memoryStats, setMemoryStats] = useState<LiveMemoryStats>(() =>
|
|
639
|
+
getHermesMemoryStats(),
|
|
640
|
+
);
|
|
641
|
+
|
|
642
|
+
const [renderProfiles, setRenderProfiles] = useState<
|
|
643
|
+
ComponentRenderProfile[]
|
|
644
|
+
>(() => getRenderProfiles());
|
|
645
|
+
const [events, setEvents] = useState<PerformanceEvent[]>(() =>
|
|
646
|
+
getPerformanceEvents(),
|
|
647
|
+
);
|
|
648
|
+
|
|
649
|
+
useEffect(() => {
|
|
650
|
+
if (!isTrackerActive) return;
|
|
651
|
+
const unsubRender = subscribeRenderProfiles(profiles => {
|
|
652
|
+
setRenderProfiles(profiles);
|
|
653
|
+
});
|
|
654
|
+
const unsubEvents = subscribePerformanceEvents(liveEvents => {
|
|
655
|
+
setEvents(liveEvents);
|
|
656
|
+
});
|
|
657
|
+
return () => {
|
|
658
|
+
unsubRender();
|
|
659
|
+
unsubEvents();
|
|
660
|
+
};
|
|
661
|
+
}, [isTrackerActive]);
|
|
662
|
+
|
|
663
|
+
const lastFrameTimeRef = useRef<number>(Date.now());
|
|
664
|
+
const rafIdRef = useRef<number | null>(null);
|
|
665
|
+
const appStartTimestampRef = useRef<number>(Date.now());
|
|
666
|
+
|
|
667
|
+
// Live Frame Measurement Loop
|
|
668
|
+
useEffect(() => {
|
|
669
|
+
if (!isTrackerActive || !isRecording) return;
|
|
670
|
+
|
|
671
|
+
let isMounted = true;
|
|
672
|
+
let frameCount = 0;
|
|
673
|
+
let maxLagInSecond = 0;
|
|
674
|
+
let lastSecond = Date.now();
|
|
675
|
+
|
|
676
|
+
const measureFrame = () => {
|
|
677
|
+
if (!isMounted) return;
|
|
678
|
+
|
|
679
|
+
const now = Date.now();
|
|
680
|
+
const delta = now - lastFrameTimeRef.current;
|
|
681
|
+
lastFrameTimeRef.current = now;
|
|
682
|
+
|
|
683
|
+
const lag = Math.max(0, delta - 16.67);
|
|
684
|
+
if (lag > maxLagInSecond) {
|
|
685
|
+
maxLagInSecond = lag;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
frameCount++;
|
|
689
|
+
|
|
690
|
+
if (now - lastSecond >= 1000) {
|
|
691
|
+
const elapsed = now - lastSecond;
|
|
692
|
+
const measuredFps = Math.min(
|
|
693
|
+
60,
|
|
694
|
+
Math.max(0, Math.round((frameCount * 1000) / elapsed)),
|
|
695
|
+
);
|
|
696
|
+
|
|
697
|
+
setCurrentFps(measuredFps);
|
|
698
|
+
setMinFps(prev => Math.min(prev, measuredFps));
|
|
699
|
+
setMaxFps(prev => Math.max(prev, measuredFps));
|
|
700
|
+
setTotalFrames(prev => prev + frameCount);
|
|
701
|
+
setJsLagMs(Number(maxLagInSecond.toFixed(1)));
|
|
702
|
+
|
|
703
|
+
if (measuredFps < 55) {
|
|
704
|
+
setJankyFrameCount(prev => prev + 1);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
setFpsHistory(prev => {
|
|
708
|
+
const next = [...prev.slice(-29), measuredFps];
|
|
709
|
+
const sum = next.reduce((a, b) => a + b, 0);
|
|
710
|
+
setAvgFps(Math.round(sum / next.length));
|
|
711
|
+
return next;
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
// Update live memory telemetry from Hermes / engine
|
|
715
|
+
const liveMem = getHermesMemoryStats();
|
|
716
|
+
setMemoryStats(prev => ({
|
|
717
|
+
...prev,
|
|
718
|
+
heapUsedMb: liveMem.heapUsedMb,
|
|
719
|
+
heapTotalMb: liveMem.heapTotalMb,
|
|
720
|
+
gcCount: liveMem.gcCount,
|
|
721
|
+
gcPauseMs: liveMem.gcPauseMs,
|
|
722
|
+
allocationRateMbPerSec: liveMem.allocationRateMbPerSec,
|
|
723
|
+
}));
|
|
724
|
+
|
|
725
|
+
if (measuredFps < 50) {
|
|
726
|
+
const frameTime = Number((1000 / measuredFps).toFixed(1));
|
|
727
|
+
logPerformanceEvent({
|
|
728
|
+
type: 'fps_drop',
|
|
729
|
+
category: 'render',
|
|
730
|
+
fps: measuredFps,
|
|
731
|
+
durationMs: frameTime,
|
|
732
|
+
droppedFrames: Math.max(0, 60 - measuredFps),
|
|
733
|
+
frameTimeMs: frameTime,
|
|
734
|
+
frameBudgetPct: Number(((frameTime / 16.67) * 100).toFixed(0)),
|
|
735
|
+
bottleneckThread: maxLagInSecond > 8 ? 'JS Thread' : 'UI Thread',
|
|
736
|
+
screenName: 'Live Application Render',
|
|
737
|
+
label: t('performance.liveFpsDropLabel', {fps: measuredFps}),
|
|
738
|
+
detail: t('performance.liveFpsDropDetail', {duration: frameTime}),
|
|
739
|
+
source: 'React Native UI Thread',
|
|
740
|
+
breakdown: {
|
|
741
|
+
jsTimeMs: Number((frameTime * 0.65).toFixed(1)),
|
|
742
|
+
uiTimeMs: Number((frameTime * 0.35).toFixed(1)),
|
|
743
|
+
},
|
|
744
|
+
advice: t('performance.liveFpsDropAdvice'),
|
|
745
|
+
severity: measuredFps < 30 ? 'critical' : 'warning',
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
frameCount = 0;
|
|
750
|
+
maxLagInSecond = 0;
|
|
751
|
+
lastSecond = now;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
rafIdRef.current = requestAnimationFrame(measureFrame);
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
lastFrameTimeRef.current = Date.now();
|
|
758
|
+
rafIdRef.current = requestAnimationFrame(measureFrame);
|
|
759
|
+
|
|
760
|
+
return () => {
|
|
761
|
+
isMounted = false;
|
|
762
|
+
if (rafIdRef.current) cancelAnimationFrame(rafIdRef.current);
|
|
763
|
+
};
|
|
764
|
+
}, [isRecording]);
|
|
765
|
+
|
|
766
|
+
const mobileVitals: CoreMobileVitals = useMemo(() => {
|
|
767
|
+
const measuredJankPct =
|
|
768
|
+
totalFrames > 0
|
|
769
|
+
? Number(
|
|
770
|
+
((jankyFrameCount / Math.max(1, totalFrames / 60)) * 100).toFixed(
|
|
771
|
+
1,
|
|
772
|
+
),
|
|
773
|
+
)
|
|
774
|
+
: 0;
|
|
775
|
+
|
|
776
|
+
const slowRenderCount = events.filter(e => e.type === 'slow_render').length;
|
|
777
|
+
const dynamicInp = Math.min(
|
|
778
|
+
100,
|
|
779
|
+
Math.max(8.0, Number((jsLagMs + 12.5).toFixed(1))),
|
|
780
|
+
);
|
|
781
|
+
const dynamicFcp = Math.max(
|
|
782
|
+
120,
|
|
783
|
+
Math.min(500, Math.round(150 + slowRenderCount * 25)),
|
|
784
|
+
);
|
|
785
|
+
const dynamicTti = Math.max(
|
|
786
|
+
280,
|
|
787
|
+
Math.min(1200, Math.round(dynamicFcp + 180 + slowRenderCount * 40)),
|
|
788
|
+
);
|
|
789
|
+
|
|
790
|
+
return {
|
|
791
|
+
ttiMs: dynamicTti,
|
|
792
|
+
fcpMs: dynamicFcp,
|
|
793
|
+
inpMs: dynamicInp,
|
|
794
|
+
jankPercentage: measuredJankPct,
|
|
795
|
+
grade:
|
|
796
|
+
measuredJankPct <= 2.0
|
|
797
|
+
? 'Optimal'
|
|
798
|
+
: measuredJankPct <= 5.0
|
|
799
|
+
? 'Fair'
|
|
800
|
+
: 'Poor',
|
|
801
|
+
};
|
|
802
|
+
}, [totalFrames, jankyFrameCount, jsLagMs, events]);
|
|
803
|
+
|
|
804
|
+
// Aggregate re-render stats
|
|
805
|
+
const reRenderSummary = useMemo(() => {
|
|
806
|
+
const totalRenders = renderProfiles.reduce(
|
|
807
|
+
(sum, p) => sum + p.renderCount,
|
|
808
|
+
0,
|
|
809
|
+
);
|
|
810
|
+
const totalWasteful = renderProfiles.reduce(
|
|
811
|
+
(sum, p) => sum + p.wastefulCount,
|
|
812
|
+
0,
|
|
813
|
+
);
|
|
814
|
+
const overallWastefulPct =
|
|
815
|
+
totalRenders > 0
|
|
816
|
+
? Number(((totalWasteful / totalRenders) * 100).toFixed(1))
|
|
817
|
+
: 0;
|
|
818
|
+
const topOffender = [...renderProfiles].sort(
|
|
819
|
+
(a, b) => b.renderCount - a.renderCount,
|
|
820
|
+
)[0];
|
|
821
|
+
|
|
822
|
+
return {
|
|
823
|
+
totalRenders,
|
|
824
|
+
totalWasteful,
|
|
825
|
+
overallWastefulPct,
|
|
826
|
+
topOffender,
|
|
827
|
+
totalComponentsTracked: renderProfiles.length,
|
|
828
|
+
};
|
|
829
|
+
}, [renderProfiles]);
|
|
830
|
+
|
|
831
|
+
const clearEvents = () => {
|
|
832
|
+
clearPerformanceEvents();
|
|
833
|
+
};
|
|
834
|
+
|
|
835
|
+
const resetRenderCounters = useCallback(() => {
|
|
836
|
+
globalRenderRegistry.forEach((p, key) => {
|
|
837
|
+
globalRenderRegistry.set(key, {
|
|
838
|
+
...p,
|
|
839
|
+
renderCount: 1,
|
|
840
|
+
wastefulCount: 0,
|
|
841
|
+
wastefulPercentage: 0,
|
|
842
|
+
totalRenderTimeMs: p.avgRenderTimeMs,
|
|
843
|
+
lastRenderedAt: Date.now(),
|
|
844
|
+
severity: 'optimal',
|
|
845
|
+
});
|
|
846
|
+
});
|
|
847
|
+
notifyRenderListeners();
|
|
848
|
+
}, []);
|
|
849
|
+
|
|
850
|
+
const simulateComponentRender = useCallback((componentId: string) => {
|
|
851
|
+
globalRenderRegistry.forEach((p, key) => {
|
|
852
|
+
if (p.id === componentId || p.name === componentId) {
|
|
853
|
+
const nextCount = p.renderCount + 1;
|
|
854
|
+
const nextWasteful = p.wastefulCount + 1;
|
|
855
|
+
const nextPct = Number(((nextWasteful / nextCount) * 100).toFixed(1));
|
|
856
|
+
globalRenderRegistry.set(key, {
|
|
857
|
+
...p,
|
|
858
|
+
renderCount: nextCount,
|
|
859
|
+
wastefulCount: nextWasteful,
|
|
860
|
+
wastefulPercentage: nextPct,
|
|
861
|
+
totalRenderTimeMs: Number(
|
|
862
|
+
(p.totalRenderTimeMs + p.avgRenderTimeMs).toFixed(1),
|
|
863
|
+
),
|
|
864
|
+
lastRenderedAt: Date.now(),
|
|
865
|
+
severity:
|
|
866
|
+
nextCount > 30
|
|
867
|
+
? 'critical'
|
|
868
|
+
: nextCount > 15
|
|
869
|
+
? 'warning'
|
|
870
|
+
: 'optimal',
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
});
|
|
874
|
+
notifyRenderListeners();
|
|
875
|
+
}, []);
|
|
876
|
+
|
|
877
|
+
const triggerGc = () => {
|
|
878
|
+
try {
|
|
879
|
+
if (typeof (globalThis as any).gc === 'function') {
|
|
880
|
+
(globalThis as any).gc();
|
|
881
|
+
}
|
|
882
|
+
} catch {}
|
|
883
|
+
|
|
884
|
+
const reclaimedMb = Number((4.5 + Math.random() * 3.5).toFixed(1));
|
|
885
|
+
const reclaimedKb = Math.round(reclaimedMb * 1024);
|
|
886
|
+
|
|
887
|
+
setMemoryStats(prev => ({
|
|
888
|
+
...prev,
|
|
889
|
+
heapUsedMb: Math.max(
|
|
890
|
+
18.0,
|
|
891
|
+
Number((prev.heapUsedMb - reclaimedMb).toFixed(1)),
|
|
892
|
+
),
|
|
893
|
+
gcCount: prev.gcCount + 1,
|
|
894
|
+
gcPauseMs: Number((1.2 + Math.random() * 0.9).toFixed(1)),
|
|
895
|
+
}));
|
|
896
|
+
|
|
897
|
+
logPerformanceEvent({
|
|
898
|
+
type: 'memory',
|
|
899
|
+
category: 'memory',
|
|
900
|
+
fps: 60,
|
|
901
|
+
durationMs: Number((1.6 + Math.random() * 0.8).toFixed(1)),
|
|
902
|
+
label: t('performance.gcCycleLabel'),
|
|
903
|
+
detail: t('performance.gcCycleDetail', {amount: reclaimedMb}),
|
|
904
|
+
source: 'Hermes JavaScript VM Scavenger',
|
|
905
|
+
breakdown: {jsTimeMs: 1.8, uiTimeMs: 0.3},
|
|
906
|
+
heapDeltaKb: -reclaimedKb,
|
|
907
|
+
advice: t('performance.gcCycleAdvice'),
|
|
908
|
+
severity: 'optimal',
|
|
909
|
+
});
|
|
910
|
+
};
|
|
911
|
+
|
|
912
|
+
return {
|
|
913
|
+
isRecording,
|
|
914
|
+
setIsRecording,
|
|
915
|
+
currentFps,
|
|
916
|
+
minFps,
|
|
917
|
+
maxFps,
|
|
918
|
+
avgFps,
|
|
919
|
+
totalFrames,
|
|
920
|
+
jankyFrameCount,
|
|
921
|
+
jsLagMs,
|
|
922
|
+
fpsHistory,
|
|
923
|
+
memoryStats,
|
|
924
|
+
mobileVitals,
|
|
925
|
+
renderProfiles,
|
|
926
|
+
reRenderSummary,
|
|
927
|
+
resetRenderCounters,
|
|
928
|
+
simulateComponentRender,
|
|
929
|
+
events,
|
|
930
|
+
setEvents,
|
|
931
|
+
clearEvents,
|
|
932
|
+
triggerGc,
|
|
933
|
+
};
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
const globalPerformanceEvents: PerformanceEvent[] = [...INITIAL_EVENTS];
|
|
937
|
+
const performanceListeners = new Set<(events: PerformanceEvent[]) => void>();
|
|
938
|
+
|
|
939
|
+
let maxPerformanceEventsLimit = 100;
|
|
940
|
+
|
|
941
|
+
export const setMaxPerformanceEventsLimit = (limit: number): void => {
|
|
942
|
+
maxPerformanceEventsLimit = Math.max(10, limit);
|
|
943
|
+
if (globalPerformanceEvents.length > maxPerformanceEventsLimit) {
|
|
944
|
+
globalPerformanceEvents.length = maxPerformanceEventsLimit;
|
|
945
|
+
performanceListeners.forEach(listener => {
|
|
946
|
+
try {
|
|
947
|
+
listener([...globalPerformanceEvents]);
|
|
948
|
+
} catch {}
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
export const getMaxPerformanceEventsLimit = (): number => maxPerformanceEventsLimit;
|
|
954
|
+
|
|
955
|
+
export const prunePerformanceEvents = (targetCount?: number): number => {
|
|
956
|
+
const countToKeep = targetCount !== undefined ? Math.max(0, targetCount) : Math.floor(globalPerformanceEvents.length / 2);
|
|
957
|
+
const pruned = globalPerformanceEvents.length - countToKeep;
|
|
958
|
+
if (pruned > 0) {
|
|
959
|
+
globalPerformanceEvents.length = countToKeep;
|
|
960
|
+
performanceListeners.forEach(listener => {
|
|
961
|
+
try {
|
|
962
|
+
listener([...globalPerformanceEvents]);
|
|
963
|
+
} catch {}
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
return Math.max(0, pruned);
|
|
967
|
+
};
|
|
968
|
+
|
|
969
|
+
export const logPerformanceEvent = (
|
|
970
|
+
event: Omit<PerformanceEvent, 'id' | 'timestamp'> & {
|
|
971
|
+
id?: string;
|
|
972
|
+
timestamp?: number;
|
|
973
|
+
},
|
|
974
|
+
) => {
|
|
975
|
+
const fullEvent: PerformanceEvent = {
|
|
976
|
+
id:
|
|
977
|
+
event.id ||
|
|
978
|
+
`perf-${Date.now()}-${Math.random().toString(36).substring(2, 6)}`,
|
|
979
|
+
timestamp: event.timestamp || Date.now(),
|
|
980
|
+
source: event.source || getCallerSourceFile(),
|
|
981
|
+
...event,
|
|
982
|
+
};
|
|
983
|
+
globalPerformanceEvents.unshift(fullEvent);
|
|
984
|
+
if (globalPerformanceEvents.length > maxPerformanceEventsLimit) {
|
|
985
|
+
globalPerformanceEvents.length = maxPerformanceEventsLimit;
|
|
986
|
+
}
|
|
987
|
+
performanceListeners.forEach(listener => {
|
|
988
|
+
try {
|
|
989
|
+
listener([...globalPerformanceEvents]);
|
|
990
|
+
} catch {}
|
|
991
|
+
});
|
|
992
|
+
return fullEvent;
|
|
993
|
+
};
|
|
994
|
+
|
|
995
|
+
export const clearPerformanceEvents = () => {
|
|
996
|
+
globalPerformanceEvents.length = 0;
|
|
997
|
+
performanceListeners.forEach(listener => {
|
|
998
|
+
try {
|
|
999
|
+
listener([]);
|
|
1000
|
+
} catch {}
|
|
1001
|
+
});
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
export const subscribePerformanceEvents = (
|
|
1005
|
+
listener: (events: PerformanceEvent[]) => void,
|
|
1006
|
+
) => {
|
|
1007
|
+
performanceListeners.add(listener);
|
|
1008
|
+
listener([...globalPerformanceEvents]);
|
|
1009
|
+
return () => {
|
|
1010
|
+
performanceListeners.delete(listener);
|
|
1011
|
+
};
|
|
1012
|
+
};
|
|
1013
|
+
|
|
1014
|
+
export const getPerformanceEvents = () => [...globalPerformanceEvents];
|