51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1845|回复: 3

【我分享】 Android应用性能测试

[复制链接]
  • TA的每日心情
    慵懒
    2017-7-9 10:38
  • 签到天数: 13 天

    连续签到: 1 天

    [LV.3]测试连长

    发表于 2015-1-6 08:44:00 | 显示全部楼层 |阅读模式
    对于Web网页来说,页面的访问、加载速度对于用户体验来说是很重要的,而如果把Android中的每个Activity都看成是一个页面的话,Activity的启动速度凭主观的话是较难精确衡量的,因此如果可以测试每个Activity的启动速度或者获得其它基本指标并进行日常监测那就更好了。一、编写继承于Instrumentation类的LaunchPerformanceBase类

    [java]

    • /**
    • * Base class for all launch performance Instrumentation classes.
    • */
    • public class LaunchPerformanceBase extends Instrumentation {
    •     public static final String TAG = "LaunchPerformanceBase";
    •     protected Bundle mResults;
    •     protected Intent mIntent;
    •     /**
    •     * Constructor.
    •     */
    •     public LaunchPerformanceBase() {
    •         mResults = new Bundle();
    •         mIntent = new Intent(Intent.ACTION_MAIN);
    •         mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    •         setAutomaticPerformanceSnapshots();
    •     }
    •     /**
    •     * Launches intent {@link #mIntent}, and waits for idle before
    •     * returning.
    •     */
    •     protected void LaunchApp() {
    •         startActivitySync(mIntent);
    •         waitForIdleSync();
    •     }
    •     @Override
    •     public void finish(int resultCode, Bundle results) {
    •         super.finish(resultCode, results);
    •     }
    • }

    在这个类的构造函数中setAutomaticPerformanceSnapshots()为Instrumentation设置为开启性能快照功能。
    LaunchApp()方法中用于启动相应的Activity, waitForIdleSync()方法则为等待Activity空闲时,即等待Activity启动完毕。

    二、编写ActivityLaunchPerformanceTest类

    [java] view plaincopy

    • public class ActivityLaunchPerformanceTest extends LaunchPerformanceBase {
    •      /**
    •         * Outfile argument name.
    •         * This argument can be passed to the instrumentation using
    •         <code>-e</code>.
    •      */
    •     private static final String launchActivityName = "launch_activity";
    •     /**
    •      * Output file name.
    •      */
    •     private String classNameForIntent;
    •       private String DEFAULT_ACTIVITY = "SplashActivity";
    •     /**
    •     * Constructor.
    •     */
    •     public ActivityLaunchPerformanceTest() {
    •         super();
    •     }
    •     @Override
    •     public void onCreate(Bundle arguments) {
    •         if ( arguments != null ) {
    •             classNameForIntent = arguments.getString(launchActivityName);
    •             }
    •         if ( classNameForIntent == null ) {
    •             classNameForIntent = DEFAULT_ACTIVITY ;
    •             }
    •         super.onCreate(arguments);
    •         mIntent.setClassName("com.company.example",
    •         "com.company.example.ui.activity." + classNameForIntent);
    •         start();
    •     }
    •     /**
    •     * Calls LaunchApp and finish.
    •     */
    •     @Override
    •     public void onStart() {
    •         super.onStart();
    •         LaunchApp();
    •         finish(Activity.RESULT_OK, mResults);
    •     }
    • }

    这个类中onCreate()方法中传入要测试的Activity的名字。当开始测试时,Instrumentation启动,onStart方法执行时,运行LaunchApp()方法启动被测试的Activity.运行完成后finish。
    Instrumentation退出,测试结束。

    三、修改AndroidManifest.xml文件
    在Manifest文件中增加Instrumentation申明

    [html]

    • <instrumentation
    •         android:label="Activity Launch Performance"
    •         android:name="com.company.example.test.performance.ActivityLaunchPerformanceTest"
    •         android:targetPackage="com.company.example" />


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

    • adb shell am instrument -e launch_activity HomeActivity -w com.company.example.test/.performance.ActivityLaunchPerformanceTest

    -e launch_activity参数后指定要测试的Activity名.测试结果大致如下:

    [plain]

    • INSTRUMENTATION_RESULT: other_pss=7437
    • INSTRUMENTATION_RESULT: java_allocated=4839
    • INSTRUMENTATION_RESULT: global_freed_size=2583696
    • INSTRUMENTATION_RESULT: native_private_dirty=1684
    • INSTRUMENTATION_RESULT: native_free=81
    • INSTRUMENTATION_RESULT: global_alloc_count=51608
    • INSTRUMENTATION_RESULT: other_private_dirty=5468
    • INSTRUMENTATION_RESULT: global_freed_count=18818
    • INSTRUMENTATION_RESULT: sent_transactions=-1
    • INSTRUMENTATION_RESULT: java_free=2784
    • INSTRUMENTATION_RESULT: received_transactions=-1
    • INSTRUMENTATION_RESULT: pre_sent_transactions=-1
    • INSTRUMENTATION_RESULT: other_shared_dirty=7300
    • INSTRUMENTATION_RESULT: pre_received_transactions=-1
    • INSTRUMENTATION_RESULT: execution_time=749
    • INSTRUMENTATION_RESULT: native_size=4772
    • INSTRUMENTATION_RESULT: native_shared_dirty=620
    • INSTRUMENTATION_RESULT: cpu_time=678
    • INSTRUMENTATION_RESULT: java_private_dirty=320
    • INSTRUMENTATION_RESULT: native_allocated=4690
    • INSTRUMENTATION_RESULT: gc_invocation_count=5
    • INSTRUMENTATION_RESULT: java_shared_dirty=1972
    • INSTRUMENTATION_RESULT: global_alloc_size=3883815
    • INSTRUMENTATION_RESULT: java_pss=2618
    • INSTRUMENTATION_RESULT: java_size=7623
    • INSTRUMENTATION_RESULT: native_pss=1699
    • INSTRUMENTATION_CODE: -1

    其中意义较大的结果有如cpu_time,execution_time分别代表Activity启动过程中进程占用的cpu时间与实际执行时间,单位毫秒。其它参数有兴趣的话可参照Instrumentation源码。


    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    2017-7-9 10:38
  • 签到天数: 13 天

    连续签到: 1 天

    [LV.3]测试连长

     楼主| 发表于 2015-1-6 08:44:17 | 显示全部楼层
    五、对测试结果进行文本处理

    1.进行格式化处理

    [html] view plaincopy在CODE上查看代码片派生到我的代码片

        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

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片

        #!/bin/bash  
        BEGIN{  
        print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"  
        print "<testsuite>"  
        FS=":"  
        }  
          
        $2 ~ /execution_time|cpu_time/{  
        print "<testcase name=\"" $2 "\" time=\"" $NF*0.001  
        print "\" />"  
        }  
        END{  
        print "</testsuite>"  
        }  

    这里只提取需要的cpu_time,execution_time两个字段的值,并将结果最终生成单元测试格式的xml文件

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

    [plain] view plaincopy在CODE上查看代码片派生到我的代码片

        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结果如下:

    [html] view plaincopy在CODE上查看代码片派生到我的代码片

        <?xml version="1.0" encoding="UTF-8"?>  
        <testsuite>  
        <testcase name="execution_time" time="0.939  
        " />  
        <testcase name="cpu_time" time="0.85  
        " />  
        </testsuite>  


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

    使用道具 举报

  • TA的每日心情
    擦汗
    2015-1-15 08:30
  • 签到天数: 11 天

    连续签到: 3 天

    [LV.3]测试连长

    发表于 2015-1-6 13:41:39 | 显示全部楼层
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-3-29 00:28 , Processed in 0.066415 second(s), 25 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表