Android應用效能測試

來源:互聯網
上載者:User

對於Web網頁來說,頁面的訪問、載入速度對於使用者體驗來說是很重要的,而如果把Android中的每個Activity都看成是一個頁面的話,Activity的啟動速度憑主觀的話是較難精確衡量的,因此如果可以測試每個Activity的啟動速度或者獲得其它基本指標並進行日常監測那就更好了。

一、編寫繼承於Instrumentation類的LaunchPerformanceBase類

/*** 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();}@Overridepublic void finish(int resultCode, Bundle results) {super.finish(resultCode, results);}}

在這個類的建構函式中setAutomaticPerformanceSnapshots()為Instrumentation設定為開啟效能快照功能。

LaunchApp()方法中用於啟動相應的Activity, waitForIdleSync()方法則為等待Activity空閑時,即等待Activity啟動完畢。

二、編寫ActivityLaunchPerformanceTest類

public class ActivityLaunchPerformanceTest extends LaunchPerformanceBase { /**    * Outfile argument name.    * This argument can be passed to the instrumentation using    -e. */    private static final String launchActivityName = "launch_activity";    /**     * Output file name.     */    private String classNameForIntent;  private String DEFAULT_ACTIVITY = "SplashActivity";/*** Constructor.*/public ActivityLaunchPerformanceTest() {    super();}@Overridepublic 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.*/@Overridepublic void onStart() {super.onStart();LaunchApp();finish(Activity.RESULT_OK, mResults);}}
這個類中onCreate()方法中傳入要測試的Activity的名字。

當開始測試時,Instrumentation啟動,onStart方法執行時,運行LaunchApp()方法啟動被測試的Activity.運行完成後finish。

Instrumentation退出,測試結束。

三、修改AndroidManifest.xml檔案

在Manifest檔案中增加Instrumentation申明



四、運行Activity啟動效能的測試案例

adb shell am instrument -e launch_activity HomeActivity -w com.company.example.test/.performance.ActivityLaunchPerformanceTest
-e launch_activity參數後指定要測試的Activity名.

測試結果大致如下:

INSTRUMENTATION_RESULT: other_pss=7437INSTRUMENTATION_RESULT: java_allocated=4839INSTRUMENTATION_RESULT: global_freed_size=2583696INSTRUMENTATION_RESULT: native_private_dirty=1684INSTRUMENTATION_RESULT: native_free=81INSTRUMENTATION_RESULT: global_alloc_count=51608INSTRUMENTATION_RESULT: other_private_dirty=5468INSTRUMENTATION_RESULT: global_freed_count=18818INSTRUMENTATION_RESULT: sent_transactions=-1INSTRUMENTATION_RESULT: java_free=2784INSTRUMENTATION_RESULT: received_transactions=-1INSTRUMENTATION_RESULT: pre_sent_transactions=-1INSTRUMENTATION_RESULT: other_shared_dirty=7300INSTRUMENTATION_RESULT: pre_received_transactions=-1INSTRUMENTATION_RESULT: execution_time=749INSTRUMENTATION_RESULT: native_size=4772INSTRUMENTATION_RESULT: native_shared_dirty=620INSTRUMENTATION_RESULT: cpu_time=678INSTRUMENTATION_RESULT: java_private_dirty=320INSTRUMENTATION_RESULT: native_allocated=4690INSTRUMENTATION_RESULT: gc_invocation_count=5INSTRUMENTATION_RESULT: java_shared_dirty=1972INSTRUMENTATION_RESULT: global_alloc_size=3883815INSTRUMENTATION_RESULT: java_pss=2618INSTRUMENTATION_RESULT: java_size=7623INSTRUMENTATION_RESULT: native_pss=1699INSTRUMENTATION_CODE: -1
其中意義較大的結果有如cpu_time,execution_time分別代表Activity啟動過程中進程佔用的cpu時間與實際執行時間,單位毫秒。

其它參數有興趣的話可參照Instrumentation源碼。

五、對測試結果進行文本處理

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

#!/bin/bashBEGIN{print ""print ""FS=":"}$2 ~ /execution_time|cpu_time/{print ""}END{print ""}
這裡只提取需要的cpu_time,execution_time兩個欄位的值,並將結果最終產生單元測試格式的xml檔案

最終執行測試案例的命令如下:

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



六、Jenkins結果展示
測試案例可以使用命令列方式執行,因此也就可以使用Jenkins完成自動化測試,且對於產生的單元測試格式的xml報告,可以使用Jenkins的Performance Plugin外掛程式進行圖表化展示:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.