@rn-perf-kit/overlay 0.1.0 → 0.1.2

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 ADDED
@@ -0,0 +1,19 @@
1
+ # @rn-perf-kit/overlay
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@rn-perf-kit/overlay.svg?style=flat-square&color=CB3837)](https://www.npmjs.com/package/@rn-perf-kit/overlay)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](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,11 +1,12 @@
1
1
  {
2
2
  "name": "@rn-perf-kit/overlay",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "In-app performance HUD overlay for rn-perf-kit",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
7
7
  "scripts": {
8
8
  "typecheck": "tsc --noEmit",
9
+ "prepublishOnly": "npm run typecheck",
9
10
  "lint": "eslint src/"
10
11
  },
11
12
  "peerDependencies": {
@@ -18,6 +19,24 @@
18
19
  "@types/react-native": "^0.73.0"
19
20
  },
20
21
  "dependencies": {
21
- "@rn-perf-kit/core": "^0.1.0"
22
- }
22
+ "@rn-perf-kit/core": "^0.1.2"
23
+ },
24
+ "author": "Arpit Kumar Sahu <aarpitkumarsahu99@gmail.com>",
25
+ "license": "MIT",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/arpitkumarsahu/rn-perf-kit.git",
29
+ "directory": "packages/overlay"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/arpitkumarsahu/rn-perf-kit/issues"
33
+ },
34
+ "homepage": "https://github.com/arpitkumarsahu/rn-perf-kit#readme",
35
+ "keywords": [
36
+ "react-native",
37
+ "hud",
38
+ "perf-overlay",
39
+ "fps-meter",
40
+ "in-app-hud"
41
+ ]
23
42
  }
@@ -2,26 +2,34 @@
2
2
  * PerfOverlay — in-app HUD showing live performance stats.
3
3
  */
4
4
  import React from 'react';
5
- import { View, Text, StyleSheet, type ViewStyle } from 'react-native';
5
+ import { View, Text, StyleSheet, Platform, StatusBar } from 'react-native';
6
6
  import { usePerfStats } from './usePerfStats';
7
7
 
8
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
- };
9
+ export interface PerfOverlayProps {
10
+ position?: Position;
11
+ disabled?: boolean;
12
+ insets?: { top: number; bottom: number }; // pass useSafeAreaInsets() from the app
13
+ }
17
14
 
18
- export function PerfOverlay({ position = 'top-right', disabled = false }: PerfOverlayProps): React.JSX.Element | null {
19
- const stats = usePerfStats();
15
+ export function PerfOverlay({
16
+ position = 'top-right',
17
+ disabled = false,
18
+ insets,
19
+ }: PerfOverlayProps): React.JSX.Element | null {
20
+ const stats = usePerfStats(!disabled);
20
21
  if (disabled) return null;
22
+
23
+ const top = (insets?.top ?? (Platform.OS === 'android' ? StatusBar.currentHeight ?? 24 : 47)) + 8;
24
+ const bottom = (insets?.bottom ?? 34) + 8;
25
+ const place = position.startsWith('top') ? { top } : { bottom };
26
+ const side = position.endsWith('right') ? { right: 8 } : { left: 8 };
27
+
21
28
  const jsFpsColor = stats.jsFps >= 55 ? '#4ade80' : stats.jsFps >= 30 ? '#facc15' : '#f87171';
22
29
  const uiFpsColor = stats.uiFps >= 55 ? '#4ade80' : stats.uiFps >= 30 ? '#facc15' : '#f87171';
30
+
23
31
  return (
24
- <View style={[styles.container, positionStyles[position]]}>
32
+ <View style={[styles.container, place, side]}>
25
33
  <View style={styles.row}>
26
34
  <Text style={styles.label}>JS </Text>
27
35
  <Text style={[styles.value, { color: jsFpsColor }]}>{stats.jsFps}fps</Text>
@@ -37,7 +45,9 @@ export function PerfOverlay({ position = 'top-right', disabled = false }: PerfOv
37
45
  )}
38
46
  {stats.currentRoute ? (
39
47
  <View style={styles.row}>
40
- <Text style={styles.route} numberOfLines={1}>{stats.currentRoute}</Text>
48
+ <Text style={styles.route} numberOfLines={1}>
49
+ {stats.currentRoute}
50
+ </Text>
41
51
  </View>
42
52
  ) : null}
43
53
  </View>
@@ -45,7 +55,14 @@ export function PerfOverlay({ position = 'top-right', disabled = false }: PerfOv
45
55
  }
46
56
 
47
57
  const styles = StyleSheet.create({
48
- container: { position: 'absolute', backgroundColor: 'rgba(0,0,0,0.75)', borderRadius: 8, padding: 8, minWidth: 90, zIndex: 9999 },
58
+ container: {
59
+ position: 'absolute',
60
+ backgroundColor: 'rgba(0,0,0,0.75)',
61
+ borderRadius: 8,
62
+ padding: 8,
63
+ minWidth: 90,
64
+ zIndex: 9999,
65
+ },
49
66
  row: { flexDirection: 'row', alignItems: 'center', marginBottom: 2 },
50
67
  label: { color: '#94a3b8', fontSize: 11, fontFamily: 'monospace' },
51
68
  value: { fontSize: 12, fontWeight: '700', fontFamily: 'monospace' },
package/src/index.ts CHANGED
@@ -1,4 +1,2 @@
1
1
  export { PerfOverlay } from './PerfOverlay';
2
2
  export { usePerfStats } from './usePerfStats';
3
-
4
- === PACKAGE B: packages/reporters ===
@@ -6,9 +6,6 @@ import { useState, useEffect, useRef } from 'react';
6
6
  import { timeline } from '@rn-perf-kit/core';
7
7
  import type { PerfEvent } from '@rn-perf-kit/core';
8
8
 
9
- const UPDATE_INTERVAL_MS = 500;
10
- const FPS_WINDOW_MS = 1000;
11
-
12
9
  export interface PerfStats {
13
10
  jsFps: number;
14
11
  uiFps: number;
@@ -17,33 +14,72 @@ export interface PerfStats {
17
14
  currentRoute: string;
18
15
  }
19
16
 
20
- export function usePerfStats(): PerfStats {
17
+ export function usePerfStats(enabled = true): PerfStats {
21
18
  const [stats, setStats] = useState<PerfStats>({
22
- jsFps: 60, uiFps: 60, stallCount: 0, jankFrameCount: 0, currentRoute: '',
19
+ jsFps: 60,
20
+ uiFps: 60,
21
+ stallCount: 0,
22
+ jankFrameCount: 0,
23
+ currentRoute: '',
23
24
  });
24
- const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
25
+
26
+ const routeRef = useRef('');
27
+
28
+ useEffect(() => {
29
+ return timeline.on('nav', (e) => {
30
+ if (e.type === 'nav') {
31
+ routeRef.current = e.route;
32
+ }
33
+ });
34
+ }, []);
25
35
 
26
36
  useEffect(() => {
27
- timerRef.current = setInterval(() => {
37
+ if (!enabled) return;
38
+
39
+ let rafFrames = 0;
40
+ let rafId = 0;
41
+ const loop = () => {
42
+ rafFrames++;
43
+ rafId = requestAnimationFrame(loop);
44
+ };
45
+ rafId = requestAnimationFrame(loop);
46
+
47
+ let refreshRateHz = 60;
48
+ try {
49
+ const { NativePerfKit } = require('@rn-perf-kit/native');
50
+ refreshRateHz = NativePerfKit?.getDeviceInfo?.()?.refreshRateHz ?? 60;
51
+ } catch {}
52
+
53
+ let last = timeline.now();
54
+ const iv = setInterval(() => {
28
55
  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
56
+ const elapsed = now - last;
57
+ const jsFps =
58
+ elapsed > 0 ? Math.min(refreshRateHz, Math.round((rafFrames * 1000) / elapsed)) : 60;
59
+ rafFrames = 0;
60
+ last = now;
61
+
62
+ // Native frames arrive in 250 ms batches, so count the window [now-1250, now-250]
63
+ const frameEvents = timeline.query(now - 1250, now - 250).filter(
64
+ (e): e is Extract<PerfEvent, { type: 'frame' }> => e.type === 'frame'
32
65
  );
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 : '';
66
+ const jankFrames = frameEvents.filter((f) => f.janky);
67
+ const stalls = timeline.recent(5000).filter((e) => e.type === 'stall');
68
+
37
69
  setStats({
38
- jsFps: Math.min(60, 60 - stalls.length * 5),
39
- uiFps: Math.min(60, frames.length),
70
+ jsFps,
71
+ uiFps: Math.min(refreshRateHz, frameEvents.length),
40
72
  stallCount: stalls.length,
41
73
  jankFrameCount: jankFrames.length,
42
- currentRoute,
74
+ currentRoute: routeRef.current,
43
75
  });
44
- }, UPDATE_INTERVAL_MS);
45
- return () => { if (timerRef.current) clearInterval(timerRef.current); };
46
- }, []);
76
+ }, 500);
77
+
78
+ return () => {
79
+ cancelAnimationFrame(rafId);
80
+ clearInterval(iv);
81
+ };
82
+ }, [enabled]);
47
83
 
48
84
  return stats;
49
85
  }