Android 性能优化之运行时间监测

  以Application为例,在进行项目优化时,我们需要监测该类中所有方法的运行时间,找出最耗时的操作进行分析优化。

  以往比较随意的做法就是在方法前后加上System.currentTimeMillis(),两者数值进行加减得出运行时间,比如:

long start = System.currentTimeMillis();
test1();
long end =  System.currentTimeMillis()-start ;
TBTools.log().i("Running time","运行时间>>>"+end);

 

这样就拿到了test1()的运行时间:

这样在方法数量很少时,可以这么操作,但是试想类中有100个方法时,我们就需要去写100次这样的代码,及其的繁琐,侵入性很强,强耦合的同时代码维护起来也十分的恼火,十分杂乱,如:

long start = System.currentTimeMillis();
test1();
long end =  System.currentTimeMillis()-start ;
TBTools.log().i("Running time","运行>>>"+end);

start = System.currentTimeMillis();
test2();
end =  System.currentTimeMillis()-start ;
TBTools.log().i("Running time","运行>>>"+end);

start = System.currentTimeMillis();
test3();
end =  System.currentTimeMillis()-start ;
TBTools.log().i("Running time","运行>>>"+end);

此时,我们便可以通过AOP(面向切面)来进行此操作,这里我使用的是:AspectJ,当然也有很多其他实现方式,
AOP的好处就是统一管理同一个事件,侵入性很低,还有一些其他好处,可自行百度。

实现方式:

根目录中的build.gradle中添加

dependencies {
    .....
    classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.0'//AOP模式监测
}

app/build.gradle中添加

apply plugin: 'android-aspectjx'

dependencies {
...

compile 'org.aspectj:aspectjrt:1.8.0'

}

完成以上配置步骤后,编写代码:

/**
* 通过使用AOP(切面)来检测方法运行时间
* Created by tanbo on 2019-06-24.
* Hello World!
*/
@Aspect
public class StateMonitor {
    @Around("call(* com.tanbo.rocery_store.APPAplication.**(..))")
    public void getTime(ProceedingJoinPoint joinPoint){
        long start = System.currentTimeMillis();
        try {
            joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        long end =  System.currentTimeMillis()-start ;
        Log.i("APOMonitor",joinPoint.getSignature().toString()+"   启动时间>>>"+end);
    }

}

这里我用的注解方式是前后添加的模式Around,后边参数意思是call(返回值 类名地址.方法(参数))。

这个类因为绑定的Aspect,运行时会自动指向这个类,并运行其中的方法,因此不用再去创建它的示例等操作。

编写完毕后直接运行项目,以上代码运行结果:

会打印出该类中的运行方法耗时,这样我们就可以根据需求进行数据筛选和优化等操作,同时也可将这些时间与我们自己的APM进行对接。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值