Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] Add React Native Firebase Performance monitoring #4173

Merged
merged 7 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: "com.android.application"
apply plugin: "com.google.firebase.firebase-perf"
apply from: '../../node_modules/react-native-unimodules/gradle.groovy'
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

Expand Down Expand Up @@ -233,6 +234,9 @@ dependencies {
implementation jscFlavor
}

implementation platform("com.google.firebase:firebase-bom:20.0.2")
implementation "com.google.firebase:firebase-perf"

// GIF support
implementation 'com.facebook.fresco:fresco:2.3.0'
implementation 'com.facebook.fresco:animated-gif:2.3.0'
Expand Down
7 changes: 6 additions & 1 deletion android/app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:usesCleartextTraffic="true" tools:targetApi="28" tools:ignore="GoogleAppIndexingWarning" />
<application android:usesCleartextTraffic="true" tools:targetApi="28" tools:ignore="GoogleAppIndexingWarning">
<meta-data
android:name="firebase_performance_logcat_enabled"
android:value="true"
/>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.expensify.chat;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ExpensifyAppPackage implements ReactPackage {

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}

@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();

modules.add(new StartupTimer(reactContext));

return modules;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected List<ReactPackage> getPackages() {
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new PlaidPackage());
packages.add(new ExpensifyAppPackage());

// Add unimodules
List<ReactPackage> unimodules = Arrays.<ReactPackage>asList(
Expand Down Expand Up @@ -76,6 +77,11 @@ public void onCreate() {
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false);
}

// Start the "js_load" custom performance tracing metric. This timer is stopped by a native
// module in the JS so we can measure total time starting in the native layer and ending in
// the JS layer.
StartupTimer.start();

// Increase SQLite DB write size
try {
Field field = CursorWindow.class.getDeclaredField("sCursorWindowSize");
Expand Down
39 changes: 39 additions & 0 deletions android/app/src/main/java/com/expensify/chat/StartupTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.expensify.chat;
import android.util.Log;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.metrics.Trace;

public class StartupTimer extends ReactContextBaseJavaModule {
Copy link
Contributor

@Julesssss Julesssss Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: Actually, my only comment is that maybe this could be made generic, so that each timing trace doesn't require a new Java class. But I'm sure you're aware of this, and it's not necessary until we add the second trace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, I guess my thinking here was that we really only need a single timer in the native layer (when the app starts) for now. If we need to do performance traces with firebase we can do that via the JS later and this package

https://rnfirebase.io/perf/usage#custom-tracing

StartupTimer(ReactApplicationContext context) {
super(context);
}

private static Trace trace = null;

@Override
public String getName() {
return "StartupTimer";
}

@ReactMethod
public void stop() {
if (trace == null) {
return;
}

trace.stop();
}

public static void start() {
if (BuildConfig.DEBUG) {
Log.d("StartupTimer", "Metric tracing disabled in DEBUG");
} else {
trace = FirebasePerformance.getInstance().newTrace("js_loaded");
trace.start();
}
}
}
3 changes: 2 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ buildscript {
dependencies {
classpath("com.android.tools.build:gradle:4.1.0")
classpath("com.google.gms:google-services:4.3.4")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.3.0")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.7.1")
classpath("com.google.firebase:perf-plugin:1.4.0")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
Expand Down
17 changes: 16 additions & 1 deletion ios/ExpensifyCash.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions ios/ExpensifyCash/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#import <React/RCTLinkingManager.h>
#import <React/RCTRootView.h>
#import <Firebase.h>
#import "RCTStartupTimer.h"

#import "RNBootSplash.h"

Expand Down Expand Up @@ -74,6 +75,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

[RNBootSplash initWithStoryboard:@"BootSplash" rootView:rootView]; // <- initialization using the storyboard file name

// Start the "js_load" custom performance tracing metric. This timer is stopped by a native
// module in the JS so we can measure total time starting in the native layer and ending in
// the JS layer.
[RCTStartupTimer start];
return YES;
}

Expand Down
19 changes: 19 additions & 0 deletions ios/ExpensifyCash/RCTStartupTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// RCTStartupTimer.h
// ExpensifyCash
//
// Created by Marc Glasser on 7/21/21.
//
#import <React/RCTBridgeModule.h>
#import <FirebasePerformance/FIRPerformance.h>

#ifndef RCTStartupTimer_h
#define RCTStartupTimer_h


#endif /* RCTStartupTimer_h */

@interface RCTStartupTimer : NSObject <RCTBridgeModule>
+ (void)start;

@end
33 changes: 33 additions & 0 deletions ios/ExpensifyCash/RCTStartupTimer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// RCTStartupTimer.m
// ExpensifyCash
//
// Created by Marc Glasser on 7/21/21.
//

#import "RCTStartupTimer.h"

@implementation RCTStartupTimer

static FIRTrace *trace = nil;

+ (void)start {
#if DEBUG
// We don't want to record this on debug since it will skew the metrics we collect
NSLog(@"[StartupTimer] Metric tracing disabled in DEBUG");
#else
trace = [FIRPerformance startTraceWithName:@"js_loaded"];
#endif
}

RCT_EXPORT_METHOD(stop)
{
if (trace) {
[trace stop];
}
}

// To export a module named StartupTimer
RCT_EXPORT_MODULE(StartupTimer);

@end
Loading