现在的位置: 首页 > 移动开发 > 正文

利用 Android Systrace 做performance 分析

2019年10月09日 移动开发 ⁄ 共 2171字 ⁄ 字号 评论关闭

Systrace 是Android4.1 之后推出的。对系统性能分析的工具

systrace 可以通过命令启动,或者使用eclipse, android studio 都有。

systrace 工具你可以在sdk/platform-tools/ 找到,  或者在源码里位于 external/chromium-trace  下面

一、systrace 使用方法

你可以通过python systrace.py -h 来查看systrace 的使用帮助

Usage: systrace.py [options] [category1 [category2 ...]]

Example: systrace.py -b 32768 -t 15 gfx input view sched freq

Options:
  -h, --help            show this help message and exit
  -o FILE               write HTML to FILE
  -t N, --time=N        trace for N seconds
  -b N, --buf-size=N    use a trace buffer size of N KB
  -k KFUNCS, --ktrace=KFUNCS
                        specify a comma-separated list of kernel functions to
                        trace
  -l, --list-categories
                        list the available categories and exit
  -a APP_NAME, --app=APP_NAME
                        enable application-level tracing for comma-separated
                        list of app cmdlines
  --link-assets         link to original CSS or JS resources instead of
                        embedding them
  --from-file=FROM_FILE
                        read the trace from a file (compressed) rather than
                        running a live trace
  --asset-dir=ASSET_DIR
  -e DEVICE_SERIAL, --serial=DEVICE_SERIAL
                        adb device serial number

生成的trace 文件 需要Chrome 来打开。

二、trace 文件的分析

          打开trace文件后, 你可以用如下键盘操作

Key Description
w Zoom into the trace timeline.
s Zoom out of the trace timeline.
a Pan left on the trace timeline.
d Pan right on the trace timeline.
e Center the trace timeline on the current mouse location.
g Show grid at the start of the currently selected task.
Shift+g Show grid at the end of the currently selected task.
Right Arrow Select the next event on the currently selected timeline.
Left Arrow Select the previous event on the currently selected timeline.
Double Click Zoom into the trace timeline.
Shift+Double Click Zoom out of the trace timeline.

你可以找到你要分析的进程ID, 分析每个线程在一段时间类具体做什么工作。如果你发现某个方法耗用了很长时间。 可以去代码里面搜索去具体的实现。

framework 里面 已经有很多trace 的代码。

三、在代码中加入trace

      你可以自己在一些方法里加入trace 方便自己 跟踪调试 ,  如下:

     Trace.traceBegin(Trace.TRACE_TAG_VIEW, "performTraversals");
            try {
                             

            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_VIEW);
            }

       你需要保证 traceBegin 与 traceEnd 一定要成对出现。  并且一定要在同一个线程里面。

你还可以参考:

http://developer.android.com/tools/debugging/systrace.html

http://developer.android.com/tools/help/systrace.html

抱歉!评论已关闭.