Reproduced
Two known methods seem to be available, but the feeling results are inaccurate: one is, adb shell am start-w packagename/activity, this can get two values, Thistime and totaltime, do not know what the difference between the two, And it does not match the actual boot time, both of which may be smaller than the actual startup time (the difference is greater when testing the game), and the other is the way the ADB logcat, feeling that the results are different from the actual
1 Application Launch Scenario
In fact, the startup time of an App in Android can be calculated accurately. But to divide the scene. That is to say separate games and apps. As we all know, game development and application development are not the same thing in Android. So we need to be separate.
1.1 Application Startup
We usually write the application, the general will specify a mainactivity, the user on the desktop click on the activity, the system will directly play this activity. We know the Activity will go oncreate/onstart/onresume when it starts. These callback functions.
Many books say that when the Onresume function is executed, the application is displayed ... In fact, this is an inaccurate statement, because from a system level, an Activity after Oncreate/onstart/onresume these life cycle, just completed the application of some of its own configuration, such as window of some properties of the settings/View tree establishment (Just build, and not show, that is, just call the inflate only). Later Viewrootimpl also calls two times performtraversals, initializing Egl and Measure/layout/draw. And so on. So we define a startup time for an Android app, and certainly not up and down in the Activity's callback function. Instead, the user sees on the phone screen that you set the layout in the OnCreate Setcontentview as the full display, which is what we often say Apply the first frame.
It is a bit far above, not interested in the words can not look, the following direct method. The main word of the ADB shell am start-w packagename/activity, is the start time that can be fully applied. But also to divide the scene.
1.2 Application First Boot
That's what we often call cold start, when your application's process is not created. This is also a usage scenario for most applications. After you click on your app's icon on the desktop, you first create a process before you start mainactivity. This time the ADB shell am start-w packagename/mainactivity Returns the result, is the standard application startup time (note that Android 5.0 is not WaitTime this value before the phone):
? ADB shell am start-w com.meizu.media.painter/com.meizu.media.painter.paintermainactivitystarting:intent {act= Android.intent.action.MAIN Cat=[android.intent.category.launcher] cmp=com.meizu.media.painter/. Paintermainactivity}status:okactivity:com.meizu.media.painter/. Paintermainactivitythistime:355totaltime:355waittime:365complete
A total of three results have been returned, and we shall prevail on the WaitTime.
The following is an explanation of the difference between Thistime/totaltime/waittime:
The implementation of "ADB shell am start-w" is in the Frameworks\base\cmds\am\src\com\android\commands\am\am.java file. is actually to call the activitymanagerservice.startactivityandwait () interface across the binder (the Activitymanagerservice is referred to as AMS later), The results returned by this interface include the Thistime, TotalTime time printed above.
- StartTime the point in time that the startactivityandwait () was just ready to be called
- Endtime records the point in time that the startactivityandwait () function call returned
- WaitTime = startactivityandwait () call time-consuming.
The calculation of Thistime and TotalTime in the Frameworks\base\services\core\java\com\android\server\am\activityrecord.java file The reportlaunchtimelocked () function.
Let's explain the Curtime, Displaystarttime, mlaunchstarttime three time variables in the code.
- Curtime represents the point in time for the function call.
- Displaystarttime represents the start point of the last activity in a series of startup activity.
- Mlaunchstarttime represents the start point of the first activity in a series of startup activity.
Under normal circumstances, click on the desktop icon to start an Activity with the interface, Displaystarttime and Mlaunchstarttime point at the same point in time, at this time thistime=totaltime. Another case is that clicking on the Desktop icon application will start a non-interface activity to do the logical processing, and then start an interface activity, in the case of the start of a series of activity (known as the start is the case), Displaystarttime Point to the start point of the last activity, Mlaunchstarttime points to the start point of the first non-interface activity, at which time thistime! =totaltime. Both of these situations are as follows:
In the above figure, I marked three time periods with ①②③, what did I do in the three time periods respectively?
- During the ① time period, AMS creates a Activityrecord record block and selects a reasonable Task to pause the Activity of the current resume
- In the ② time period, start the process, invoke onCreate () such as no interface activity, Pause/finish no interface activity
- In the ③ time period, call the OnCreate, Onresume with interface Activity
See here should be clear thistime, TotalTime, WaitTime three time relationship. WaitTime is the total time-consuming, including the previous application activity pause and the time when the new app started; Thistime indicates that the start of the last activity for a series of start-up activity is time consuming; TotalTime indicates the time-consuming start of a new application. This includes the start of the new process and the start of the activity, but does not include the time of the previous app activity pause. In other words, developers generally only care about totaltime, this time is the actual start of their own application time-consuming.
The table of two values in Tag=am_activity_launch_time in Event log represents thistime, TotalTime, and is consistent with the values obtained through "ADB shell am start-w".
Finally, the system according to what to judge the end of the application start. We know that application launches include process initiation, activity lifecycle Oncreate/onresume, and so on. In the first onresume when adding a window to the WMS, and then Measure/layout/draw, the window is drawn after the completion of the notification WMS,WMS the appropriate timing control interface to start the display (mixed with the interface Switch animation logic). Remember that after the window interface is displayed, the WMS calls Reportlaunchtimelocked () to notify the AMS Activity to start completion.
Finally, if you only care about the startup time of an application, refer to TotalTime, if you are concerned about the time-consuming system startup application, refer to waittime; If you are concerned about the application with interface activity start time, refer to Thistime.
1.2 Apply non-first boot
If you press the back key and do not kill the application process, then executing the above command will be faster because there is no need to create a process, just start an activity. This is what we call the application hot start.
2 game Launch scene
When the game starts, it does not apply the command line method to start, because from the user clicks the desktop icon to the login interface, both the system part also has the game own part.
2.1 System Section
The game also has an activity, so the start of the time will still go to start the activity, so the system startup part is the user clicks the Desktop Desktop response to the activity to start.
2.2 Game Section
After the main activity of the game is started, it will also do some time-consuming things, when you see the interface is not operational, such as: Loading game data, networking Update data, read and update configuration files, game engine initialization and other operations. From the game development point of view, to the real user can manipulate the interface is considered a game when the real load completed. Then this time, you have to use log to record, because loading game data, network update data, read and update configuration files, game engine initialization These operations are the game's own logic, and system-independent, so the game itself to define the point of loading completed.
For the game start time, we are more inclined to calculate from the click of the desktop icon to the user can interact with the game this time period as a game startup time.
3 Summary
The most fascinating thing about computers is its accuracy, which is always equal to 2, how long it takes to start, and how long it may be, each time it's different, but the exact time of the day.
However, each company has different positioning for the application, so the application launch requirements are not the same. For example, some do ROM company, its built-in application start time must be very fast, so that the user's first feeling is fast, smooth; The internet company app is not very concerned about the startup speed, most of the Internet company's applications have a launch page, used to display ads or feature introduction, etc. The main interface is then entered. The demand is different, so it is understandable, but from the consumer's point of view, the sooner you see the main interface of course the better.
So when making an Android app, be sure to remember to apply the startup time as a performance indicator, after all
The World martial arts, only fast not broken!
How do I calculate the App startup time in Android?