Android App Performance Analysis with Perfetto

Kayvan Kaseb
Software Development
5 min readAug 21, 2020
This picture is provided by Unsplash

Basically, it is extremely vital for Android applications to perform all operations as fast as possible. An Android app is considered to have poor performance if it responds slowly, shows choppy animations, freezes, crashes, or consumes a lot of power. To avoid these problematic issues, there are some best practices and tools have been introduced and developed. This essay will discuss Android app performance analysis by using Perfetto as an advanced platform-wide tracing tool.

Introduction and Overview

As a matter of fact, recording device activity over a short period of time is is called System Tracing. System tracing produces a trace file, which can be used to generate a system report. This report helps you identify how best to improve your app or game’s performance. The Android platform supports some different options for recording traces:

  1. Android Studio CPU profiler

The Android Studio CPU profiler inspects your app’s CPU usage and thread activity in real time when you want to interact with your Android app. Also, you can be able to inspect the details in recorded method traces, function traces, and system traces.

2. System Tracing app

In fact, this option is similar to the systrace command-line utility; however, the app allows you to record traces directly from a test device itself, without needing to plug in the device and connect to it over ADB. In addition, you can use the app to share your results from these traces with your development team.

Devices running Android 9 (API level 28) or higher include a system-level app called System Tracing.

3. Systrace command-line tool

Initially, Systrace is a legacy platform-provided command-line tool, which records device activity over a short period of time in a compressed text file. The tool produces a report that combines data from the Android kernel such as the CPU scheduler, disk activity, and app threads.

4. Perfetto

Perfetto is the new platform-wide tracing tool, which is introduced in Android 10. It is a more general and sophisticated open-source tracing project for Android, Linux, and Chrome. It provides a superset of data sources in comparison with Systrace, and allows you to record arbitrarily long traces in a protobuf-encoded binary stream. Basically, you can currently collect a trace in the following ways:

  • Through the record page in the Perfetto UI.
  • Using the perfetto command line interface.

Using Perfetto for performance analysis

Google offers to use a tool called Perfetto for both system trace collection and analysis. Perfetto gives developers the option to collect a wider variety of data than they were collecting before with Atrace. Besides, the new Perfetto Trace Viewer support large traces without crashing Chrome. Google is taking this opportunity to improve the UX. Now, you can load Perfetto trace files or Systrace files into the Trace Viewer at ui.perfetto.dev to try it out by yourself.

Perfetto allows you to collect system-wide performance traces from Android devices from a variety of data sources (kernel scheduler via ftrace, userspace instrumentation via atrace and all other data sources listed in this site).

Perfetto UI not only plays a significant role in visualizing system tracing files, but also allows developers to interact and analyze the results easily as follows:

1. You can be able to check each thread in each process.

2. Some measures for each issue are showed clearly on hierarchy trees in the timeline graph. For instance, “activity start” needs 150ms including 100ms for “layout inflation”. Also, some metrics for “resource retrievals” and “class verifying” are presented.

3. Developers do not expect to find exactly which line of code can cause performance issues. In short, you can just only understand which part of your code lead to some performance problems. Additionally, proposing improvement strategies become much more easier in this method.

4. As you noticed, Perfetto use a software technique that is called Software Visualization. To be specific, the objectives of software visualization are to support the understanding of software systems and algorithms as well as the analysis and exploration of software systems and their anomalies and their development and evolution. One of the strengths of software visualization is to combine and relate information of software systems, which are not inherently linked. For instance, by projecting code changes onto software execution traces. As a result, this technique could be an efficient way for performance analysis of Android app to observe the real obstacles.

Software Visualization refers to the visualization of information of and related to software systems — either the architecture of its source code or metrics of their runtime behavior — and their development process by means of static, interactive or animated 2-D or 3-D visual representations of their structure, execution, behavior, and evolution.

Some practical examples in using Perfetto

In the below case, there are two red activity start sections in the middle of the app startup. Trampoline activities like this are always a potential opportunity for improvement.

The picture is provided by Google resources

In the below example, the app probably refactor already, and just only has one activity starting. However, inside their activity start, there is a huge inflate section for a view called Login View, but there is no exact visible login page when you open this app. Therefore, it is possible that this developer is creating and inflating an entire Login View, and then hiding it when it is not being used, which leads to be most of the time. They probably are not aware that it is having such an impact on app startup. Undoubtedly, this 76 millisecond is quite significant. So, to solve this issue, they could potentially defer inflating those views until after checking to see if the user has logged in.

The picture is provided by Google resources

Eventually, in this app, we can observe a large background image loading in as part of the startup inside the activity resume. This image load takes 14 milliseconds, which is about 5% of the overall app startup time. It might be worth it for the image quality that you are looking for, but the developers might be able to optimize this image. For instance, they could use WebP format instead of PNG one, or they can do an asynchronous loading effect like a fade-in, that does not actually block that first frame of the app from being drawn to the screen.

The picture is provided by Google resources

In conclusion

Fundamentally, it is extremely vital for Android applications to perform all operations as fast as possible. To avoid some app performance issues, there are some best practices and tools have been mentioned and developed. This essay discussed Android app performance analysis by using Perfetto as a new platform-wide tracing tool based on Google resources.

--

--

Kayvan Kaseb
Software Development

Senior Android Developer, Technical Writer, Researcher, Artist, Founder of PURE SOFTWARE YAZILIM LİMİTED ŞİRKETİ https://www.linkedin.com/in/kayvan-kaseb