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" />  

四、运行Activity启动性能的测试用例

  1. adb shell am instrument -e launch_activity HomeActivity -w com.company.example.test/.performance.ActivityLaunchPerformanceTest  
-e launch_activity参数后指定要测试的Activity名.

测试结果大致如下:

  1. INSTRUMENTATION_RESULT: other_pss=7437  
  2. INSTRUMENTATION_RESULT: java_allocated=4839  
  3. INSTRUMENTATION_RESULT: global_freed_size=2583696  
  4. INSTRUMENTATION_RESULT: native_private_dirty=1684  
  5. INSTRUMENTATION_RESULT: native_free=81  
  6. INSTRUMENTATION_RESULT: global_alloc_count=51608  
  7. INSTRUMENTATION_RESULT: other_private_dirty=5468  
  8. INSTRUMENTATION_RESULT: global_freed_count=18818  
  9. INSTRUMENTATION_RESULT: sent_transactions=-1  
  10. INSTRUMENTATION_RESULT: java_free=2784  
  11. INSTRUMENTATION_RESULT: received_transactions=-1  
  12. INSTRUMENTATION_RESULT: pre_sent_transactions=-1  
  13. INSTRUMENTATION_RESULT: other_shared_dirty=7300  
  14. INSTRUMENTATION_RESULT: pre_received_transactions=-1  
  15. INSTRUMENTATION_RESULT: execution_time=749  
  16. INSTRUMENTATION_RESULT: native_size=4772  
  17. INSTRUMENTATION_RESULT: native_shared_dirty=620  
  18. INSTRUMENTATION_RESULT: cpu_time=678  
  19. INSTRUMENTATION_RESULT: java_private_dirty=320  
  20. INSTRUMENTATION_RESULT: native_allocated=4690  
  21. INSTRUMENTATION_RESULT: gc_invocation_count=5  
  22. INSTRUMENTATION_RESULT: java_shared_dirty=1972  
  23. INSTRUMENTATION_RESULT: global_alloc_size=3883815  
  24. INSTRUMENTATION_RESULT: java_pss=2618  
  25. INSTRUMENTATION_RESULT: java_size=7623  
  26. INSTRUMENTATION_RESULT: native_pss=1699  
  27. INSTRUMENTATION_CODE: -1  
其中意义较大的结果有如cpu_time,execution_time分别代表Activity启动过程中进程占用的cpu时间与实际执行时间,单位毫秒。

其它参数有兴趣的话可参照Instrumentation源码。

五、对测试结果进行文本处理

1.进行格式化处理

  1. adb  shell am instrument -e launch_activity HomeActivity -w com.company.example.test/.performance.ActivityLaunchPerformanceTest | sed 's/=/:/' | sed 's/ //' | sed 's/\r//'  
对测试结果进行=替换为:去除空格等格式化处理

2.编写gawk脚本,名字为txt_to_xml.gawk

  1. #!/bin/bash  
  2. BEGIN{  
  3. print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"  
  4. print "<testsuite>"  
  5. FS=":"  
  6. }  
  7.   
  8. $2 ~ /execution_time|cpu_time/{  
  9. print "<testcase name=\"" $2 "\" time=\"" $NF*0.001  
  10. print "\" />"  
  11. }  
  12. END{  
  13. print "</testsuite>"  
  14. }  
这里只提取需要的 cpu_time, execution_time两个字段的值,并将结果最终生成单元测试格式的xml文件

最终执行测试用例的命令如下:

  1. adb shell am instrument -e launch_activity HomeActivity -w com.company.example.test/.performance.ActivityLaunchPerformanceTest | sed 's/=/:/' | sed 's/ //' | sed 's/\r//' | gawk -f txt_to_xml.gawk > TEST-HomeActivity.xml  
得到的xml结果如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <testsuite>  
  3. <testcase name="execution_time" time="0.939  
  4. />  
  5. <testcase name="cpu_time" time="0.85  
  6. />  
  7. </testsuite>  

六、Jenkins结果展示
测试用例可以使用命令行方式执行,因此也就可以使用Jenkins完成自动化测试,且对于生成的单元测试格式的xml报告,可以使用Jenkins的Performance Plugin 插件进行图表化展示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值