About App UI performance test, Android provides a native tool Systrace, normal rendering FPS is generally around 60, but if some code is not written well, the UI performance may be affected, resulting in interface lag, this kind of problem is the most difficult to check, why the lag, where the lag. Of course, you can also log to see the time, but after all, it is not convenient. Systrace can solve this problem. I checked the information about Systrace on the Internet, but there are still few of them, and maybe fewer people use them. So most of this article is translated from the official Google documentation


The preparatory work

First of all, if you want to run Systrace, you need to install Python and Android SDK Tools 20 or later. Meanwhile, Systrace currently only supports Android4.1 and above.

Start recording

There are two ways to start recording. One is to use Python, and the other is to use the built-in plugin in AndroidStudio. There’s a problem here. I’m not sure if using the built-in add-ons in AndroidStudio also requires Python to be installed, because I have these things installed on my computer all the time, so I can just run them.

The Python launch

First switch to the Android SDK’s platform-tools directory, which has a systrace folder, and then cut to this folder:


python systrace.py --time=10 -o mynewtrace.html sched gfx view wmCopy the code

The operating rules are as follows:

Parameter names meaning
-h,–help Help information
-o Save file name
-t N,–time=N The number of seconds of data, the default is 5 seconds, based on the current point in time back N time
-b N,–buf-size=N The unit is kilobytes, limiting the data size
-k –ktrace= Tracking specific methods
-l,–list-categories Set the tag to track
-a ,–app= The package name
–from-file= Create the source trace file for the report
-e ,–serial= Device number

Label abbreviation:

shorthand The full name
gfx – Graphics
input – Input
view – View
webview – WebView
wm – Window Manager
am – Activity Manager
sync – Synchronization Manager
audio – Audio
video – Video
camera – Camera

These tags are required under SDK 17 according to the official documentation

--set-tags = <TAGS>Copy the code

To add, SDK18 and higher are simply shorthand. This tag represents the tag used to generate performance analysis results, which will be seen later.

Android Studio start

Open Android Studio:




The data analysis

I will use the command line mode to record the data. Before recording, I wrote a simple demo that had three activities, a home screen and a listView Activity, An Activity that has a RecyclerView (the code is so basic that I won’t post it) and I added 1000 items to the ListView, each of which has text and images, and RecyclerView as well. Notice that the ListView doesn’t do any optimization, because what we’re looking for is bad effects. I’ll run the program and type the following on the command line:

python systrace.py --time=20 -o deep.html -a deep.testsystrace sched gfx view wmCopy the code

Testsystrace, generate the file deep.html, run it, and then keep swiping the ListView: Until the record ends, the command line will say:

Then we open the folder and find the deep.html we just generated.



Key Description
w Zoom into the trace timeline.
s Zoom out of the trace timeline.
a Pan left on the trace timeline.
d Pan right on the trace timeline.
e Center the trace timeline on the current mouse location.
g Show grid at the start of the currently selected task.
Shift+g Show grid at the end of the currently selected task.
Right Arrow Select the next event on the currently selected timeline.
Left Arrow Select the previous event on the currently selected timeline.

Testsystrace we found our project deep. Testsystrace in the webpage, but we don’t know if it is a bug, the webpage only shows ep. Testsystrace, but we can confirm it is our application according to pid. And then we went through them one by one, starting with Frames. Let’s zoom in on the image above (shortcut key W) :











We found that there were no red F, but there were still a few yellow F, which we could optimize again according to the analysis and add the ViewHolder. We don’t do tests here anymore. Similarly, we can also modify according to the prompt in the Alert, but we feel that the prompt cannot directly point to the cause. It is easier to find the cause by analyzing the number of frames.

Custom record

We can also Trace. BeginSection to record some information as follows:



conclusion

In this way, the reasons for some problems of APP lag can be analyzed and solved. For app developers, it is better to master. For more development knowledge, or any questions, you can pay attention to my public number, give me a message: