函数耗时(Android)

在做性能优化时,我们得先知道要优化的函数的耗时,以便作出更好的优化方案,从函数级别分析app性能瓶颈。

使用指南

函数耗时通过ASM插装实现,当你接入dokit插件时就已经自动集成了函数耗时操作。

默认函数调用栈策略: 启动时默认会将application的attachBaseContext和onCreate的函数耗时打印出来。

效果如下:(过滤TAG:MethodStackUtil)

  1. I: =========DoKit函数调用栈==========
  2. level time function
  3. 0********6ms********com/didichuxing/doraemondemo/App&attachBaseContext
  4. I: =========DoKit函数调用栈==========
  5. level time function
  6. 0********98ms********com/didichuxing/doraemondemo/App&onCreate
  7. 1********50ms*************com/didichuxing/doraemonkit/DoraemonKit&install
  8. 2********48ms*****************com/didichuxing/doraemonkit/DoraemonKit&install
  9. 3********47ms*********************com/didichuxing/doraemonkit/DoraemonKitReal&install
  10. 1********29ms*************com/facebook/drawee/backends/pipeline/Fresco&initialize
  11. 2********28ms*****************com/facebook/drawee/backends/pipeline/Fresco&initialize
  12. 3********17ms*********************com/facebook/drawee/backends/pipeline/Fresco&initializeDrawee

全局慢函数策略: 在你操作app的过程中,dokit框架会自动统计你的函数执行时长。并在控制台中打印执行时间大于200us(微妙)的函数信息。打印信息示例如下(过滤TAG 为MethodCostUtil):

  1. I: methodName===>com/didichuxing/doraemondemo/MainDebugActivity&test4 threadName==>main thresholdTime===>50 costTime===>201
  2. I: at com.didichuxing.doraemondemo.MainDebugActivity.test4(MainDebugActivity.java:206)
  3. I: at com.didichuxing.doraemondemo.MainDebugActivity.test3(MainDebugActivity.java:196)
  4. I: at com.didichuxing.doraemondemo.MainDebugActivity.test2(MainDebugActivity.java:187)
  5. I: at com.didichuxing.doraemondemo.MainDebugActivity.test1(MainDebugActivity.java:178)
  6. I: at com.didichuxing.doraemondemo.MainDebugActivity.onClick(MainDebugActivity.java:311)
  7. I: at android.view.View.performClick(View.java:6312)
  8. I: at android.view.View$PerformClick.run(View.java:24927)
  9. I: at android.os.Handler.handleCallback(Handler.java:790)
  10. I: at android.os.Handler.dispatchMessage(Handler.java:99)
  11. I: at android.os.Looper.loop(Looper.java:192)
  12. I: at android.app.ActivityThread.main(ActivityThread.java:6866)
  13. I: at java.lang.reflect.Method.invoke(Native Method)
  14. I: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
  15. I: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:817)

参考: 安卓接入指南的插件配置