我的新浪微博:http://weibo.com/u/1602714773 CSDN博客:http://blog.csdn.net/hunterno4

Android应用性能测试

上一篇 / 下一篇  2014-04-07 21:22:22 / 个人分类:android自动化测试

对于Web网页来说,页面的访问、加载速度对于用户体验来说是很重要的,而如果把Android中的每个Activity都看成是一个页面的话,Activity的启动速度凭主观的话是较难精确衡量的,因此如果可以测试每个Activity的启动速度或者获得其它基本指标并进行日常监测那就更好了。

一、编写继承于Instrumentation类的LaunchPerformanceBase类

  1. /** 
  2. * Base class for all launch performance Instrumentation classes. 
  3. */  
  4. public class LaunchPerformanceBase extends Instrumentation {  
  5.     public static final String TAG = "LaunchPerformanceBase";  
  6.     protected Bundle mResults;  
  7.     protected Intent mIntent;  
  8.       
  9.     /** 
  10.     * Constructor. 
  11.     */  
  12.     public LaunchPerformanceBase() {  
  13.         mResults = new Bundle();  
  14.         mIntent = new Intent(Intent.ACTION_MAIN);  
  15.         mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  16.         setAutomaticPerformanceSnapshots();  
  17.     }  
  18.     /** 
  19.     * Launches intent {@link #mIntent}, and waits for idle before 
  20.     * returning. 
  21.     */  
  22.     protected void LaunchApp() {  
  23.         startActivitySync(mIntent);  
  24.         waitForIdleSync();  
  25.     }  
  26.       
  27.     @Override  
  28.     public void finish(int resultCode, Bundle results) {  
  29.         super.finish(resultCode, results);  
  30.     }  
  31. }  

在这个类的构造函数中setAutomaticPerformanceSnapshots()为Instrumentation设置为开启性能快照功能。

LaunchApp()方法中用于启动相应的Activity, waitForIdleSync()方法则为等待Activity空闲时,即等待Activity启动完毕。

二、编写ActivityLaunchPerformanceTest类

  1. public class ActivityLaunchPerformanceTest extends LaunchPerformanceBase {  
  2.      /** 
  3.         * Outfile argument name. 
  4.         * This argument can be passed to the instrumentation using 
  5.         <code>-e</code>. 
  6.      */  
  7.     private static final String launchActivityName = "launch_activity";  
  8.     /** 
  9.      * Output file name. 
  10.      */  
  11.     private String classNameForIntent;  
  12.       private String DEFAULT_ACTIVITY = "SplashActivity";  
  13.       
  14.     /** 
  15.     * Constructor. 
  16.     */  
  17.     public ActivityLaunchPerformanceTest() {  
  18.         super();  
  19.     }  
  20.       
  21.     @Override  
  22.     public void onCreate(Bundle arguments) {  
  23.         if ( arguments != null ) {  
  24.             classNameForIntent = arguments.getString(launchActivityName);  
  25.             }  
  26.         if ( classNameForIntent == null ) {  
  27.             classNameForIntent = DEFAULT_ACTIVITY ;  
  28.             }  
  29.         super.onCreate(arguments);  
  30.         mIntent.setClassName("com.company.example",  
  31.         "com.company.example.ui.activity." + classNameForIntent);  
  32.         start();  
  33.     }  
  34.       
  35.     /** 
  36.     * Calls LaunchApp and finish. 
  37.     */  
  38.     @Override  
  39.     public void onStart() {  
  40.         super.onStart();  
  41.         LaunchApp();  
  42.         finish(Activity.RESULT_OK, mResults);  
  43.     }  
  44. }  
这个类中onCreate()方法中传入要测试的Activity的名字。

当开始测试时,Instrumentation启动,onStart方法执行时,运行LaunchApp()方法启动被测试的Activity.运行完成后finish。

Instrumentation退出,测试结束。

三、修改AndroidManifest.xml文件

在Manifest文件中增加Instrumentation申明

  1. <instrumentation  
  2.         android:label="Activity Launch Performance"  
  3.         android:name="com.company.example.test.performance.ActivityLaunchPerformanceTest"  
  4.         android:targetPackage="com.company.example" />  
哎,51testing的日志功能又写不下了,有兴趣的请转而访问:

TAG: activity Activity Android android 性能测试 应用

hunterno4的个人空间 引用 删除 hunterno4   /   2014-04-10 18:52:04
哦,好的,希望能对更多人有所启发
51Testing小编的个人空间 引用 删除 zaza9084   /   2014-04-09 10:39:04
您好,我是51Testing软件测试网的编辑,您的本篇博文近日将被推荐至51Testing软件测试网首页发表~
感谢您关注并支持51Testing博客,期待您更多的优秀原创博文。
 

评分:0

我来说两句

Open Toolbar