Android程序性能测试工具

来源:互联网 发布:成都专业网络推广公司 编辑:程序博客网 时间:2024/05/27 09:46
Traceview是android平台配备的一个很好的性能分析工具。它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到method。

进行Traceview的版本限制
对于Android 1.5及以下的版本:不支持
对于Android1.5以上2.1下(含2.1)的版本:受限支持。trace文件只能生成到SD卡,且必须在程序中加入代码。

对于Android2.2上(含2.2)的版本:全支持。可以不用SD卡,不用在程序中加代码,直接自己用DDMS就可以进程Traceview。

Android 1.5以上2.1下(含2.1)的版本中Traceview的使用
首先,必须在程序当中加入代码,以便生成trace文件,有了这个trace文件我们才可以将其转化为图形。

A启动
使用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, intbufferSize, int flags)
Start method tracing, specifying the trace log file name and thebuffer size.

使用指定trace文件的名字和最大容量的方式开始方法的追踪。并可指定flags.
注:int flags好像没意义。一般都用0.
static void
startMethodTracing(String traceName, intbufferSize)
Start method tracing, specifying the trace log file name and thebuffer size.

使用指定trace文件的名字和最大容量的方式开始方法的追踪。
注1:以上的方法的文件都会创建于SD卡下,即"/sdcard/"下,对默认文件名的就是"/sdcard/dmtrace.trace"
如果没SD卡,以上方法会抛异常致使程序crash.
注2:如果文件名没有指定类型,系统为其加上类型.trace

B停止
使用Debug的静态方法方法来停止:
public static void stopMethodTracing ()。
例如在activity的onCreate()中添加Debug.startMethodTracing(),而在onDestroy()中添加 Debug.stopMethodTracing(),

如下:

java代码:
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. Debug.startMethodTracing();
  4. super.onCreate(savedInstanceState);
  5. ..............................
  6. }
  7. protected void onDestroy() {
  8. super.onDestroy();
  9. .................
  10. Debug.stopMethodTracing();


  11. }
复制代码


对于模拟器我们还得创建一个带有SD card的AVD,这样才能使trace文件保存到/sdcard/...当中。
可以在命令中分别单独创建,也可以在创建avd的时候一起将sdcard创建。创建之后通过DDMS fileexplore我们就可以看到/sdcard/目录下有一个trace文件,如果没有在Debug语句中设置名字则默认为dmtrace.trace.

C 把trace文件从SD卡拷到电脑上
现在我们把这个文件copy到我们的电脑上指定的目录:
adb pull /sdcard/dmtrace.trace d:

D 拷贝traceview文件到电脑
现在就可以通过命令行来执行traceview了。进入SDK的tools目录后,执行traceview,如下:
traceview D:\dmtrace.trace.之后即可以看到图形界面了。

E 分析traceview结果
Timeline Panel
窗口的上半部分是时间轴面图(Timeline Panel)
The image below shows a close up of the timeline panel. Eachthread’s execution is shown in its own row, with time increasing tothe right.

Each method is shown in another color (colors are reused in around-robin fashion starting with the methods that have the mostinclusive time).
The thin lines underneath the first row show the extent (entry toexit) of all the calls to the selected method.
界面上方的尺子代表了MethodTracing的时间段(从Debug.startMethodTracing()到Debug.stopMethodTracing()的时间)。

 

 

网址:http://www.cnblogs.com/devinzhang/archive/2011/12/18/2291592.html

原创粉丝点击