获取方法耗时

获取方法耗时

手动打点

1
2
3
long start = System.currentTimeMillis();
// do something...
long cost = System.currentTimeMillis() - start;

方便但是不够优雅,代码侵入性强且工作量大

AOP/Art Hook实现

参考Aspectjx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Aspect
public class PerformanceApp {

@Around("call(* com.benben.starttest.MainActivity.**(..))")
public void getTime(ProceedingJoinPoint joinPoint) {
long start = System.currentTimeMillis();
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
Log.e("time","cost: " + (System.currentTimeMillis() - start));
}
}

参考ART

总结:

无侵入性、推荐使用

注意:

需要将简单函数过滤掉,最好支持黑名单过滤