Performance/Fenix/Debug API

From MozillaWiki
Jump to: navigation, search

The built-in Debug API (documentation) can be used to capture profiles that:

  • execute fairly early in the profile (when the application can start executing code)
  • capture all Java threads
  • can be opened the Firefox Profiler interface

The downsides are that:

  • has low-ish overhead (a few hundred ms in a 1.5-2.5s start up)
  • requires manually instrumenting the code

The Firefox Profiler is generally preferred but the Debug API can be used to work around some of its limitations. For an overview of other profiling tools, see Performance/Fenix/Getting Started.

Firefox Profiler interface

You can open *.trace files generated by the Debug API in the Firefox Profiler. Just go to and click "Load a profile from a file". The profiler interface works nearly as well as profiles taken within the Firefox Profiler.

Usage for start up

First, you need to start tracing. Add the following to FenixApplication.kt to measure start up as early as possible:

+    override fun attachBaseContext(base: Context) {
+        if (base.isMainProcess()) {
+            Debug.startMethodTracingSampling(
+                "startup",
+                0,
+                TimeUnit.MILLISECONDS.toMicros(1).toInt()
+            )
+        }
+        super.attachBaseContext(base)
+    }

Then, we need to stop tracing by calling Debug.stopMethodTracing(): where you call this depends on what you're trying to profile. An easy place to put it is at the end of HomeActivity.onResume.

The trace file will be saved to the device at /sdcard/<filename> by default. The app requires permission to access this so run this before launching the app for the first time:

adb shell pm grant org.mozilla.fenix android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant org.mozilla.fenix android.permission.WRITE_EXTERNAL_STORAGE

Now launch the app!

Once the device has hit stopMethodTracing, you can pull the profile from the device. If you didn't change the file name provided to start* in the code sample above, you can run the following to get the profile:

adb pull /sdcard/startup.trace

This profile can then be opened by the Firefox Profiler (preferred) or the Android Studio profiler (with File -> Open).