Traceview的使用

Traceview的使用  

 |字号 订阅

Traceview是android平台配备的一个很好的性能分析工具。它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到method。
使用Traceview的版本限制
对于 Android 1.5及以下的版本: 不支持
对于 Android 1.5以上 2.1下(含2.1)的版本: 受限支持。trace文件只能生成到 SD卡,且必须在程序中加入代码。
对于 Android 2.2上(含2.2)的版本:全支持。可以 不用SD卡,不用在程序中加代码,直接自己用DDMS就可以进程Traceview。
一、Android 1.5以上2.1下(含2.1)的版本中Traceview的使用
首先,必须在程序当中加入代码,以便生成trace文件,有了这个trace文件我们才可以将其转化为图形。
1.1、启动追踪
使用Debug的以下静态方法方法来启动:
static void  startMethodTracing(String traceName)
Start method tracing, specifying the trace log file name.
使用指定trace文件的名字和默认最大容量(8M)的方式开始方法的追踪
static void  startMethodTracing()
Start method tracing with default log name and buffer size.
使用默认trace文件的名字(dmtrace.trace)和默认最大容量(8M)的方式开始方法的追踪
static void  startMethodTracing(String traceName, int bufferSize, int flags)
Start method tracing, specifying the trace log file name and the buffer size.
使用指定trace文件的名字和最大容量的方式开始方法的追踪。并可指定flags.
注:int flags好像没意义。一般都用0.
static void  startMethodTracing(String traceName, int bufferSize)
Start method tracing, specifying the trace log file name and the buffer size.
使用指定trace文件的名字和最大容量的方式开始方法的追踪。
注1:以上的方法的文件都会创建于SD卡下,即"/sdcard/"下,对默认文件名的就是"/sdcard/dmtrace.trace"
如果没SD卡,以上方法会抛异常致使程序crash.
注2:如果文件名没有指定类型,系统为其加上类型.trace
1.2、停止追踪
使用Debug的静态方法方法来停止:
public static void  stopMethodTracing ()。
例如在activity的onCreate()中添加Debug.startMethodTracing(), 而在onDestroy()中添加Debug.stopMethodTracing(),
如下:
     @Override
     public void   onCreate (Bundle savedInstanceState) {
      Debug.startMethodTracing();
        super.onCreate(savedInstanceState);
      ..............................
     }
protected void   onDestroy () {
super.onDestroy();
.................
Debug.stopMethodTracing();

}
对于模拟器我们还得创建一个带有SD card的AVD,这样才能使trace文件保存到/sdcard/...当中。
可以在命令中分别单独创建,也可以在创建avd的时候一起将sdcard创建。创建之后通过DDMS file explore我们就可以看到/sdcard/目录下有一个trace文件,如果没有在Debug语句中设置名字则默认为dmtrace.trace.
注意:需要在 AndroidManifest.xml文件中添加 android.permission.WRITE_EXTERNAL_STORAGE权限。
1.3、 把trace文件从SD卡拷到电脑上
现在我们把这个文件copy到我们的电脑上指定的目录:
adb pull /sdcard/dmtrace.trace d:
1.4、启动traceview可视界面
现在就可以通过命令行来执行traceview了。进入SDK的tools目录后,执行traceview,如下: 
traceview D:\dmtrace.trace.
之后即可以看到图形界面了。
1.5、分析traceview结果
Timeline Panel
窗口的上半部分是时间轴面图(Timeline Panel)
The image below shows a close up of the timeline panel. Each thread’s execution is shown in its own row, with time increasing to the right. 
Each method is shown in another color (colors are reused in a round-robin fashion starting with the methods that have the most inclusive time). 
The thin lines underneath the first row show the extent (entry to exit) of all the calls to the selected method.
界面上方的尺子代表了MethodTracing的时间段(从Debug.startMethodTracing()到Debug.stopMethodTracing()的时间)。
每个线程的函数执行 时间图 处于和 线程名同一行 的右侧。
注1线宽度代表执行该函数本身操作所用的 时间
注2:函数所调用的 子函数时间线夹杂在该函数 本身操作所用的时间线之间。
注3:时间线的 高度不知道有什么意义。
注4:函数本身是 嵌套的。
注5每行下面粗的线段标注了Profile Panel中被选中函数调用所消耗的时间段。每个线段对应一次函数的运行。
在下面的图中我们可以看到有11次对LoadListener.nativeFinished()的调用,其中有一次调用耗时特别的多。这里的11次和图2并不一致,应该是google的疏忽。
图1The Traceview Timeline Panel
Traceview的使用 - hubingforever - 民主与科学
 
Profile Panel
窗口的下半界面是对各个函数调用的汇总图Profile Panel
  Figure 2 shows the profile pane, a summary of all the time spent in a method. 
The table shows both the inclusive and exclusive times (as well as the percentage of the total time). 
Exclusive time is the time spent in the method. 
Inclusive time is the time spent in the method plus the time spent in any called functions. 
We refer to calling methods as "parents" and called methods as "children." When a method is selected (by clicking on it), 
it expands to show the parents and children. Parents are shown with a purple background and children with a yellow background. 
The last column in the table shows the number of calls to this method plus the number of recursive calls. 
The last column shows the number of calls out of the total number of calls made to that method. 
In this view, we can see that there were 14 calls to LoadListener.nativeFinished(); 
looking at the timeline panel shows that one of those calls took an unusually long time.
图2是对各个函数调用的 汇总图Profile Panel。该表给出了the inclusive and exclusive times及他们所占有的百分比。
Exclusive time 是该函数本身基本操作不包括子函数调用)的时间。
Inclusive time 是该函数调用所用的时间(包括子函数调用)的时间。
列1:" Name"表示函数名。
双击 函数名,可以看到在上半界面是时间轴面图(Timeline Panel)看他的所消耗的时间段。(用粗的线段标注)。
双击函数名左边的" +"展开后可以看到,该函数的" parents"和" children"
列2"incl%"表示函数的 Inclusive time整个MethodTracing时间里占的 百分比
列3:" Inclusive"表示 Inclusive time
列4:" Excl%"表示函数的 Exclusive time整个MethodTracing时间里占的 百分比
列5:" Exclusive"表示 Exclusive time
列6:"Calls+RecurCalls/Total"表示对函数的 调用次数包括递归调用)。如图2的nativeFinished()那列为"14+0"表示14次非递归调用,0次递归调用.
列7:新的版本(比如2.1)还有" time/calls"表示 平均的调用时间(即Inclusive time/ total calls)。如图3。来自google文档的图2感觉有老了。
:如果函数A调用函数B那么函数A称为函数B的" parents",函数B称为函数A的" children."
图2Profile Panel Traceview的使用 - hubingforever - 民主与科学
图3Profile Panel
Traceview的使用 - hubingforever - 民主与科学
 
二、Android 2.2以上(含2.2)的版本中Traceview的简化使用。
Android 2.2除了像2.1当中那样使用Traceview,还可以在 DDMS使用Traceview,比以前简化的不少。
* The trace log files are streamed directly to your development machine.
在Android 2.2后可以不用SD卡。 Traceview 文件流是直接指向开发调试的主机(即DDMS所在的电脑)
To start method profiling:
   1. On the Devices tab, select the process that you want to enable method profiling for.
   2. Click the Start Method Profiling button.
   3. Interact with your application to start the methods that you want to profile.
   4. Click the Stop Method Profiling button. DDMS stops profiling your application and opens Traceview with the method profiling information that was collected between the time you clicked on Start Method Profiling and Stop Method Profiling.
 在DDMS中如何进行Traceview。
  1,在设备表中 选中你想进行method trace的 进程
  2,单击 Method Profiling按钮开始method trace。该按钮在窗口的左上方,它在"stop"按钮的左方,"Device菜单的正下方"。
  3,method trace 进行中
  4,单击Method Profiling 按钮停止method trace。紧接着系统会自动弹出Traceview窗口来显示刚才的method trace结果。
三、Traceview文件格式
本文参照来源:
http://wbdban.javaeye.com/blog/564309
http://developer.android.com/guide/developing/debugging/debugging-tracing.html
http://developer.android.com/guide/developing/debugging/ddms.html#profiling
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值