利用 Android Systrace 做performance 分析

来源:互联网 发布:mysql osx dmg 编辑:程序博客网 时间:2024/05/16 04:48

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文件后, 你可以用如下键盘操作

KeyDescriptionwZoom into the trace timeline.sZoom out of the trace timeline.aPan left on the trace timeline.dPan right on the trace timeline.eCenter the trace timeline on the current mouse location.gShow grid at the start of the currently selected task.Shift+gShow grid at the end of the currently selected task.Right ArrowSelect the next event on the currently selected timeline.Left ArrowSelect the previous event on the currently selected timeline.Double ClickZoom into the trace timeline.Shift+Double ClickZoom 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


0
0
 
 
0 0
原创粉丝点击