@rn-perf-kit/overlay 0.0.0-stage → 0.1.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/README.md +18 -2
- package/package.json +39 -4
- package/src/PerfOverlay.tsx +53 -0
- package/src/index.ts +4 -0
- package/src/usePerfStats.ts +49 -0
- package/tsconfig.json +5 -0
package/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rn-perf-kit/overlay
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@rn-perf-kit/overlay)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
> In-app floating performance HUD overlay for **rn-perf-kit** apps.
|
|
7
|
+
|
|
8
|
+
Part of the [rn-perf-kit](https://github.com/arpitkumarsahu/rn-perf-kit) monorepo.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```tsx
|
|
13
|
+
import { PerfOverlay } from '@rn-perf-kit/overlay';
|
|
14
|
+
|
|
15
|
+
<PerfOverlay position="top-right" />
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
- Throttled to **2 Hz** to keep overhead < 0.5% CPU.
|
|
19
|
+
- Displays live UI FPS, JS FPS, long commits, stalls, and active screen.
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rn-perf-kit/overlay",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "In-app performance HUD overlay for rn-perf-kit",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"lint": "eslint src/"
|
|
10
|
+
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"react": ">=18.0.0",
|
|
13
|
+
"react-native": ">=0.73.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"typescript": "^5.3.0",
|
|
17
|
+
"@types/react": "^18.0.0",
|
|
18
|
+
"@types/react-native": "^0.73.0"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@rn-perf-kit/core": "^0.1.1"
|
|
22
|
+
},
|
|
23
|
+
"author": "Arpit Kumar Sahu <aarpitkumarsahu99@gmail.com>",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/arpitkumarsahu/rn-perf-kit.git",
|
|
28
|
+
"directory": "packages/overlay"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/arpitkumarsahu/rn-perf-kit/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/arpitkumarsahu/rn-perf-kit#readme",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"react-native",
|
|
36
|
+
"hud",
|
|
37
|
+
"perf-overlay",
|
|
38
|
+
"fps-meter",
|
|
39
|
+
"in-app-hud"
|
|
40
|
+
]
|
|
41
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PerfOverlay — in-app HUD showing live performance stats.
|
|
3
|
+
*/
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { View, Text, StyleSheet, type ViewStyle } from 'react-native';
|
|
6
|
+
import { usePerfStats } from './usePerfStats';
|
|
7
|
+
|
|
8
|
+
type Position = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
|
|
9
|
+
interface PerfOverlayProps { position?: Position; disabled?: boolean; }
|
|
10
|
+
|
|
11
|
+
const positionStyles: Record<Position, ViewStyle> = {
|
|
12
|
+
'top-right': { top: 50, right: 8 },
|
|
13
|
+
'top-left': { top: 50, left: 8 },
|
|
14
|
+
'bottom-right': { bottom: 50, right: 8 },
|
|
15
|
+
'bottom-left': { bottom: 50, left: 8 },
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function PerfOverlay({ position = 'top-right', disabled = false }: PerfOverlayProps): React.JSX.Element | null {
|
|
19
|
+
const stats = usePerfStats();
|
|
20
|
+
if (disabled) return null;
|
|
21
|
+
const jsFpsColor = stats.jsFps >= 55 ? '#4ade80' : stats.jsFps >= 30 ? '#facc15' : '#f87171';
|
|
22
|
+
const uiFpsColor = stats.uiFps >= 55 ? '#4ade80' : stats.uiFps >= 30 ? '#facc15' : '#f87171';
|
|
23
|
+
return (
|
|
24
|
+
<View style={[styles.container, positionStyles[position]]}>
|
|
25
|
+
<View style={styles.row}>
|
|
26
|
+
<Text style={styles.label}>JS </Text>
|
|
27
|
+
<Text style={[styles.value, { color: jsFpsColor }]}>{stats.jsFps}fps</Text>
|
|
28
|
+
</View>
|
|
29
|
+
<View style={styles.row}>
|
|
30
|
+
<Text style={styles.label}>UI </Text>
|
|
31
|
+
<Text style={[styles.value, { color: uiFpsColor }]}>{stats.uiFps}fps</Text>
|
|
32
|
+
</View>
|
|
33
|
+
{stats.stallCount > 0 && (
|
|
34
|
+
<View style={styles.row}>
|
|
35
|
+
<Text style={[styles.label, { color: '#f87171' }]}>stalls:{stats.stallCount}</Text>
|
|
36
|
+
</View>
|
|
37
|
+
)}
|
|
38
|
+
{stats.currentRoute ? (
|
|
39
|
+
<View style={styles.row}>
|
|
40
|
+
<Text style={styles.route} numberOfLines={1}>{stats.currentRoute}</Text>
|
|
41
|
+
</View>
|
|
42
|
+
) : null}
|
|
43
|
+
</View>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const styles = StyleSheet.create({
|
|
48
|
+
container: { position: 'absolute', backgroundColor: 'rgba(0,0,0,0.75)', borderRadius: 8, padding: 8, minWidth: 90, zIndex: 9999 },
|
|
49
|
+
row: { flexDirection: 'row', alignItems: 'center', marginBottom: 2 },
|
|
50
|
+
label: { color: '#94a3b8', fontSize: 11, fontFamily: 'monospace' },
|
|
51
|
+
value: { fontSize: 12, fontWeight: '700', fontFamily: 'monospace' },
|
|
52
|
+
route: { color: '#7dd3fc', fontSize: 10, maxWidth: 100, fontFamily: 'monospace' },
|
|
53
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook that computes live performance stats for the overlay.
|
|
3
|
+
* Updates at ~2 Hz to minimize overhead.
|
|
4
|
+
*/
|
|
5
|
+
import { useState, useEffect, useRef } from 'react';
|
|
6
|
+
import { timeline } from '@rn-perf-kit/core';
|
|
7
|
+
import type { PerfEvent } from '@rn-perf-kit/core';
|
|
8
|
+
|
|
9
|
+
const UPDATE_INTERVAL_MS = 500;
|
|
10
|
+
const FPS_WINDOW_MS = 1000;
|
|
11
|
+
|
|
12
|
+
export interface PerfStats {
|
|
13
|
+
jsFps: number;
|
|
14
|
+
uiFps: number;
|
|
15
|
+
stallCount: number;
|
|
16
|
+
jankFrameCount: number;
|
|
17
|
+
currentRoute: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function usePerfStats(): PerfStats {
|
|
21
|
+
const [stats, setStats] = useState<PerfStats>({
|
|
22
|
+
jsFps: 60, uiFps: 60, stallCount: 0, jankFrameCount: 0, currentRoute: '',
|
|
23
|
+
});
|
|
24
|
+
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
timerRef.current = setInterval(() => {
|
|
28
|
+
const now = timeline.now();
|
|
29
|
+
const recent = timeline.recent(FPS_WINDOW_MS * 2);
|
|
30
|
+
const frames = recent.filter(
|
|
31
|
+
(e): e is Extract<PerfEvent, { type: 'frame' }> => e.type === 'frame' && e.t >= now - FPS_WINDOW_MS
|
|
32
|
+
);
|
|
33
|
+
const stalls = recent.filter((e) => e.type === 'stall' && e.t >= now - 5000);
|
|
34
|
+
const jankFrames = frames.filter((f) => f.janky);
|
|
35
|
+
const navEvents = recent.filter((e) => e.type === 'nav').slice(-1);
|
|
36
|
+
const currentRoute = navEvents.length > 0 && navEvents[0].type === 'nav' ? navEvents[0].route : '';
|
|
37
|
+
setStats({
|
|
38
|
+
jsFps: Math.min(60, 60 - stalls.length * 5),
|
|
39
|
+
uiFps: Math.min(60, frames.length),
|
|
40
|
+
stallCount: stalls.length,
|
|
41
|
+
jankFrameCount: jankFrames.length,
|
|
42
|
+
currentRoute,
|
|
43
|
+
});
|
|
44
|
+
}, UPDATE_INTERVAL_MS);
|
|
45
|
+
return () => { if (timerRef.current) clearInterval(timerRef.current); };
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
return stats;
|
|
49
|
+
}
|