如何统计启动Activity所用的时间
如何统计Activity启动所用的时间?
可以通过分析Log得到(这个就是DDMS的那个Log)。
当我们点击触摸时会了类似以下的Log A:
03-06 03:36:47.865: VERBOSE/InputDevice(2486): ID[0]=0(0) Dn(0=>1)
03-06 03:36:47.865: INFO/PowerManagerService(2486): Ulight 3->7|0
03-06 03:36:47.933: INFO/(2486): PRODUCT_SHIP = false
03-06 03:36:47.933: VERBOSE/InputDevice(2486): ID[0]=0(0) Up(1=>0)
当系统收到一个启动Activity的Intent时会有类似以下的Log B:
03-06 03:36:47.954: INFO/ActivityManager(2486): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] 易做图=0x10200000 cmp=com.teleca/.ContextMenuActivity }
当系统完成Activity进程启动时会有类似以下的Log C:
03-06 03:36:48.068: INFO/ActivityManager(2486): Start proc com.teleca for activity com.teleca/.ContextMenuActivity: pid=2933 uid=10089 gids={}
当系统完成显示一个Activity时会有类似以下的Log D:
03-06 03:36:48.302: INFO/ActivityManager(2486): Displayed activity com.teleca/.ContextMenuActivity: 240 ms (total 41289 ms)
那么启动Activity所用的所有时间=Log D那刻时间 减去Log A 那刻时间。
即:03:36:48.302减去03:36:47.933,简化即为:48.302减去47.933=369ms.
注1:从Log A到Log B为系统所用时间。:
注2:从Log B到Log D主要是activity本身创建所用的时间(on create(),onresume(),onDraw()等)。48.302减去47.954=348ms.有时也包括了系统为创建activity所在应用程序(APK)进程所用的时间。
注3:Log D 的“240 ms ”和“(total 41289 ms)”的意义还不清楚。只是”240ms“和Log C到Log D的时间很接近。
附件1:
一个启动activity的Log:
03-06 03:36:47.865: VERBOSE/InputDevice(2486): ID[0]=0(0) Dn(0=>1)
03-06 03:36:47.865: INFO/PowerManagerService(2486): Ulight 3->7|0
03-06 03:36:47.933: INFO/(2486): PRODUCT_SHIP = false
03-06 03:36:47.933: VERBOSE/InputDevice(2486): ID[0]=0(0) Up(1=>0)
03-06 03:36:47.935: VERBOSE/WindowManager(2486): Dsptch 1 x82.0 y592.0 > Window{485bd6c0 com.sec.android.app.twlauncher/com.sec.android.app.twlauncher.Launcher paused=false}
03-06 03:36:47.954: INFO/ActivityManager(2486): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] 易做图=0x10200000 cmp=com.teleca/.ContextMenuActivity }
03-06 03:36:48.029: INFO/Launcher(2570): onPause()
03-06 03:36:48.044: DEBUG/AudioHardwareALSA(2377): ALSA OPEN mode 0,device 2
03-06 03:36:48.044: INFO/AudioHardwareALSA(2377): Try to open ALSA PLAYBACK device AndroidPlayback_Speaker_normal
03-06 03:36:48.048: WARN/Launcher(2570): Wall易做图Manager setVisibility visible true
03-06 03:36:48.068: INFO/ActivityManager(2486): Start proc com.teleca for activity com.teleca/.ContextMenuActivity: pid=2933 uid=10089 gids={}
03-06 03:36:48.099: INFO/AudioHardwareALSA(2377): Initialized ALSA PLAYBACK device AndroidPlayback_Speaker_normal
03-06 03:36:48.099: DEBUG/AudioHardwareALSA(2377): Set PLAYBACK PCM format to S16_LE (Signed 16 bit Little Endian)
03-06 03:36:48.099: DEBUG/AudioHardwareALSA(2377): Using 2 channels for PLAYBACK.
03-06 03:36:48.099: DEBUG/AudioHardwareALSA(2377): Set PLAYBACK sample rate to 44100 HZ
03-06 03:36:48.099: DEBUG/AudioHardwareALSA(2377): Buffer size: 2048
03-06 03:36:48.099: DEBUG/AudioHardwareALSA(2377): Latency: 46439
03-06 03:36:48.134: INFO/WindowManager(2486): Setting rotation to 1, animFlags=1
03-06 03:36:48.154: INFO/ActivityManager(2486): Config changed: { scale=1.0 imsi=0/0 loc=en_CA touch=3 keys=1/1/2 nav=1/1 orien=2 layout=34 uiMode=17 seq=3 FlipFont=0}
03-06 03:36:48.189: INFO/Launcher(2570): onWindowFocusChanged(false)
03-06 03:36:48.302: INFO/ActivityManager(2486): Displayed activity com.teleca/.ContextMenuActivity: 240 ms (total 41289 ms)
摘自 robin的专栏
补充:移动开发 , Android ,