Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 應用效能測試:用 Instrumentation 量化 Activity 啟動速度

編輯:關於 Android 程式設計

對 Web 而言,頁面載入速度直接影響使用者體驗。如果把 Android 的每個 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();
    }

    @Override
    public void finish(int resultCode, Bundle results) {
        super.finish(resultCode, results);
    }
}

建構子中的 setAutomaticPerformanceSnapshots() 是關鍵,它把 Instrumentation 開啟為「自動擷取效能快照」模式。

LaunchApp() 負責啟動目標 Activity,而 waitForIdleSync() 會等到主執行緒閒置,也就是等 Activity 真正啟動完成才返回。

二、撰寫 ActivityLaunchPerformanceTest

public class ActivityLaunchPerformanceTest extends LaunchPerformanceBase {
    /** 參數名稱,可透過 -e 傳入 instrumentation */
    private static final String launchActivityName = "launch_activity";

    private String classNameForIntent;
    private String DEFAULT_ACTIVITY = "SplashActivity";

    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 中宣告 Instrumentation

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

四、執行測試

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

-e launch_activity 後面接的檔名就是要測的 Activity。

輸出大致如下:

INSTRUMENTATION_RESULT: other_pss=7437
INSTRUMENTATION_RESULT: java_allocated=4839
INSTRUMENTATION_RESULT: global_freed_size=2583696
INSTRUMENTATION_RESULT: native_private_dirty=1684
INSTRUMENTATION_RESULT: global_alloc_count=51608
INSTRUMENTATION_RESULT: other_private_dirty=5468
INSTRUMENTATION_RESULT: global_freed_count=18818
INSTRUMENTATION_RESULT: java_free=2784
INSTRUMENTATION_RESULT: other_shared_dirty=7300
INSTRUMENTATION_RESULT: execution_time=749
INSTRUMENTATION_RESULT: native_size=4772
INSTRUMENTATION_RESULT: cpu_time=678
INSTRUMENTATION_RESULT: java_private_dirty=320
INSTRUMENTATION_RESULT: native_allocated=4690
INSTRUMENTATION_RESULT: gc_invocation_count=5
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,分別代表啟動過程中行程占用的 CPU 時間與實際執行時間,單位是毫秒。其他欄位有興趣可對照 Instrumentation 原始碼。

五、把結果做文字處理

第一步,正規化格式:

adb shell am instrument -e launch_activity HomeActivity \
    -w com.company.example.test/.performance.ActivityLaunchPerformanceTest \
    | sed 's/=/:/' | sed 's/ //' | sed 's/\r//'

把等號換成冒號、去掉空白與 carriage return。

第二步,寫一個 gawk 腳本 txt_to_xml.gawk:

#!/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(time 除以 1000 換算成秒)。完整指令:

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 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 畫成趨勢圖,這樣每次提交的啟動效能變化就一目了然,退化時也能及早發現。

實務上有兩點值得注意:一是同一台裝置連續測量的波動可能不小,建議同一版本跑 5~10 次取中位數;二是首次啟動(冷啟動)與二次啟動(熱啟動)差距明顯,兩者應該分開統計,才有比較意義。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved