@rn-perf-kit/native 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/RnPerfKit.podspec +23 -0
- package/android/build.gradle +47 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/java/com/rnperfkit/RnPerfKitModule.kt +115 -0
- package/android/src/main/java/com/rnperfkit/RnPerfKitPackage.kt +21 -0
- package/app.plugin.js +22 -0
- package/ios/RnPerfKit.h +11 -0
- package/ios/RnPerfKit.mm +187 -0
- package/package.json +19 -4
- package/react-native.config.js +12 -0
- package/src/NativePerfKit.ts +56 -0
- package/src/device-tier.ts +23 -0
- package/src/frame-collector.ts +33 -0
- package/src/index.ts +4 -0
- package/src/stall-detector.ts +35 -0
- package/tsconfig.json +5 -0
- package/README.md +0 -3
|
@@ -0,0 +1,23 @@
|
|
|
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 = "RnPerfKit"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = "https://github.com/rn-perf-kit/rn-perf-kit"
|
|
10
|
+
s.license = "MIT"
|
|
11
|
+
s.authors = { "rn-perf-kit team" => "contact@rn-perf-kit.dev" }
|
|
12
|
+
s.platforms = { :ios => "13.4" }
|
|
13
|
+
s.source = { :git => "https://github.com/rn-perf-kit/rn-perf-kit.git", :tag => "#{s.version}" }
|
|
14
|
+
|
|
15
|
+
s.source_files = "ios/**/*.{h,m,mm,swift}"
|
|
16
|
+
|
|
17
|
+
# React Native New Architecture dependencies
|
|
18
|
+
if respond_to?(:install_modules_dependencies, true)
|
|
19
|
+
install_modules_dependencies(s)
|
|
20
|
+
else
|
|
21
|
+
s.dependency "React-Core"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
apply plugin: 'com.android.library'
|
|
2
|
+
apply plugin: 'kotlin-android'
|
|
3
|
+
|
|
4
|
+
def getExtOrDefault(name, defaultValue) {
|
|
5
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
android {
|
|
9
|
+
compileSdkVersion getExtOrDefault('compileSdkVersion', 34)
|
|
10
|
+
|
|
11
|
+
defaultConfig {
|
|
12
|
+
minSdkVersion getExtOrDefault('minSdkVersion', 24)
|
|
13
|
+
targetSdkVersion getExtOrDefault('targetSdkVersion', 34)
|
|
14
|
+
versionCode 1
|
|
15
|
+
versionName "0.1.0"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
buildTypes {
|
|
19
|
+
release {
|
|
20
|
+
minifyEnabled false
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
lintOptions {
|
|
25
|
+
abortOnError false
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
compileOptions {
|
|
29
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
30
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
kotlinOptions {
|
|
34
|
+
jvmTarget = "1.8"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
dependencies {
|
|
39
|
+
// React Native dependency
|
|
40
|
+
implementation "com.facebook.react:react-android:+"
|
|
41
|
+
|
|
42
|
+
// AndroidX JankStats for frame performance & jank detection
|
|
43
|
+
implementation "androidx.metrics:metrics-performance:1.0.0-beta01"
|
|
44
|
+
|
|
45
|
+
// Kotlin stdlib
|
|
46
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
47
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
package com.rnperfkit
|
|
2
|
+
|
|
3
|
+
import android.app.ActivityManager
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.os.Build
|
|
6
|
+
import android.os.PowerManager
|
|
7
|
+
import android.os.SystemClock
|
|
8
|
+
import android.util.Log
|
|
9
|
+
import com.facebook.react.bridge.*
|
|
10
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
11
|
+
|
|
12
|
+
@ReactModule(name = RnPerfKitModule.NAME)
|
|
13
|
+
class RnPerfKitModule(reactContext: ReactApplicationContext) :
|
|
14
|
+
NativeRnPerfKitSpec(reactContext) {
|
|
15
|
+
|
|
16
|
+
companion object {
|
|
17
|
+
const val NAME = "RnPerfKit"
|
|
18
|
+
private const val TAG = "RnPerfKit"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
override fun getName(): String = NAME
|
|
22
|
+
|
|
23
|
+
override fun nativeNow(): Double = SystemClock.elapsedRealtime().toDouble()
|
|
24
|
+
|
|
25
|
+
override fun getDeviceInfo(): WritableMap {
|
|
26
|
+
val ctx = reactApplicationContext
|
|
27
|
+
val am = ctx.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
|
28
|
+
val mi = ActivityManager.MemoryInfo()
|
|
29
|
+
am.getMemoryInfo(mi)
|
|
30
|
+
val totalRamMb = (mi.totalMem / 1024 / 1024).toInt()
|
|
31
|
+
val cpuCores = Runtime.getRuntime().availableProcessors()
|
|
32
|
+
val isLowRam = am.isLowRamDevice
|
|
33
|
+
val mediaPerf = if (Build.VERSION.SDK_INT >= 31) Build.VERSION.MEDIA_PERFORMANCE_CLASS else -1
|
|
34
|
+
val refreshRate = if (Build.VERSION.SDK_INT >= 30) ctx.display?.refreshRate?.toInt() ?: 60 else 60
|
|
35
|
+
val tier = when {
|
|
36
|
+
isLowRam || totalRamMb < 3000 || cpuCores <= 4 -> "LOW"
|
|
37
|
+
totalRamMb >= 6000 && cpuCores >= 8 && mediaPerf >= 31 -> "HIGH"
|
|
38
|
+
else -> "MID"
|
|
39
|
+
}
|
|
40
|
+
return Arguments.createMap().apply {
|
|
41
|
+
putString("tier", tier)
|
|
42
|
+
putInt("totalRamMb", totalRamMb)
|
|
43
|
+
putInt("cpuCores", cpuCores)
|
|
44
|
+
putInt("refreshRateHz", refreshRate)
|
|
45
|
+
putBoolean("isLowRamDevice", isLowRam)
|
|
46
|
+
putString("os", "android")
|
|
47
|
+
putString("osVersion", Build.VERSION.RELEASE)
|
|
48
|
+
if (mediaPerf >= 0) putInt("mediaPerformanceClass", mediaPerf)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
override fun getMemorySnapshot(): WritableMap {
|
|
53
|
+
val ctx = reactApplicationContext
|
|
54
|
+
val am = ctx.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
|
55
|
+
val mi = ActivityManager.MemoryInfo()
|
|
56
|
+
am.getMemoryInfo(mi)
|
|
57
|
+
return Arguments.createMap().apply {
|
|
58
|
+
putDouble("t", nativeNow())
|
|
59
|
+
putInt("usedMb", ((mi.totalMem - mi.availMem) / 1024 / 1024).toInt())
|
|
60
|
+
putInt("availableMb", (mi.availMem / 1024 / 1024).toInt())
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
override fun getThermalStatus(): WritableMap {
|
|
65
|
+
val status = if (Build.VERSION.SDK_INT >= 29) {
|
|
66
|
+
val pm = reactApplicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
67
|
+
when (pm.currentThermalStatus) {
|
|
68
|
+
PowerManager.THERMAL_STATUS_NONE -> "none"
|
|
69
|
+
PowerManager.THERMAL_STATUS_LIGHT -> "light"
|
|
70
|
+
PowerManager.THERMAL_STATUS_MODERATE -> "moderate"
|
|
71
|
+
PowerManager.THERMAL_STATUS_SEVERE -> "severe"
|
|
72
|
+
PowerManager.THERMAL_STATUS_CRITICAL -> "critical"
|
|
73
|
+
PowerManager.THERMAL_STATUS_EMERGENCY -> "emergency"
|
|
74
|
+
PowerManager.THERMAL_STATUS_SHUTDOWN -> "shutdown"
|
|
75
|
+
else -> "none"
|
|
76
|
+
}
|
|
77
|
+
} else "none"
|
|
78
|
+
return Arguments.createMap().apply {
|
|
79
|
+
putDouble("t", nativeNow())
|
|
80
|
+
putString("status", status)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private var frameCallback: Callback? = null
|
|
85
|
+
|
|
86
|
+
override fun startFrameCollection(onFrame: Callback) {
|
|
87
|
+
frameCallback = onFrame
|
|
88
|
+
Log.d(TAG, "startFrameCollection: attach JankStats via RnPerfKitActivity")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
override fun stopFrameCollection() {
|
|
92
|
+
frameCallback = null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
override fun startMemoryMonitoring(onTrim: Callback) {
|
|
96
|
+
Log.d(TAG, "startMemoryMonitoring: stub")
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
override fun stopMemoryMonitoring() {}
|
|
100
|
+
|
|
101
|
+
override fun startThermalMonitoring(onThermal: Callback) {
|
|
102
|
+
Log.d(TAG, "startThermalMonitoring: stub")
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
override fun stopThermalMonitoring() {}
|
|
106
|
+
|
|
107
|
+
override fun writeSessionFile(path: String, json: String, promise: Promise) {
|
|
108
|
+
try {
|
|
109
|
+
java.io.File(path).writeText(json)
|
|
110
|
+
promise.resolve(null)
|
|
111
|
+
} catch (e: Exception) {
|
|
112
|
+
promise.reject("WRITE_ERROR", e.message, e)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
package com.rnperfkit
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.TurboReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
|
|
9
|
+
class RnPerfKitPackage : TurboReactPackage() {
|
|
10
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? =
|
|
11
|
+
if (name == RnPerfKitModule.NAME) RnPerfKitModule(reactContext) else null
|
|
12
|
+
|
|
13
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider {
|
|
14
|
+
mapOf(
|
|
15
|
+
RnPerfKitModule.NAME to ReactModuleInfo(
|
|
16
|
+
RnPerfKitModule.NAME, RnPerfKitModule.NAME,
|
|
17
|
+
false, false, false, true
|
|
18
|
+
)
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
}
|
package/app.plugin.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expo Config Plugin for rn-perf-kit native module.
|
|
3
|
+
* Enables zero-config setup in managed and bare Expo projects.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { withDangerousMod, withPlugins } = require('@expo/config-plugins');
|
|
7
|
+
|
|
8
|
+
function withRnPerfKitAndroid(config) {
|
|
9
|
+
return withDangerousMod(config, [
|
|
10
|
+
'android',
|
|
11
|
+
async (modConfig) => {
|
|
12
|
+
// Custom Android project modifications if required
|
|
13
|
+
return modConfig;
|
|
14
|
+
},
|
|
15
|
+
]);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function withRnPerfKit(config) {
|
|
19
|
+
return withPlugins(config, [withRnPerfKitAndroid]);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = withRnPerfKit;
|
package/ios/RnPerfKit.h
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#import <React/RCTBridgeModule.h>
|
|
2
|
+
#import <React/RCTEventEmitter.h>
|
|
3
|
+
|
|
4
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
5
|
+
#import <RnPerfKitSpec/RnPerfKitSpec.h>
|
|
6
|
+
@interface RnPerfKit : NSObject <NativeRnPerfKitSpec>
|
|
7
|
+
#else
|
|
8
|
+
@interface RnPerfKit : NSObject <RCTBridgeModule>
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
@end
|
package/ios/RnPerfKit.mm
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
#import "RnPerfKit.h"
|
|
2
|
+
#import <QuartzCore/QuartzCore.h>
|
|
3
|
+
#import <mach/mach.h>
|
|
4
|
+
#import <sys/utsname.h>
|
|
5
|
+
|
|
6
|
+
@implementation RnPerfKit {
|
|
7
|
+
CADisplayLink *_displayLink;
|
|
8
|
+
RCTResponseSenderBlock _frameCallback;
|
|
9
|
+
CFTimeInterval _lastFrameTimestamp;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
RCT_EXPORT_MODULE(RnPerfKit)
|
|
13
|
+
|
|
14
|
+
+ (BOOL)requiresMainQueueSetup {
|
|
15
|
+
return YES;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ── Clock Synchronization ───────────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
RCT_EXPORT_METHOD(nativeNow:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
21
|
+
// CACurrentMediaTime() returns seconds since boot with microsecond resolution.
|
|
22
|
+
double ms = CACurrentMediaTime() * 1000.0;
|
|
23
|
+
resolve(@(ms));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
- (NSNumber *)nativeNowSync {
|
|
27
|
+
return @(CACurrentMediaTime() * 1000.0);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ── Device Information & Tier ───────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
RCT_EXPORT_METHOD(getDeviceInfo:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
33
|
+
uint64_t physicalMemory = [NSProcessInfo processInfo].physicalMemory;
|
|
34
|
+
int totalRamMb = (int)(physicalMemory / 1024 / 1024);
|
|
35
|
+
int cpuCores = (int)[[NSProcessInfo processInfo] activeProcessorCount];
|
|
36
|
+
|
|
37
|
+
UIScreen *mainScreen = [UIScreen mainScreen];
|
|
38
|
+
int refreshRateHz = 60;
|
|
39
|
+
if (@available(iOS 10.3, *)) {
|
|
40
|
+
refreshRateHz = (int)mainScreen.maximumFramesPerSecond;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Device tier classification:
|
|
44
|
+
// LOW: <= 3GB RAM (older iPhones e.g. iPhone 8, XR, SE 2nd gen)
|
|
45
|
+
// HIGH: >= 6GB RAM (iPhone 12 Pro, 13 Pro, 14 Pro, 15, etc.)
|
|
46
|
+
// MID: 4GB RAM (iPhone 11, 12, 13 base)
|
|
47
|
+
NSString *tier = @"MID";
|
|
48
|
+
if (totalRamMb <= 3200) {
|
|
49
|
+
tier = @"LOW";
|
|
50
|
+
} else if (totalRamMb >= 5800) {
|
|
51
|
+
tier = @"HIGH";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
resolve(@{
|
|
55
|
+
@"tier": tier,
|
|
56
|
+
@"totalRamMb": @(totalRamMb),
|
|
57
|
+
@"cpuCores": @(cpuCores),
|
|
58
|
+
@"refreshRateHz": @(refreshRateHz),
|
|
59
|
+
@"isLowRamDevice": totalRamMb <= 3200 ? @YES : @NO,
|
|
60
|
+
@"os": @"ios",
|
|
61
|
+
@"osVersion": [[UIDevice currentDevice] systemVersion]
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ── Memory Monitoring ───────────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
RCT_EXPORT_METHOD(getMemorySnapshot:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
68
|
+
struct mach_task_basic_info info;
|
|
69
|
+
mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
|
|
70
|
+
|
|
71
|
+
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count) == KERN_SUCCESS) {
|
|
72
|
+
int usedMb = (int)(info.resident_size / 1024 / 1024);
|
|
73
|
+
uint64_t totalRam = [NSProcessInfo processInfo].physicalMemory;
|
|
74
|
+
int totalMb = (int)(totalRam / 1024 / 1024);
|
|
75
|
+
int availableMb = MAX(0, totalMb - usedMb);
|
|
76
|
+
|
|
77
|
+
resolve(@{
|
|
78
|
+
@"t": @(CACurrentMediaTime() * 1000.0),
|
|
79
|
+
@"usedMb": @(usedMb),
|
|
80
|
+
@"availableMb": @(availableMb)
|
|
81
|
+
});
|
|
82
|
+
} else {
|
|
83
|
+
reject(@"MEMORY_ERROR", @"Unable to retrieve task_info", nil);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ── Thermal Monitoring ──────────────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
RCT_EXPORT_METHOD(getThermalStatus:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
|
|
90
|
+
NSProcessInfoThermalState state = [NSProcessInfo processInfo].thermalState;
|
|
91
|
+
NSString *statusString = @"none";
|
|
92
|
+
|
|
93
|
+
switch (state) {
|
|
94
|
+
case NSProcessInfoThermalStateNominal:
|
|
95
|
+
statusString = @"none";
|
|
96
|
+
break;
|
|
97
|
+
case NSProcessInfoThermalStateFair:
|
|
98
|
+
statusString = @"light";
|
|
99
|
+
break;
|
|
100
|
+
case NSProcessInfoThermalStateSerious:
|
|
101
|
+
statusString = @"severe";
|
|
102
|
+
break;
|
|
103
|
+
case NSProcessInfoThermalStateCritical:
|
|
104
|
+
statusString = @"critical";
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
resolve(@{
|
|
109
|
+
@"t": @(CACurrentMediaTime() * 1000.0),
|
|
110
|
+
@"status": statusString
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ── CADisplayLink Frame Monitoring ──────────────────────────────────────────
|
|
115
|
+
|
|
116
|
+
RCT_EXPORT_METHOD(startFrameCollection:(RCTResponseSenderBlock)onFrame) {
|
|
117
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
118
|
+
self->_frameCallback = onFrame;
|
|
119
|
+
if (!self->_displayLink) {
|
|
120
|
+
self->_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onDisplayLinkTick:)];
|
|
121
|
+
[self->_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
|
|
122
|
+
self->_lastFrameTimestamp = 0;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
RCT_EXPORT_METHOD(stopFrameCollection) {
|
|
128
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
129
|
+
if (self->_displayLink) {
|
|
130
|
+
[self->_displayLink invalidate];
|
|
131
|
+
self->_displayLink = nil;
|
|
132
|
+
}
|
|
133
|
+
self->_frameCallback = nil;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
- (void)onDisplayLinkTick:(CADisplayLink *)link {
|
|
138
|
+
CFTimeInterval current = link.timestamp;
|
|
139
|
+
if (_lastFrameTimestamp > 0) {
|
|
140
|
+
CFTimeInterval duration = current - _lastFrameTimestamp;
|
|
141
|
+
double durationMs = duration * 1000.0;
|
|
142
|
+
CFTimeInterval targetDuration = link.targetTimestamp - current;
|
|
143
|
+
double targetDurationMs = (targetDuration > 0 ? targetDuration : (1.0 / 60.0)) * 1000.0;
|
|
144
|
+
|
|
145
|
+
// Jank threshold: frame duration > 1.5x expected vsync budget
|
|
146
|
+
BOOL isJanky = durationMs > (targetDurationMs * 1.5);
|
|
147
|
+
|
|
148
|
+
if (_frameCallback) {
|
|
149
|
+
_frameCallback(@[@{
|
|
150
|
+
@"t": @(current * 1000.0),
|
|
151
|
+
@"dur": @(durationMs),
|
|
152
|
+
@"janky": @(isJanky)
|
|
153
|
+
}]);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
_lastFrameTimestamp = current;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// ── File Export ─────────────────────────────────────────────────────────────
|
|
160
|
+
|
|
161
|
+
RCT_EXPORT_METHOD(writeSessionFile:(NSString *)path
|
|
162
|
+
json:(NSString *)json
|
|
163
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
164
|
+
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
165
|
+
NSError *error = nil;
|
|
166
|
+
BOOL success = [json writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
|
|
167
|
+
if (success) {
|
|
168
|
+
resolve(nil);
|
|
169
|
+
} else {
|
|
170
|
+
reject(@"WRITE_ERROR", error.localizedDescription, error);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Stubs for TurboModule compliance
|
|
175
|
+
RCT_EXPORT_METHOD(startMemoryMonitoring:(RCTResponseSenderBlock)onTrim) {}
|
|
176
|
+
RCT_EXPORT_METHOD(stopMemoryMonitoring) {}
|
|
177
|
+
RCT_EXPORT_METHOD(startThermalMonitoring:(RCTResponseSenderBlock)onThermal) {}
|
|
178
|
+
RCT_EXPORT_METHOD(stopThermalMonitoring) {}
|
|
179
|
+
|
|
180
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
181
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
182
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params {
|
|
183
|
+
return std::make_shared<facebook::react::NativeRnPerfKitSpecJSI>(params);
|
|
184
|
+
}
|
|
185
|
+
#endif
|
|
186
|
+
|
|
187
|
+
@end
|
package/package.json
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rn-perf-kit/native",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Native TurboModule for rn-perf-kit: frames, memory, thermal, device tier",
|
|
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-native": ">=0.73.0"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"typescript": "^5.3.0",
|
|
16
|
+
"@types/react-native": "^0.73.0"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@rn-perf-kit/core": "^0.1.0"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TurboModule spec for rn-perf-kit native bridge.
|
|
3
|
+
*/
|
|
4
|
+
import type { TurboModule } from 'react-native';
|
|
5
|
+
import { TurboModuleRegistry } from 'react-native';
|
|
6
|
+
|
|
7
|
+
export interface DeviceInfo {
|
|
8
|
+
tier: 'LOW' | 'MID' | 'HIGH';
|
|
9
|
+
totalRamMb: number;
|
|
10
|
+
cpuCores: number;
|
|
11
|
+
refreshRateHz: number;
|
|
12
|
+
isLowRamDevice: boolean;
|
|
13
|
+
os: 'android' | 'ios';
|
|
14
|
+
osVersion: string;
|
|
15
|
+
mediaPerformanceClass?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface FrameEvent {
|
|
19
|
+
t: number;
|
|
20
|
+
dur: number;
|
|
21
|
+
janky: boolean;
|
|
22
|
+
inputDur?: number;
|
|
23
|
+
animationDur?: number;
|
|
24
|
+
layoutDur?: number;
|
|
25
|
+
drawDur?: number;
|
|
26
|
+
syncDur?: number;
|
|
27
|
+
gpuDur?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ThermalEvent {
|
|
31
|
+
t: number;
|
|
32
|
+
status: 'none' | 'light' | 'moderate' | 'severe' | 'critical' | 'emergency' | 'shutdown';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface MemorySnapshot {
|
|
36
|
+
t: number;
|
|
37
|
+
usedMb: number;
|
|
38
|
+
availableMb: number;
|
|
39
|
+
trimLevel?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface Spec extends TurboModule {
|
|
43
|
+
nativeNow(): number;
|
|
44
|
+
getDeviceInfo(): DeviceInfo;
|
|
45
|
+
getMemorySnapshot(): MemorySnapshot;
|
|
46
|
+
getThermalStatus(): ThermalEvent;
|
|
47
|
+
startFrameCollection(onFrame: (event: FrameEvent) => void): void;
|
|
48
|
+
stopFrameCollection(): void;
|
|
49
|
+
startMemoryMonitoring(onTrim: (level: number) => void): void;
|
|
50
|
+
stopMemoryMonitoring(): void;
|
|
51
|
+
startThermalMonitoring(onThermal: (event: ThermalEvent) => void): void;
|
|
52
|
+
stopThermalMonitoring(): void;
|
|
53
|
+
writeSessionFile(path: string, json: string): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default TurboModuleRegistry.getEnforcing<Spec>('RnPerfKit');
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Device tier classification: LOW / MID / HIGH
|
|
3
|
+
*/
|
|
4
|
+
import type { DeviceInfo } from './NativePerfKit';
|
|
5
|
+
import type { DeviceTier } from '@rn-perf-kit/core';
|
|
6
|
+
|
|
7
|
+
export function classifyDeviceTier(info: DeviceInfo): DeviceTier {
|
|
8
|
+
if (info.isLowRamDevice || info.totalRamMb < 3000 || info.cpuCores <= 4) {
|
|
9
|
+
return 'LOW';
|
|
10
|
+
}
|
|
11
|
+
if (
|
|
12
|
+
info.totalRamMb >= 6000 &&
|
|
13
|
+
info.cpuCores >= 8 &&
|
|
14
|
+
(info.mediaPerformanceClass ?? 0) >= 31
|
|
15
|
+
) {
|
|
16
|
+
return 'HIGH';
|
|
17
|
+
}
|
|
18
|
+
return 'MID';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function frameBudgetMs(refreshRateHz: number): number {
|
|
22
|
+
return 1000 / refreshRateHz;
|
|
23
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridges native FrameMetrics/JankStats (Android) and CADisplayLink (iOS)
|
|
3
|
+
* into the core timeline as 'frame' events.
|
|
4
|
+
*/
|
|
5
|
+
import { timeline, perfClock } from '@rn-perf-kit/core';
|
|
6
|
+
import NativePerfKit, { type FrameEvent } from './NativePerfKit';
|
|
7
|
+
|
|
8
|
+
export function startFrameCollection(): void {
|
|
9
|
+
NativePerfKit.startFrameCollection((nativeEvent: FrameEvent) => {
|
|
10
|
+
const t = perfClock.nativeToJs(nativeEvent.t);
|
|
11
|
+
timeline.add({
|
|
12
|
+
type: 'frame',
|
|
13
|
+
t,
|
|
14
|
+
dur: nativeEvent.dur,
|
|
15
|
+
janky: nativeEvent.janky,
|
|
16
|
+
breakdown:
|
|
17
|
+
nativeEvent.inputDur != null
|
|
18
|
+
? {
|
|
19
|
+
input: nativeEvent.inputDur ?? 0,
|
|
20
|
+
animation: nativeEvent.animationDur ?? 0,
|
|
21
|
+
layoutMeasure: nativeEvent.layoutDur ?? 0,
|
|
22
|
+
draw: nativeEvent.drawDur ?? 0,
|
|
23
|
+
sync: nativeEvent.syncDur ?? 0,
|
|
24
|
+
gpu: nativeEvent.gpuDur ?? 0,
|
|
25
|
+
}
|
|
26
|
+
: undefined,
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function stopFrameCollection(): void {
|
|
32
|
+
NativePerfKit.stopFrameCollection();
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JS thread stall detector.
|
|
3
|
+
* Posts a sentinel task to the JS message queue every POLL_MS.
|
|
4
|
+
* If late by STALL_THRESHOLD_MS -> emit 'stall' event.
|
|
5
|
+
*/
|
|
6
|
+
import { timeline } from '@rn-perf-kit/core';
|
|
7
|
+
|
|
8
|
+
const POLL_MS = 50;
|
|
9
|
+
const STALL_THRESHOLD_MS = 50;
|
|
10
|
+
|
|
11
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
12
|
+
let lastScheduled = 0;
|
|
13
|
+
|
|
14
|
+
function tick(): void {
|
|
15
|
+
const now = performance.now();
|
|
16
|
+
const delay = now - lastScheduled - POLL_MS;
|
|
17
|
+
if (delay > STALL_THRESHOLD_MS) {
|
|
18
|
+
timeline.add({ type: 'stall', t: lastScheduled, dur: delay });
|
|
19
|
+
}
|
|
20
|
+
lastScheduled = now;
|
|
21
|
+
timer = setTimeout(tick, POLL_MS);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function startStallDetector(): void {
|
|
25
|
+
if (timer !== null) return;
|
|
26
|
+
lastScheduled = performance.now();
|
|
27
|
+
timer = setTimeout(tick, POLL_MS);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function stopStallDetector(): void {
|
|
31
|
+
if (timer !== null) {
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
timer = null;
|
|
34
|
+
}
|
|
35
|
+
}
|
package/tsconfig.json
ADDED
package/README.md
DELETED