函数耗时(Android)
在做性能优化时,我们得先知道要优化的函数的耗时,以便作出更好的优化方案,从函数级别分析app性能瓶颈。
使用指南
函数耗时通过ASM插装实现,当你接入dokit插件时就已经自动集成了函数耗时操作。
默认函数调用栈策略: 启动时默认会将application的attachBaseContext和onCreate的函数耗时打印出来。
效果如下:(过滤TAG:MethodStackUtil)
I: =========DoKit函数调用栈==========
level time function
0********6ms********com/didichuxing/doraemondemo/App&attachBaseContext
I: =========DoKit函数调用栈==========
level time function
0********98ms********com/didichuxing/doraemondemo/App&onCreate
1********50ms*************com/didichuxing/doraemonkit/DoraemonKit&install
2********48ms*****************com/didichuxing/doraemonkit/DoraemonKit&install
3********47ms*********************com/didichuxing/doraemonkit/DoraemonKitReal&install
1********29ms*************com/facebook/drawee/backends/pipeline/Fresco&initialize
2********28ms*****************com/facebook/drawee/backends/pipeline/Fresco&initialize
3********17ms*********************com/facebook/drawee/backends/pipeline/Fresco&initializeDrawee
全局慢函数策略: 在你操作app的过程中,dokit框架会自动统计你的函数执行时长。并在控制台中打印执行时间大于200us(微妙)的函数信息。打印信息示例如下(过滤TAG 为MethodCostUtil):
I: methodName===>com/didichuxing/doraemondemo/MainDebugActivity&test4 threadName==>main thresholdTime===>50 costTime===>201
I: at com.didichuxing.doraemondemo.MainDebugActivity.test4(MainDebugActivity.java:206)
I: at com.didichuxing.doraemondemo.MainDebugActivity.test3(MainDebugActivity.java:196)
I: at com.didichuxing.doraemondemo.MainDebugActivity.test2(MainDebugActivity.java:187)
I: at com.didichuxing.doraemondemo.MainDebugActivity.test1(MainDebugActivity.java:178)
I: at com.didichuxing.doraemondemo.MainDebugActivity.onClick(MainDebugActivity.java:311)
I: at android.view.View.performClick(View.java:6312)
I: at android.view.View$PerformClick.run(View.java:24927)
I: at android.os.Handler.handleCallback(Handler.java:790)
I: at android.os.Handler.dispatchMessage(Handler.java:99)
I: at android.os.Looper.loop(Looper.java:192)
I: at android.app.ActivityThread.main(ActivityThread.java:6866)
I: at java.lang.reflect.Method.invoke(Native Method)
I: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
I: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:817)
参考: 安卓接入指南的插件配置