How to calculate the App startup time

Source: Internet
Author: User

App 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. such as.
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 to the full display, which is what we used to say. First frame.

The above is a bit far, not interested in the words can not look, the following directly said method.
The main words of the ADB shell am start-w packagename/activity, is the start time 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 the user clicks on your app's icon on the desktop, the first thing to do is to create a process before starting mainactivity.
The result of the ADB shell am start-w packagename/mainactivity return is the standard application startup time (note that the phone before Android 5.0 is not WaitTime this value):

1
? 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 of a series of start-up activity is time consuming;
    • TotalTime indicates the time-consuming startup of a new application, including the start of a new process and the start of the Activity, but excluding the previous

A time-consuming application of 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.

How to calculate the App startup time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.