Current location - Trademark Inquiry Complete Network - Futures platform - How to quantify the "stuckness" of Android applications
How to quantify the "stuckness" of Android applications

Before determining the measurement indicators, let’s first study Android’s UI update mechanism.

1. How to draw UI on Android?

As for how Android updates the UI, I believe there have been many articles introducing the steps and processes. Generally speaking, the following figure can be used to show it:

As you can see from the figure, no matter which As the road goes on, SurfaceFlinger will always control the final update.

During the Android version update process, it was discovered that Google had added a Project Butter to Jelly Bean to solve the problem of "poor UI fluency", one of the problems that seriously affects Android's reputation. Project Butter mainly introduces three core elements: VSYNC (vertical synchronization), Triple Buffer and Choreographer.

2. Start with VSYNC

VSync is the abbreviation of Vertical Synchronization. It is a technology that has been widely used on PCs for a long time. It can be simply thought of as It is a timed interrupt. The VSync mechanism has been introduced in Android 4.1 (JB).

The above figure shows the drawing process under the VSync mechanism. As can be seen from the above figure, the processing time of both CPU and GPU is less than one VSync interval, which is 16.6ms. If every interval is drawn, the current FPS is 60 frames.

When the CPU and GPU processing times are both slow, or due to other reasons, such as too much work in the main thread, then a situation like the following picture will occur.

As you can see from the picture above, the processing time of the CPU and GPU is greater than the interval of one VSync (16.6ms) for various reasons, so when the second VSync is still processing the drawing of area A, It is impossible to achieve the theoretical FPS60, and frame dropping (SF: Skipped Frame) also occurs.

For ease of understanding, the above picture uses the dual Buffer mechanism. In fact, Android 4.1 introduced Triple Buffer, so when the dual Buffer is not enough, the Triple Buffer frame loss situation is as shown in the figure below.

Oh my ladygaga!

These all made Sajia dizzy! Let’s use an analogy to make it simpler!

The VSync mechanism is like an engine with a fixed speed (60 rpm/s). Each turn will prompt you to do some UI-related things, but not every turn will have work to do (just like sometimes in neutral, sometimes in D). Sometimes due to various resistances, the workload of a certain circle is heavier than 16.6ms, so the engine will not rotate 60 times in this second. Of course, it may also be affected by other factors, such as insufficient oil supply (work done in the main thread). Too much) and so on, the speed will decrease. We call this speed smoothness.

3. From FPS & frame loss to smoothness (SM: SMoothness)

In fact, in many Android apps, there are few scenes that require continuous drawing. The pages are all static. That is to say, there will be a situation like this. Although not all of the 60 Loops of VSync in 1 second are doing drawing work, and the FPS will be relatively low, it does not mean that the program is not smooth at this time (for example, if I leave the App motionless, The measured FPS is 1). Therefore, low FPS does not mean that the UI of the current App is not smooth, but the number of times the VSync Loop is run within 1 second can better illustrate the smoothness of the current App. Therefore, the following two indicators can better represent whether the current App is in a smooth state than FPS. The same two indicators can better quantify the degree of App lag:

a) Skipped Frame (SF: Skipped Frame): As shown in Figure 2 above, the work should be completed in 16.6ms but failed due to various reasons. After it is done, it takes up the last n 16.6ms, which is equivalent to losing n frames.

b) Smoothness (SM: SMoothness): As opposed to frame loss, the number of Loop runs within 1 second in the VSync mechanism.

Compared with frame loss, there are 60 loops in 1 second because certain working times exceed 16.6ms (frame loss), so the loop cannot run 60 times (theoretical maximum).

When the fluency is smaller, it means the current program is lagging.

4. Counting: How to get smoothness (SM: SMoothness)

Following the above conclusion, if notification is given before each Loop is run under such a mechanism, record the number Just fine.

Fortunately, we found an object called Choreographer in the new Android mechanism.

According to Google's official API document description, it is used to coordinate animations, input and drawing timing, and each Loop uses a Choreographer object.

The following figure shows the definition and structure of Choreographer.

Conclusion

Through the above principle analysis, we can draw the following conclusions:

1. After Android 4.1 introduces the VSync mechanism, you can use its Loop to understand the current App’s highest Drawing ability.

It is executed every 16.6ms (this value is a static variable and will adopt different values ??according to different system versions. The current test version is 16.6ms so that the highest refresh frame rate is controlled at 60FPS Within);

If there is no above event, such a Loop will also be run;

How many times this Loop has been run within 1s can indicate the highest value drawn by the current App. The ability, that is, the degree of lag of the Android App;

In addition, if the execution time exceeds 16.6ms during a Loop, then divide the time more than 16.6ms by 16.6ms, which is the current App SF: Skipped Frame.

2. In Choreographer's callback FrameCallback, the smoothness of the current App can be expressed by counting in seconds, that is, smoothness SM (SMoothness).

Using this method, you can observe the fluency of the current App inside the App.

The editor has something to say

As a professional quality tracking platform, in addition to analyzing various crash issues on mobile terminals, Tencent Bugly will also continue to share with you Goose Factory’s mobile quality methods , track mobile app quality more efficiently!