美文网首页「性能优化」
「性能优化1.1」计算方法的执行时间

「性能优化1.1」计算方法的执行时间

作者: 未见哥哥 | 来源:发表于2019-03-17 21:13 被阅读12次

    「性能优化1.0」启动分类及启动时间的测量
    「性能优化1.1」计算方法的执行时间

    一、计算方法的执行时间

    我们在前面的几篇博客中分析了如何获取应用的启动时间,我们也知道启动过程中我们能优化的方向就是 Application 和 Activity 的生命周期,那么我们现在就来计算在启动过程 Application 中每一个调用每一个方法的执行耗时。

    1.1、常规方案

    手动埋点的方式,这种方案就是跟我们埋点获取应用启动时间是一样的原理,只要在方法执行前和执行后埋点,然后计算这两个埋点的时间差值就是该方法执行的耗时时间了。

    具体的埋点实现如下代码所示,在 Application 中 onCreate 方法中调用了很多初始化的方法,我们通过埋点的方式在每一个调用的方法内部都去计算其方法的耗时时间。

    //Application.java
    @Override
    public void onCreate() {
        initImageLoader();
        initBugly();
        initSharedPreference();
        //.....
    }
    
    private void initImageLoader() {
        long startTime = System.currentTimeMillis();
        Fresco.initialize(this);
        Log.d(TAG, "initImageLoader cost :" + (System.currentTimeMillis() - startTime));
    
    private void initBugly() {
        long startTime = System.currentTimeMillis();
        //init bugly
        Log.d(TAG, "initBugly cost :" + (System.currentTimeMillis() - startTime));    
    }
    
    private void initSharedPreference() {
        long startTime = System.currentTimeMillis();
        //init SharedPreference
        Log.d(TAG, "initSharedPreference cost :" + (System.currentTimeMillis() - startTime));    
    }
    

    我们可以看到,以上这种方法是我们第一个想到的实现方式,但是也是缺点也是很明显:

    • 代码很不优雅,每一个方法都需要进行埋点。
    • 对项目的侵入性很大,并且工作量也很大。

    1.2、AOP方式来获取

    1.2.1、基本概念

    AOP 就是我们常说的面向切面编程,它可以针对同一类问题进行统一处理

    下面我们就来通过 AOP 的方式来优雅的获取Application每一个方法的执行时间。

    1.2.2、引入 AspectJ

    在 Android 中通过 AspectJ 这个库来使用 AOP ,接下来来引入这个库:

    • 在工程根 build.gradle 中引入 AspectJ 插件

    classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4'

    • 在 build.gradle 应用插件

    apply plugin: 'android-aspectjx'

    • 在 build.gradle 中引入 aspectj 库

    implementation 'org.aspectj:aspectjrt:1.8.9'

    好了通过以上几步,我们就快速的接入的 AspectJ 这个库了。

    1.2.3、具体使用

    • 定义一个类PerformanceAop使用@Aspect注解
    • @Around("execution(* com.example.perfermance.aop.MyApplication.**(..))")表示需要对 MyApplication中的每一个方法都做 hook 处理。
    • 记录方法执行前后的时间戳,并计算对应的时间差。

    注意: 关于 AspectJ 详细使用,读者可以去网上查阅资料,目前笔者只是实现计算方法的耗时计算,详细的操作并没有展开分析。

    @Aspect
    public class PerformanceAop {
    
        private static final String TAG = PerformanceAop.class.getSimpleName();
    
        @Around("execution(* com.example.perfermance.aop.MyApplication.**(..))")
        public void getTime(ProceedingJoinPoint joinPoint) {
            Signature signature = joinPoint.getSignature();
            //得到方法的名字,例如:MyApplication.attachBaseContext(..)
            String name = signature.toShortString();
            //记录方法执行前的时间戳
            long startTime = System.currentTimeMillis();
    
            try {
                //执行该方法
                joinPoint.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            //记录执行该方法的时间
            long costTime = System.currentTimeMillis() - startTime;
    
            Log.e(TAG, "method " + name + " cost:" + costTime);
        }
    }
    

    程序运行的结果:

    2019-03-17 20:29:12.946 10094-10094/com.example.perfermance E/PerformanceAop: method MyApplication.attachBaseContext(..) cost:1
    2019-03-17 20:29:12.979 10094-10094/com.example.perfermance E/PerformanceAop: method MyApplication.initBugly() cost:12
    2019-03-17 20:29:13.002 10094-10094/com.example.perfermance E/PerformanceAop: method MyApplication.initImageLoader() cost:23
    2019-03-17 20:29:13.002 10094-10094/com.example.perfermance E/PerformanceAop: method MyApplication.onCreate() cost:35
    

    二、总结

    我们在本篇文章主要是从常规的方式和 AOP 的方式来计算方法的耗时计算,而常规实现方案可以看出代码很不优雅,每一个方法都需要进行埋点并且对项目的侵入性很大,工作量也很大。而 AOP 这种方式是一个比较优雅的方式实现的,它对已有代码是零侵入性的,修改也方便。

    记录于 2019年3月17日

    相关文章

      网友评论

        本文标题:「性能优化1.1」计算方法的执行时间

        本文链接:https://www.haomeiwen.com/subject/chspmqtx.html