Skip to content

Commit e2bf843

Browse files
a-dilettantefacebook-github-bot
authored andcommitted
Introduce Module Setup Metric (facebook#23859)
Summary: The `RCTBridge` contains numerous definitions of notification names, which we can observe in order to get insights into the React Native performance. The Android implementation is a little different, such that you can listen for any of the [following](https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactMarkerConstants.java) marker constants, simply by including the following code: ```java ReactMarker.addListener(new ReactMarker.MarkerListener() { Override public void logMarker(ReactMarkerConstants name, Nullable String tag, int instanceKey) { Log.d("ReactNativeEvent", "name: "+ name.name() + " tag: " + tag); } }); ``` This will allow you to perform the necessary processing, calculations as required. --- This PR enables observing for the module setup event (`RCTDidSetupModuleNotification`) by including the respective module's name & setup time in milliseconds. [iOS] [Added] - Gain insights on the module setup times by observing `RCTDidSetupModuleNotification`. The `userInfo` dictionary will contain the module name and setup time in milliseconds. These values can be extracted via `RCTDidSetupModuleNotificationModuleNameKey ` and `RCTDidSetupModuleNotificationSetupTimeKey`. Pull Request resolved: facebook#23859 Differential Revision: D14579066 Pulled By: PeteTheHeat fbshipit-source-id: 52645127c3fc6aa5bd73e3bd471fccd79cb05c14
1 parent f9a344c commit e2bf843

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

React/Base/RCTBridge.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ RCT_EXTERN NSString *const RCTJavaScriptDidFailToLoadNotification;
4848
*/
4949
RCT_EXTERN NSString *const RCTDidInitializeModuleNotification;
5050

51+
/**
52+
* This notification fires each time a native module is setup after it is initialized. The
53+
* `RCTDidSetupModuleNotificationModuleNameKey` key will contain a reference to the module name and
54+
* `RCTDidSetupModuleNotificationSetupTimeKey` will contain the setup time in ms.
55+
*/
56+
RCT_EXTERN NSString *const RCTDidSetupModuleNotification;
57+
58+
/**
59+
* Key for the module name (NSString) in the
60+
* RCTDidSetupModuleNotification userInfo dictionary.
61+
*/
62+
RCT_EXTERN NSString *const RCTDidSetupModuleNotificationModuleNameKey;
63+
64+
/**
65+
* Key for the setup time (NSNumber) in the
66+
* RCTDidSetupModuleNotification userInfo dictionary.
67+
*/
68+
RCT_EXTERN NSString *const RCTDidSetupModuleNotificationSetupTimeKey;
69+
5170
/**
5271
* This notification fires just before the bridge starts processing a request to
5372
* reload.

React/Base/RCTBridge.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
NSString *const RCTJavaScriptDidLoadNotification = @"RCTJavaScriptDidLoadNotification";
2828
NSString *const RCTJavaScriptDidFailToLoadNotification = @"RCTJavaScriptDidFailToLoadNotification";
2929
NSString *const RCTDidInitializeModuleNotification = @"RCTDidInitializeModuleNotification";
30+
NSString *const RCTDidSetupModuleNotification = @"RCTDidSetupModuleNotification";
31+
NSString *const RCTDidSetupModuleNotificationModuleNameKey = @"moduleName";
32+
NSString *const RCTDidSetupModuleNotificationSetupTimeKey = @"setupTime";
3033
NSString *const RCTBridgeWillReloadNotification = @"RCTBridgeWillReloadNotification";
3134
NSString *const RCTBridgeWillDownloadScriptNotification = @"RCTBridgeWillDownloadScriptNotification";
3235
NSString *const RCTBridgeDidDownloadScriptNotification = @"RCTBridgeDidDownloadScriptNotification";

React/Base/RCTPerformanceLogger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef NS_ENUM(NSUInteger, RCTPLTag) {
2020
RCTPLNativeModuleMainThread,
2121
RCTPLNativeModulePrepareConfig,
2222
RCTPLNativeModuleMainThreadUsesCount,
23+
RCTPLNativeModuleSetup,
2324
RCTPLJSCWrapperOpenLibrary,
2425
RCTPLBridgeStartup,
2526
RCTPLTTI,

React/Base/RCTPerformanceLogger.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ - (instancetype)init
4141
@"NativeModulePrepareConfig",
4242
@"NativeModuleInjectConfig",
4343
@"NativeModuleMainThreadUsesCount",
44+
@"NativeModuleSetup",
4445
@"JSCWrapperOpenLibrary",
4546
@"JSCExecutorSetup",
4647
@"BridgeStartup",

React/CxxBridge/RCTCxxBridge.mm

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ static bool isRAMBundle(NSData *script) {
9999
return parseTypeFromHeader(header) == ScriptTag::RAMBundle;
100100
}
101101

102+
static void notifyAboutModuleSetup(RCTPerformanceLogger *performanceLogger, const char *tag) {
103+
NSString *moduleName = [[NSString alloc] initWithUTF8String:tag];
104+
if (moduleName) {
105+
int64_t setupTime = [performanceLogger durationForTag:RCTPLNativeModuleSetup];
106+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTDidSetupModuleNotification
107+
object:nil
108+
userInfo:@{
109+
RCTDidSetupModuleNotificationModuleNameKey: moduleName,
110+
RCTDidSetupModuleNotificationSetupTimeKey: @(setupTime)
111+
}];
112+
}
113+
}
114+
102115
static void registerPerformanceLoggerHooks(RCTPerformanceLogger *performanceLogger) {
103116
__weak RCTPerformanceLogger *weakPerformanceLogger = performanceLogger;
104117
ReactMarker::logTaggedMarker = [weakPerformanceLogger](const ReactMarker::ReactMarkerId markerId, const char *__unused tag) {
@@ -116,11 +129,16 @@ static void registerPerformanceLoggerHooks(RCTPerformanceLogger *performanceLogg
116129
[weakPerformanceLogger appendStopForTag:RCTPLRAMNativeRequires];
117130
[weakPerformanceLogger addValue:1 forTag:RCTPLRAMNativeRequiresCount];
118131
break;
132+
case ReactMarker::NATIVE_MODULE_SETUP_START:
133+
[weakPerformanceLogger markStartForTag:RCTPLNativeModuleSetup];
134+
break;
135+
case ReactMarker::NATIVE_MODULE_SETUP_STOP:
136+
[weakPerformanceLogger markStopForTag:RCTPLNativeModuleSetup];
137+
notifyAboutModuleSetup(weakPerformanceLogger, tag);
138+
break;
119139
case ReactMarker::CREATE_REACT_CONTEXT_STOP:
120140
case ReactMarker::JS_BUNDLE_STRING_CONVERT_START:
121141
case ReactMarker::JS_BUNDLE_STRING_CONVERT_STOP:
122-
case ReactMarker::NATIVE_MODULE_SETUP_START:
123-
case ReactMarker::NATIVE_MODULE_SETUP_STOP:
124142
case ReactMarker::REGISTER_JS_SEGMENT_START:
125143
case ReactMarker::REGISTER_JS_SEGMENT_STOP:
126144
// These are not used on iOS.

0 commit comments

Comments
 (0)