Skip to content

Instantly share code, notes, and snippets.

@axemclion
Created November 11, 2018 21:02
Show Gist options
  • Save axemclion/7ac685abaf197a46264dc48cc9893309 to your computer and use it in GitHub Desktop.
Save axemclion/7ac685abaf197a46264dc48cc9893309 to your computer and use it in GitHub Desktop.
React Native Android Startup - logging
@Override
public void onCreate() {
super.onCreate();
///////// Add lines below this to capture times of the large sections
final String TAG = "RN_STARTUP"; // If you already have a TAG for the app, use that instead.
final TimingLogger timingLogger = new TimingLogger("AXE", "REACT_NATIVE_STARTUP");
timingLogger.reset();
ReactMarker.addListener(new ReactMarker.MarkerListener() {
@Override
public void logMarker(ReactMarkerConstants constant, @Nullable String tag, int instanceKey) {
StringBuilder message = new StringBuilder(constant.toString()).append(" ").append(tag).append(" ").append(instanceKey);
// While we log these in ADB logcat, you should ideally also log these from production.
// That way, you will more accurate timings from real devices. You can cross reference this with device types too !
// Basically, just save the timestamps to a list, and then upload the times to your server in the next method.
//Also note that the markers have a _START and an _END, so you could even log timespans, instead of just timestamps
Log.i("REACT_NATIVE_STARTUP", message.toString());
timingLogger.addSplit(message.toString());
}
});
// This should not be just a simple handler, but should be invoked after your "Load" event. The "Load" event itself is
// different for different apps - some define it as the time a first screen is displayed, for others, it is defined as when
// a network request returns with data and all of that is rendered.
// For now, we can live with a 10second delay, will talk more about the actual end of a "Load" event in a subsequent post.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// During development, you will see this in ADB logcat. Just ensure that you run
// adb shell setprop log.tag.RN_STARTUP VERBOSE on the terminal, before running adb logcat
// This is where you would upload all the timespans to a server, so that you can aggregate them
timingLogger.dumpToLog();
}
}, 10000);
/////////////////// End of snippet
SoLoader.init(this, /* native exopackage */ false);
}
@axemclion
Copy link
Author

Have questions about this script ? Leave your questions here, and will try to answer them

@gengjiawen
Copy link

Add this script to RNTester ?

@axemclion
Copy link
Author

@gengjiawen - sure, but where will be log the data ?

@Raat-Jaaga-Taara
Copy link

Ram, I had been using this technique but face one issue - When I use multiple react instances in my app, how do I know which measure is of which instance? One very crude way is maybe make some rough sense from thread ids. Also what if multiple React instances(context + bridge init) are happening simultaneously in the same app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment