应用启动时间的测试

应用程序的启动按照场景大致可分为三种:Cold Start、Warm Start、Lukewarm Start


Cold Start(冷启动):

冷启动的场景一般是:系统重启后首次打开应用,或系统杀死应用进程后首次打开应用。

冷启动时,系统会执行下面的三个任务:

  1. 加载和拉起应用。   ## Loading and launching the app
  2. 应用拉起后,立即显示空白的启动页面。    ## Displaying a blank starting window for the app immediately after launch
  3. 创建应用的进程。    ##Creating the app process

在系统创建好应用进程后,应用进程就会开始下面的步骤:

  1. 创建应用的实体。    ## Creating the app object
  2. 运行应用的主线程。    ## Launching the main thread
  3. 创建应用的主页面。    ## Creating the main activity
  4. 绘制页面上的view。    ##Inflating views
  5. 布局页面。    ##Laying out the screen
  6. 执行首次的绘制。    ## Performing the initial draw

Warm Start(热启动):

热启动时,系统将应用的activity从后台拉回前台。应用程序的activities在内存中没有被销毁,所以不需要重新初始化和重新绘制。

Lukewarm Start(温启动?大概就是介于上面两者之间的意思吧):

这类启动和下面的场景相关:

  1. 用户按返回键退出应用,然后重新运行。此时应用的进程还没被杀掉可以直接继续运行,但应用必须通过调用onCreate()重新创建页面。
  2. 系统回收了应用的内存,然后用户重新运行应用。

测试启动时间的两个方法:

方法一:

通过logcat看系统日志,命令:adb logcat | grep "Displayed"  或  adb logcat -s "ActivityManager" | grep "Displayed"

ActivityManager: Displayed com.android.myexample/.StartupTiming: +3s534ms

或者

ActivityManager: Displayed com.android.myexample/.StartupTiming: +3s534ms (total +1m22s643ms)

(这里显示了两个时间,第一个时间是只测量了首次绘制的页面的时间。第二个时间即“total”,从应用进程启动时开始测量,包括了其他的也是首次绘制的但没有显示到屏幕上的页面的时间。“total”这个是时间只会在第一个显示的时间和“total”这个值不一致时,才会显示。)

这个值指的是应用进程开始运行到完成屏幕上页面的绘制之间的时间。在这个时间内,按顺序执行了一下的事情:

1.Launch the process.

2.Initialize the objects.

3.Create and initialize the activity.

4.Inflate the layout.

5.Draw your application for the first time.

适用于首次启动应用、进程被杀或返回键退出应用的情况。

 

方法二:

使用adb工具,命令:adb shell am start -S -W packagename/activityname,例如:adb shell am start -S -W com.autotest.tool.appmonitor/.Main2Activity

其中-S 启动页面前先强制停止应用, -W 等待启动完成

命令:

adb [-d|-e|-s <serialNumber>] shell am start -S -W com.example.app/.MainActivity

-c android.intent.category.LAUNCHER

-a android.intent.action.MAIN

#后面两个参数是可选的,用于指定类别<category>和行为<action>

输出内容:

Starting: Intent

Activity: com.example.app/.MainActivity

ThisTime: 2044

TotalTime: 2044

WaitTime: 2054

Complete

home键将应用置于后台后,使用不带-S的参数也可以获得应用显示回前台的时间。

原文地址:https://www.cnblogs.com/mgzc-1508873480/p/7954445.html