如何测试 Android 应用程序的性能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1957135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:55:44  来源:igfitidea点击:

How to test the performance of an Android application?

androidperformancetesting

提问by Bharat Pawar

How can I test an application for performance in Android? What is the support provided in Android and how do I use it?

如何在 Android 中测试应用程序的性能?Android 中提供了哪些支持以及如何使用它?

回答by Dave Webb

If you want to profile your application to find performance bottlenecks you can use the traceviewtool. This gives you a graphical view of performance traces of your application.

如果您想分析您的应用程序以查找性能瓶颈,您可以使用该traceview工具。这为您提供了应用程序性能跟踪的图形视图。

To create a trace add the following to your code where you want to start tracing:

要创建跟踪,请将以下内容添加到您要开始跟踪的代码中:

Debug.startMethodTracing("myapp");

and then put the following when you want to stop tracing:

然后在要停止跟踪时输入以下内容:

Debug.stopMethodTracing();

This will create a trace file call myapp.tracein the root directory of the SD Card. As it is written to the SD Card:

这将myapp.trace在 SD 卡的根目录中创建一个跟踪文件调用。当它写入 SD 卡时:

  • If you're using the emulator you'll need to add an SD card to your AVD.
  • You'll need to give you app permission to write the SD card by adding the following to your Manifest:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  • 如果您使用模拟器,则需要向AVD 添加一张 SD 卡
  • 您需要通过将以下内容添加到您的清单来授予您的应用程序写入 SD 卡的权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Once the file has been created you'll need to copy it to your PC. You can do this using the adbcommand:

创建文件后,您需要将其复制到 PC。您可以使用以下adb命令执行此操作:

adb pull /sdcard/myapp.trace c:/my/dir/myapp.trace

Finally, start traceviewgiving it the full pathto the trace file:

最后,开始traceview为其提供跟踪文件的完整路径

traceview c:/my/dir/myapp.trace

I did have some problems with traceviewfailing with OutOfMemoryexceptions. I fixed this on Windows by changing the last line of traceview.batfrom:

我确实遇到了一些traceviewOutOfMemory异常而失败的问题。我通过更改traceview.batfrom的最后一行在 Windows 上修复了这个问题:

call java -Djava.ext.dirs=%javaextdirs% -Dcom.android.traceview.toolsdir= -jar %jarpath% %*

to:

到:

call java -Xmx1g -Djava.ext.dirs=%javaextdirs% -Dcom.android.traceview.toolsdir= -jar %jarpath% %*

Adding the -Xmx1goption allows traceviewto use more memory.

添加该-Xmx1g选项允许traceview使用更多内存。

回答by Samuh

Also, theoretically, DDMS can get memory allocations for your program and then you can analyze the dump using profilers. DDMS Reference.
The reason why I have theoretically in italics is that I myself have not tried doing anything such, yet.

此外,理论上,DDMS 可以为您的程序获取内存分配,然后您可以使用分析器分析转储。 DDMS 参考。
理论上我用斜体的原因是我自己还没有尝试过这样做。

回答by coocood

I think traceView contains too much information, you can easily get lost.

我认为 traceView 包含的信息太多,你很容易迷路。

My solution is just log the system time at three place in the code.

我的解决方案是在代码中的三个位置记录系统时间。

Before and after and center at the potiential slow code.

前后并以潜在的慢代码为中心。

like binary search, next time, narrow it down step by step, then finally find the culprit code.

就像二分查找一样,下一次,一步一步缩小范围,然后最终找到罪魁祸首。

回答by Paarthiban NK

you can use load runner.,

你可以使用负载运行器。,

use this link to find more about it.,

使用此链接可了解更多信息。,

http://www.perftesting.co.uk/recording-and-performance-testing-android-applications-with-hp-loadrunner-vugen/2011/11/14/

http://www.perftesting.co.uk/recording-and-performance-testing-android-applications-with-hp-loadrunner-vugen/2011/11/14/

Steps to be followed are:

要遵循的步骤是:

  1. Create New VuGen Script
  2. Select Mobile Application-HTTP/HTML
  3. Recording Options--> Select Record Emulator
  4. Give the path to Record Emulator as D:\android\AVD Manager.exe
  5. In the command line -avd AVD_NAME -netspeed full -netdelay none where AVD_Name is the name of your Device
  6. select the working directory
  7. click finish
  1. 创建新的 VuGen 脚本
  2. 选择移动应用程序-HTTP/HTML
  3. 录制选项--> 选择录制模拟器
  4. 将 Record Emulator 的路径指定为 D:\android\AVD Manager.exe
  5. 在命令行中 -avd AVD_NAME -netspeed full -netdelay none 其中 AVD_Name 是您的设备名称
  6. 选择工作目录
  7. 点击完成

Now you can perform your test.,

现在你可以执行你的测试了。,

回答by Paarthiban NK

Another way to test is Using TruClient on Load Runner

另一种测试方法是在 Load Runner 上使用TruClient

Steps to be followed for Mobile Web are:

移动 Web 应遵循的步骤是:

  1. New VuGen Script
  2. Mobile Protocol
  3. Select TruClient Mobile Web
  4. Click Create
  5. Now you can generate scripts
  6. Click Develop Script button
  7. PoP up window appears to select the device
  8. Select Actions and you can record the scripts
  1. 新的 VuGen 脚本
  2. 移动协议
  3. 选择 TruClient 移动网络
  4. 点击创建
  5. 现在您可以生成脚本
  6. 单击开发脚本按钮
  7. 弹出窗口出现以选择设备
  8. 选择Actions,就可以录制脚本了

Steps to be followed for Native Mobile are:

本机移动要遵循的步骤是:

  1. New VuGen Script
  2. Mobile Protocol
  3. Select TruClient Native Mobile
  4. Click Create
  5. Now you can generate scripts
  6. click develop script
  7. TruClient window that plugged with Firefox appears
  8. Click General Settings
  9. Configure the SERVER URL PORT
  10. If u don't know the server url port means install OS MONITOR application on your device. here you can find the ip address
  11. Enter User Name and Password
  12. Click done
  1. 新的 VuGen 脚本
  2. 移动协议
  3. 选择 TruClient Native Mobile
  4. 点击创建
  5. 现在您可以生成脚本
  6. 点击开发脚本
  7. 出现插入 Firefox 的 TruClient 窗口
  8. 点击常规设置
  9. 配置服务器 URL 端口
  10. 如果您不知道服务器 url 端口,则意味着在您的设备上安装 OS MONITOR 应用程序。在这里你可以找到IP地址
  11. 输入用户名和密码
  12. 点击完成

you can record the scripts and perform your testing....

您可以记录脚本并执行测试....