android团战兵器Android知识Android开发

性能优化工具知识梳理(1) - TraceView

2017-03-25  本文已影响477人  泽毛

性能优化工具知识梳理(1) - TraceView
性能优化工具知识梳理(2) - Systrace
性能优化工具知识梳理(3) - 调试GPU过度绘制 & GPU呈现模式分析
性能优化工具知识梳理(4) - Hierarchy Viewer
性能优化工具知识梳理(5) - MAT
性能优化工具知识梳理(6) - Memory Monitor & Heap Viewer & Allocation Tracker
性能优化工具知识梳理(7) - LeakCanary
性能优化工具知识梳理(8) - Lint

一、概述

使用Systrace需要开发者对于整个渲染的原理有较深的理解,而TraceView则更为直观,你可以通过它来分析在一段时间应用内各个线程的运行情况,它会帮你计算出每个方法的具体耗时,这样我们就可以了解到方法运行的效率,定位到当前性能的瓶颈在哪,从而考虑将一些耗时的操作放在子线程中进行,或者延后执行来优化应用的启动速度和解决卡顿问题。
和之前一样,我们分几个部分介绍它:

二、获取*.trace报表

三、分析*.trace报表

获取完报表之后,我们就可以通过它来分析,这个区域分为三个部分:


前面两个区域都比较好理解,我们主要看一下紫色区域的每一列具体的含义:


上面的表格中,一部分单位是百分比,另一部分是ms,要注意区别,除此之外,有两点需要解释一下:

    public static void inclExcl(Context context) { //这个函数的运行时间为 Incl Real Time
        //假如这里没有调用任何函数,那么这里的运行时间就是:Excl Real Time
        writeSomething(context, 1000); //这里的运行时间是:Incl Real Time - Excl Real Time
    }

我们可以通过点击具体的列进行排序,在平时的分析中,我们主要关注一下:

四、具体案例

我们分析一下在启动过程中,由于布局复杂或者耗时操作导致的问题:
首先我们看一下正常的情况:

4.1 布局层次过于复杂导致的问题

我们修改Activity的根布局:

<FrameLayout
        android:background="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:background="@android:drawable/screen_background_dark"
            android:layout_width="match_parent"
            android:layout_height="500dp">
            <FrameLayout
                android:background="@android:drawable/star_big_off"
                android:layout_width="match_parent"
                android:layout_height="400dp">
                <FrameLayout
                    android:background="@android:drawable/bottom_bar"
                    android:layout_width="match_parent"
                    android:layout_height="300dp">
                    <FrameLayout
                        android:background="@android:color/holo_blue_bright"
                        android:layout_width="match_parent"
                        android:layout_height="200dp">
                    </FrameLayout>
                </FrameLayout>
            </FrameLayout>
        </FrameLayout>
</FrameLayout>

再次抓取之后,看一下onCreate的时间,发现耗时增加到17ms


之后再点击这个函数,分析里面耗时最长的子方法,可以看到就是由于布局引起的:

4.2 在启动过程中进行耗时的操作

首先把布局恢复成没有问题的状态,然后在onCreateonResume方法中增加不同的IO操作:

public class TraceViewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trace_view);
        TraceViewOperation.writeOnActivityOnCreate(this, 1000);
    }

    @Override
    protected void onResume() {
        super.onResume();
        TraceViewOperation.writeOnActivityOnResume(this, 1000);
    }

}

其中onCreate在主线程中进行,而onResume里面则另起了一个线程:

public class TraceViewOperation {

    public static void writeOnActivityOnCreate(Context context, int count) {
        writeSomething(context, count);
    }
    
    public static void writeOnActivityOnResume(final Context context, final int count) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                writeSomething(context, count);
            }
        }.start();
    }

}

五、小结

通过TraceView,我们可以了解到某些关键函数上的运行时间,正如前面第三点谈到的,根据不同的情况进行分析和优化,将会有效地提高应用的启动速度,避免出现卡顿现象。


更多文章,欢迎访问我的 Android 知识梳理系列:

上一篇下一篇

猜你喜欢

热点阅读