@onekeyfe/react-native-perf-stats 3.0.25

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 (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +64 -0
  3. package/ReactNativePerfStats.podspec +30 -0
  4. package/android/CMakeLists.txt +24 -0
  5. package/android/build.gradle +130 -0
  6. package/android/gradle.properties +4 -0
  7. package/android/src/main/AndroidManifest.xml +8 -0
  8. package/android/src/main/cpp/cpp-adapter.cpp +6 -0
  9. package/android/src/main/java/com/margelo/nitro/reactnativeperfstats/PerfStatsInitProvider.kt +43 -0
  10. package/android/src/main/java/com/margelo/nitro/reactnativeperfstats/ReactNativePerfStats.kt +514 -0
  11. package/android/src/main/java/com/margelo/nitro/reactnativeperfstats/ReactNativePerfStatsPackage.kt +24 -0
  12. package/ios/ReactNativePerfStats.swift +391 -0
  13. package/lib/module/ReactNativePerfStats.nitro.js +4 -0
  14. package/lib/module/ReactNativePerfStats.nitro.js.map +1 -0
  15. package/lib/module/index.js +6 -0
  16. package/lib/module/index.js.map +1 -0
  17. package/lib/module/package.json +1 -0
  18. package/lib/typescript/package.json +1 -0
  19. package/lib/typescript/src/ReactNativePerfStats.nitro.d.ts +51 -0
  20. package/lib/typescript/src/ReactNativePerfStats.nitro.d.ts.map +1 -0
  21. package/lib/typescript/src/index.d.ts +4 -0
  22. package/lib/typescript/src/index.d.ts.map +1 -0
  23. package/nitro.json +17 -0
  24. package/nitrogen/generated/android/c++/JHybridReactNativePerfStatsSpec.cpp +83 -0
  25. package/nitrogen/generated/android/c++/JHybridReactNativePerfStatsSpec.hpp +69 -0
  26. package/nitrogen/generated/android/c++/JPerfSample.hpp +65 -0
  27. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativeperfstats/HybridReactNativePerfStatsSpec.kt +74 -0
  28. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativeperfstats/PerfSample.kt +44 -0
  29. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativeperfstats/reactnativeperfstatsOnLoad.kt +35 -0
  30. package/nitrogen/generated/android/reactnativeperfstats+autolinking.cmake +81 -0
  31. package/nitrogen/generated/android/reactnativeperfstats+autolinking.gradle +27 -0
  32. package/nitrogen/generated/android/reactnativeperfstatsOnLoad.cpp +44 -0
  33. package/nitrogen/generated/android/reactnativeperfstatsOnLoad.hpp +25 -0
  34. package/nitrogen/generated/ios/ReactNativePerfStats+autolinking.rb +60 -0
  35. package/nitrogen/generated/ios/ReactNativePerfStats-Swift-Cxx-Bridge.cpp +49 -0
  36. package/nitrogen/generated/ios/ReactNativePerfStats-Swift-Cxx-Bridge.hpp +122 -0
  37. package/nitrogen/generated/ios/ReactNativePerfStats-Swift-Cxx-Umbrella.hpp +47 -0
  38. package/nitrogen/generated/ios/ReactNativePerfStatsAutolinking.mm +33 -0
  39. package/nitrogen/generated/ios/ReactNativePerfStatsAutolinking.swift +25 -0
  40. package/nitrogen/generated/ios/c++/HybridReactNativePerfStatsSpecSwift.cpp +11 -0
  41. package/nitrogen/generated/ios/c++/HybridReactNativePerfStatsSpecSwift.hpp +102 -0
  42. package/nitrogen/generated/ios/swift/Func_void_PerfSample.swift +47 -0
  43. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +47 -0
  44. package/nitrogen/generated/ios/swift/HybridReactNativePerfStatsSpec.swift +60 -0
  45. package/nitrogen/generated/ios/swift/HybridReactNativePerfStatsSpec_cxx.swift +182 -0
  46. package/nitrogen/generated/ios/swift/PerfSample.swift +58 -0
  47. package/nitrogen/generated/shared/c++/HybridReactNativePerfStatsSpec.cpp +25 -0
  48. package/nitrogen/generated/shared/c++/HybridReactNativePerfStatsSpec.hpp +68 -0
  49. package/nitrogen/generated/shared/c++/PerfSample.hpp +83 -0
  50. package/package.json +169 -0
  51. package/src/ReactNativePerfStats.nitro.ts +54 -0
  52. package/src/index.tsx +8 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 OneKey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # @onekeyfe/react-native-perf-stats
2
+
3
+ Native CPU and RAM sampler for React Native, with an optional draggable on-screen overlay. Built on [Nitro Modules](https://nitro.margelo.com/) — the sampler runs on a dedicated background thread so the overlay keeps updating even when the JS thread is frozen.
4
+
5
+ - **Android**: HandlerThread + `/proc/self/stat` (CPU ticks) + `/proc/self/statm` (RSS); overlay attached via `WindowManager` to the current Activity (no `SYSTEM_ALERT_WINDOW` permission).
6
+ - **iOS**: GCD dispatch source + `task_info` (`phys_footprint`); overlay added as a `UILabel` on the key `UIWindow`.
7
+
8
+ ## Installation
9
+
10
+ ```sh
11
+ yarn add @onekeyfe/react-native-perf-stats react-native-nitro-modules
12
+ ```
13
+
14
+ `react-native-nitro-modules` is a required peer dependency.
15
+
16
+ ## Usage
17
+
18
+ ```ts
19
+ import { ReactNativePerfStats } from '@onekeyfe/react-native-perf-stats';
20
+
21
+ // Start sampling at 1 Hz. Idempotent — calling again just updates the interval.
22
+ ReactNativePerfStats.start(1000);
23
+
24
+ // Show the floating overlay (drag to reposition).
25
+ ReactNativePerfStats.showOverlay();
26
+
27
+ // One-shot read without touching the overlay timer.
28
+ const sample = await ReactNativePerfStats.sample();
29
+ console.log(sample); // { cpu: 12.3, rss: 187654144, timestamp: 1730000000000 }
30
+
31
+ // Tear down.
32
+ ReactNativePerfStats.hideOverlay();
33
+ ReactNativePerfStats.stop();
34
+ ```
35
+
36
+ ### `PerfSample`
37
+
38
+ | field | unit | notes |
39
+ | ----------- | -------------------------- | ------------------------------------------------------------------------------------------------------ |
40
+ | `cpu` | percent of one core | `(Δcpu / Δwall) * 100`. May exceed 100 on multi-core saturation. First sample after launch is `0`. |
41
+ | `rss` | bytes | iOS `phys_footprint`, Android `VmRSS`. |
42
+ | `timestamp` | ms since unix epoch | Wall-clock at sample time. |
43
+
44
+ ### API
45
+
46
+ - `start(intervalMs: number): void` — minimum interval is clamped to 200 ms.
47
+ - `stop(): void` — also hides the overlay.
48
+ - `showOverlay(): void` / `hideOverlay(): void` — overlay shows `--` until `start` runs.
49
+ - `sample(): Promise<PerfSample>` — runs off the JS thread, shares baseline with the overlay sampler.
50
+
51
+ ### Anomaly logging
52
+
53
+ While the sampler is running, the native side emits a warn to `@onekeyfe/react-native-native-logger` (`OneKeyLog.warn`, tag `PerfStats`) whenever a metric stays over its threshold for **5 consecutive samples**. Each category has an independent **30 s cooldown** so a sustained spike produces one log every 30 s rather than one per sample.
54
+
55
+ | metric | threshold |
56
+ | ------ | --------- |
57
+ | CPU | ≥ 150 % |
58
+ | RSS | ≥ 800 MB |
59
+
60
+ One-off `sample()` calls do **not** trip this path — only the periodic timer started by `start()` does.
61
+
62
+ ## License
63
+
64
+ MIT
@@ -0,0 +1,30 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "ReactNativePerfStats"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => min_ios_version_supported }
14
+ s.source = { :git => "https://github.com/OneKeyHQ/app-modules/react-native-perf-stats.git", :tag => "#{s.version}" }
15
+
16
+ s.source_files = [
17
+ "ios/**/*.{swift}",
18
+ "ios/**/*.{m,mm}",
19
+ "cpp/**/*.{hpp,cpp}",
20
+ ]
21
+
22
+ s.dependency 'React-jsi'
23
+ s.dependency 'React-callinvoker'
24
+ s.dependency 'ReactNativeNativeLogger'
25
+
26
+ load 'nitrogen/generated/ios/ReactNativePerfStats+autolinking.rb'
27
+ add_nitrogen_files(s)
28
+
29
+ install_modules_dependencies(s)
30
+ end
@@ -0,0 +1,24 @@
1
+ project(reactnativeperfstats)
2
+ cmake_minimum_required(VERSION 3.9.0)
3
+
4
+ set(PACKAGE_NAME reactnativeperfstats)
5
+ set(CMAKE_VERBOSE_MAKEFILE ON)
6
+ set(CMAKE_CXX_STANDARD 20)
7
+
8
+ # Define C++ library and add all sources
9
+ add_library(${PACKAGE_NAME} SHARED src/main/cpp/cpp-adapter.cpp)
10
+
11
+ # Add Nitrogen specs :)
12
+ include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/reactnativeperfstats+autolinking.cmake)
13
+
14
+ # Set up local includes
15
+ include_directories("src/main/cpp" "../cpp")
16
+
17
+ find_library(LOG_LIB log)
18
+
19
+ # Link all libraries together
20
+ target_link_libraries(
21
+ ${PACKAGE_NAME}
22
+ ${LOG_LIB}
23
+ android # <-- Android core
24
+ )
@@ -0,0 +1,130 @@
1
+ buildscript {
2
+ ext.getExtOrDefault = {name ->
3
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['ReactNativePerfStats_' + name]
4
+ }
5
+
6
+ repositories {
7
+ google()
8
+ mavenCentral()
9
+ }
10
+
11
+ dependencies {
12
+ classpath "com.android.tools.build:gradle:8.7.2"
13
+ // noinspection DifferentKotlinGradleVersion
14
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
15
+ }
16
+ }
17
+
18
+ def reactNativeArchitectures() {
19
+ def value = rootProject.getProperties().get("reactNativeArchitectures")
20
+ return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
21
+ }
22
+
23
+ apply plugin: "com.android.library"
24
+ apply plugin: "kotlin-android"
25
+ apply from: '../nitrogen/generated/android/reactnativeperfstats+autolinking.gradle'
26
+
27
+ apply plugin: "com.facebook.react"
28
+
29
+ def getExtOrIntegerDefault(name) {
30
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ReactNativePerfStats_" + name]).toInteger()
31
+ }
32
+
33
+ android {
34
+ namespace "com.margelo.nitro.reactnativeperfstats"
35
+
36
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
37
+
38
+ defaultConfig {
39
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
40
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
41
+
42
+ externalNativeBuild {
43
+ cmake {
44
+ cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
45
+ arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
46
+ abiFilters (*reactNativeArchitectures())
47
+
48
+ buildTypes {
49
+ debug {
50
+ cppFlags "-O1 -g"
51
+ }
52
+ release {
53
+ cppFlags "-O2"
54
+ }
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ externalNativeBuild {
61
+ cmake {
62
+ path "CMakeLists.txt"
63
+ }
64
+ }
65
+
66
+ packagingOptions {
67
+ excludes = [
68
+ "META-INF",
69
+ "META-INF/**",
70
+ "**/libc++_shared.so",
71
+ "**/libfbjni.so",
72
+ "**/libjsi.so",
73
+ "**/libfolly_json.so",
74
+ "**/libfolly_runtime.so",
75
+ "**/libglog.so",
76
+ "**/libhermes.so",
77
+ "**/libhermes-executor-debug.so",
78
+ "**/libhermes_executor.so",
79
+ "**/libreactnative.so",
80
+ "**/libreactnativejni.so",
81
+ "**/libturbomodulejsijni.so",
82
+ "**/libreact_nativemodule_core.so",
83
+ "**/libjscexecutor.so"
84
+ ]
85
+ }
86
+
87
+ buildFeatures {
88
+ buildConfig true
89
+ prefab true
90
+ }
91
+
92
+ buildTypes {
93
+ release {
94
+ minifyEnabled false
95
+ }
96
+ }
97
+
98
+ lintOptions {
99
+ disable "GradleCompatible"
100
+ }
101
+
102
+ compileOptions {
103
+ sourceCompatibility JavaVersion.VERSION_1_8
104
+ targetCompatibility JavaVersion.VERSION_1_8
105
+ }
106
+
107
+ sourceSets {
108
+ main {
109
+ java.srcDirs += [
110
+ "generated/java",
111
+ "generated/jni"
112
+ ]
113
+ }
114
+ }
115
+ }
116
+
117
+ repositories {
118
+ mavenCentral()
119
+ google()
120
+ }
121
+
122
+ def kotlin_version = getExtOrDefault("kotlinVersion")
123
+
124
+ dependencies {
125
+ implementation "com.facebook.react:react-android"
126
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
127
+ implementation project(":react-native-nitro-modules")
128
+
129
+ implementation project(":onekeyfe_react-native-native-logger")
130
+ }
@@ -0,0 +1,4 @@
1
+ ReactNativePerfStats_kotlinVersion=1.9.25
2
+ ReactNativePerfStats_compileSdkVersion=35
3
+ ReactNativePerfStats_targetSdkVersion=35
4
+ ReactNativePerfStats_minSdkVersion=24
@@ -0,0 +1,8 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ <application>
3
+ <provider
4
+ android:name=".PerfStatsInitProvider"
5
+ android:authorities="${applicationId}.perfstats-init"
6
+ android:exported="false" />
7
+ </application>
8
+ </manifest>
@@ -0,0 +1,6 @@
1
+ #include <jni.h>
2
+ #include "reactnativeperfstatsOnLoad.hpp"
3
+
4
+ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
5
+ return margelo::nitro::reactnativeperfstats::initialize(vm);
6
+ }
@@ -0,0 +1,43 @@
1
+ package com.margelo.nitro.reactnativeperfstats
2
+
3
+ import android.app.Application
4
+ import android.content.ContentProvider
5
+ import android.content.ContentValues
6
+ import android.database.Cursor
7
+ import android.net.Uri
8
+
9
+ /**
10
+ * Auto-initialises [Overlay] before Application.onCreate().
11
+ *
12
+ * ContentProvider.onCreate() runs between Application.attachBaseContext()
13
+ * and Application.onCreate(), so registering ActivityLifecycleCallbacks
14
+ * here guarantees we capture the launcher Activity's first onResumed
15
+ * event. Without this hook, JS code calling showOverlay() after the React
16
+ * tree mounts would arrive too late and `currentActivity` would stay null.
17
+ */
18
+ class PerfStatsInitProvider : ContentProvider() {
19
+
20
+ override fun onCreate(): Boolean {
21
+ val app = context?.applicationContext as? Application ?: return true
22
+ Overlay.bootstrap(app)
23
+ return true
24
+ }
25
+
26
+ override fun query(
27
+ uri: Uri,
28
+ projection: Array<String>?,
29
+ selection: String?,
30
+ selectionArgs: Array<String>?,
31
+ sortOrder: String?,
32
+ ): Cursor? = null
33
+
34
+ override fun getType(uri: Uri): String? = null
35
+ override fun insert(uri: Uri, values: ContentValues?): Uri? = null
36
+ override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int = 0
37
+ override fun update(
38
+ uri: Uri,
39
+ values: ContentValues?,
40
+ selection: String?,
41
+ selectionArgs: Array<String>?,
42
+ ): Int = 0
43
+ }