Systrace自定义TAG使用
2020-05-17 本文已影响0人
GarveyLian
自定义Trace TAG
使用Trace.beginSection()
和 Trace.endSection()
定义代码段
@Override
protected void onCreate(Bundle savedInstanceState) {
Trace.beginSection("MainActivity.onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.rv_main);
Trace.beginSection("MainActivity.initData");
initData();
Trace.endSection();
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
mAdapter = new MyAdapter(this, mData);
mRecyclerView.setAdapter(mAdapter);
Trace.endSection();
}
其中Trace.endSection();
与最近的Trace.beginSection("SelfTag");
配对结束。
使用命令
首先安装Python2环境,然后在Android Sdk里的\platform-tools\systrace 目录下有systrace.py 脚本,执行下面的命令,注意-a 参数写自己的进程名字,-o 参数写自己的输出文件名:
python systrace.py -a com.xxx.xxx -o xxx.html sched freq idle am wm gfx view binder_driver

脚本开始Starting 后就可以操作你的应用,比如滑动界面等。结束操作后按Enter结束Systrace 的抓取(也可以通过设置 -t 参数设置自动抓取停止时间例如-t 10
抓取10秒,默认-t
时间是5秒)。
抓取完成后在对应目录会生成一个html格式的报告

分析报告
通过Chrome浏览器可以直接打开报告html文件

可以通过右上角搜索框(快捷键:
/
)输出你自定义的Tag,按Enter
快速选择其中一个或者通过右上角的左右箭头选择上一个下一个。通过上述步骤选中其中一个后可以通过快捷键
M
快速定位对应Frame
中的位置,并查看相应的执行时间与连续帧的位置。