Created
November 16, 2019 10:49
-
-
Save kyle-ssg/6a64f2ec81df42a5892d7e3c02d19ca2 to your computer and use it in GitHub Desktop.
Recording ReactMarker times in react native to track performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.perf; | |
import android.app.Application; | |
import android.content.Context; | |
import com.facebook.react.PackageList; | |
import com.facebook.react.ReactApplication; | |
import com.facebook.react.ReactNativeHost; | |
import com.facebook.react.ReactPackage; | |
import com.facebook.soloader.SoLoader; | |
import java.lang.reflect.InvocationTargetException; | |
import java.util.List; | |
import com.facebook.react.bridge.ReactMarker; | |
import com.facebook.react.bridge.ReactMarkerConstants; | |
import androidx.annotation.Nullable; | |
import java.time.Duration; | |
import java.time.Instant; | |
public class MainApplication extends Application implements ReactApplication { | |
private final ReactNativeHost mReactNativeHost = | |
new ReactNativeHost(this) { | |
@Override | |
public boolean getUseDeveloperSupport() { | |
return BuildConfig.DEBUG; | |
} | |
@Override | |
protected List<ReactPackage> getPackages() { | |
@SuppressWarnings("UnnecessaryLocalVariable") | |
List<ReactPackage> packages = new PackageList(this).getPackages(); | |
// Packages that cannot be autolinked yet can be added manually here, for example: | |
// packages.add(new MyReactNativePackage()); | |
return packages; | |
} | |
@Override | |
protected String getJSMainModuleName() { | |
return "index"; | |
} | |
}; | |
@Override | |
public ReactNativeHost getReactNativeHost() { | |
return mReactNativeHost; | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
Instant start = Instant.now(); | |
//your code | |
ReactMarker.addListener(new ReactMarker.MarkerListener() { | |
@Override | |
public void logMarker(ReactMarkerConstants constant, @Nullable String tag, int instanceKey) { | |
Instant end = Instant.now(); | |
Duration timeElapsed = Duration.between(start, end); | |
StringBuilder message = new StringBuilder(constant.toString()).append(" ").append(tag).append(" ").append(instanceKey).append(" REACT_NATIVE_STARTUP"); | |
System.out.println(message.toString() + " " + timeElapsed.toMillis()); | |
} | |
}); | |
SoLoader.init(this, /* native exopackage */ false); | |
initializeFlipper(this); // Remove this line if you don't want Flipper enabled | |
} | |
/** | |
* Loads Flipper in React Native templates. | |
* | |
* @param context | |
*/ | |
private static void initializeFlipper(Context context) { | |
if (BuildConfig.DEBUG) { | |
try { | |
/* | |
We use reflection here to pick up the class that initializes Flipper, | |
since Flipper library is not available in release mode | |
*/ | |
Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper"); | |
aClass.getMethod("initializeFlipper", Context.class).invoke(null, context); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (NoSuchMethodException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment