I recently had a situation where I needed to change from
FlexboxLayout. Before I could implement this change I needed to be sure we didn't make the UI slower in any way. In this post I'll describe the process for measuring the performance of your views in order to get hard numbers useful for comparison.
In the official Android documentation you can find information on how to enable GPU profiling on an Android device or emulator. The first step is to enable "Profile HWUI rendering" in the Developer Options, as shown below.
This dialog presents two profiling options, and you want to select the second one which lets you print the data using ADB. The first option in the dialog will display colored bars on top of your application, where you get an overview of how long each pass takes. While easy to use, it's not particularly useful for comparing the performance when doing changes to a layout in the same screen. Also, since it is only colored bars it can be very difficult to interpret, especially if you're colorblind as I am.
The testing section of the training documentation contains the information needed on how to use ADB to get the exact numbers from the GPU profiler. Once you've read that page, you'll know that in order to get the numbers of the last 120 frames for a specific app (this is the amount of frame recorded by the GPU profiler), you need to run the following ADB command:
This will, among a bunch of other stats, print a comma separated list of numbers. These are the nanosecond timestamps for each of the phases in the rendering of a frame (up to the last 120 frames).
The easiest way to work with this is to copy it all and paste it into Google Sheets. See the trick below for how to paste CSV data into columns.
Interpret framestat data
Note that you might only see one or two rows of data in the output, depending on what is happening on your screen. Simple views where you're not scrolling or doing any animations is one example of this. You might want to run the app several times to get multiple values for your measurement.
The first number in each row is a flag that indicates if this is a valid measurement or not.
0 is a valid measurement, whereas anything else denotes a frame that was rendered during a transition between two activities or some other event that you're not interested in.
Next, there are two intervals we want to collect: the measure/layout pass and draw pass. These will tell you how well your view is performing. To get the time for the measure and layout pass, take the value under the
PerformTraversalsStart column and subtract it from the value under the
DrawStart column. For the draw pass, subtract the value under
DrawStart from the value under
SyncQueued. You now have the time for how long each of these took.
All the values are in nanoseconds, so don't be alarmed if it looks very big. Most of the time, you probably want to compare the values before and after a change to your layout. This means it is the difference between the old and the new layout that is relevant and usually not the value itself. If the new one is faster, or at least as fast, everything is good. If it's slower, you probably need to do some optimizations.
The GPU profiler in Android is very useful, but only for certain scenarios. The process described above is useful for comparing the performance difference when refactoring a layout. There is plenty of other information coming from the profiler that can be useful, but which I'm not covering in this post.
Sometimes you probably don't even need to measure the performance difference. If you have a layout consisting of multiple nested
RelativeLayout, and you managed to replace them with a single
ConstraintLayout, there is probably no point in spending time on measuring this.
In my case, I needed to change from a
LinearLayout to a
FlexboxLayout due to a bug in Right-To-Left rendering. After comparing the performance between the two layouts I actually found that
FlexboxLayout performed even better than
LinearLayout. A nice side-effect for a rather cumbersome refactoring!