paint-brush
Comparing Perfetto With Android Profilerby@leonidivankin
2,322 reads
2,322 reads

Comparing Perfetto With Android Profiler

by Leonid IvankinSeptember 28th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The Android Profiler is designed specifically for profiling Android applications. However, it has a number of disadvantages, which you can find on StackOverFlow or Issue Tracker. In such cases, it is recommended to use Perfetto instead of Profiler- it sees the whole picture, not just the Android app. With Android 11 (R, API 30) it is enabled by default. It is only available with Android 9 (P, API 28) and it needs to be activated.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Comparing Perfetto With Android Profiler
Leonid Ivankin HackerNoon profile picture

Introduction

The Android Profiler is first of all good because it is integrated into Android Studio and is designed specifically for profiling Android applications. However, it has a number of disadvantages, which you can find on StackOverFlow or Issue Tracker: hangs, crashes, incorrect data (over- or underestimated time), not always attached to the process.


This is especially noticeable on large commercial projects (small projects usually have no problems). In such cases, it is recommended to use Perfetto.


It is worth noting that Perfetto also has disadvantages, but works more stably. Its disadvantages are mainly related to the inconvenience of work. Perfetto knows how to work not only with Android applications. It integrates with ftrace Linux, so it can trace Android, Linux as well as Chromium.


Related to this is another advantage of Perfetto - it sees the whole picture, not just the Android app. This means that if another process has affected the application process, you can see it through Perfetto. This is not available to Profiler.


Another downside toPerfetto is that it is only available with Android 9 (P, api 28). And it needs to be activated. With Android 11 (R, api 30) it is enabled by default. To activate it, you need to enter:

adb shell setprop persist.traced.enable 1

Working with Perfetto

There are several ways of working with Perfetto. Let's look at one of the most obvious ones.


Let's take PerfettoActivity as an example. Create a project and insert the following code:


class PerfettoActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       Trace.beginSection("Perf: printString")
       Thread.sleep(2000)
       Trace.endSection()
   }
}


The point of this example is that we delay the main thread by 2000ms and wrap it in a Trace.beginSection() and Trace.endSection() section. With these methods it will be easier for us to find in the renderer.


Next, go to Device Settings -> System -> Advanced -> Developer options -> System Tracing

It is necessary to enable the Show Quick Settings tile option. When this option is checked, the Record trace button in the quick settings becomes available.


Next, go to Quick settings, clickRecord trace, launch the application and click Stop tracing. A file at data/misc will appear in the device memory. In order to retrieve it, you must enter the command: adb pull /data/local/traces


After that, the traces folder will appear in the root of the project, where the necessary file will be located:


Then go to:  https://ui.perfetto.dev/.


There is a visualizer available atui.perfetto.dev for easy viewing and analysis of the results. The new visualizer takes advantage of modern Web platform technologies. Its multi-threaded design, based on WebWorkers, provides a consistently responsive user interface. You can read more about it here.


The Perfettouser interface, once opened once, works completely offline. Traces opened with the UI are handled locally by the browser and require no server-side interaction.


In the panel on the left, click the Open trace file button. Find the .perfetto-trace file just generated by Android Studio and click Open.


The following result appears:


The picture shows all the processes and threads that take place on the device. The colored bars show the methods running in these threads.


Let's find the process of our application in the list. In my case it is com.leonidivankin.draftandroid (see point 1).


Here we find the main thread (point 2). As you see, its name is not main. In this line you can see the methods executed when creating and starting the activation (point 3).


They also have different names, in particular you can find theperformCreate:com.leonidivankin.draftandroid.articles.perfetto.PerfettoActivity - this is the onCreate() method in Android concepts. As I see it, this is the main drawback of Perfetto before Profiler - the inconsistency of thread and method names relative to the source code.


Here we see our section marked with the Perf: printString tag (point 4). Click on this section and a window opens with additional information Current Selection:


Here you can see additional information about the work done in the section (point 2), in particular the time of completion (point 3).


You can also use the search bar (point 1) and enter there the Perf: printString tag you are looking for. The Perfettovisualizer highlights the desired process (point 2), thread (point 3), and segment (point 4).


Using control+scroll, you can zoom in and out:


Scroll down and up through the Perfetto panel. You may notice processes you are familiar with, such as bluetooth, audio.service, composer, systemui, and others:


This way you can establish dependencies between your application and others and look for problems in their dependencies. From my point of view, this is the most important advantage of Perfetto over Profiler.


Comparing Perfetto with Profiler

Advantages:

  • sees the processes of all applications;
  • works more stably and accurately with large projects.

Disadvantages:

  • not integrated into Android Studio;
  • only available with Android 9;
  • inconsistency of names and threads with the source code.

Useful Links

https://perfetto.dev/docs/ https://ui.perfetto.dev/ https://github.com/google/perfetto https://www.youtube.com/watch?v=phhLFicMacY https://www.youtube.com/watch?v=QCj-NaErICI https://www.youtube.com/watch?v=9kNhB_z704I https://medium.com/kayvan-kaseb/android-app-performance-analysis-with-perfetto-c8707d879abd


Conclusion

Perfetto is a quite powerful tool when you need to look inside the processes running on your device. It is most often used to analyze applications and optimize, for example, the cold-warm-hot start speed of an application. It has several advantages. For example, analyzing all the processes as a whole and getting more accurate results. However, as you have seen, it is not without disadvantages.