編輯:關於 Android 程式設計
對 Web 而言,頁面載入速度直接影響使用者體驗。如果把 Android 的每個 Activity 都當成一個頁面,那啟動速度就是對應的指標。問題是主觀感受很難精確衡量,因此更理想的做法是:把啟動速度變成可量測的數字,並且能納入日常監測。
/**
* 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 真正啟動完成才返回。
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 退出,測試結束。
<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 做自動化。產生的單元測試格式 XML 報告,可以透過 Jenkins 的 Performance Plugin 畫成趨勢圖,這樣每次提交的啟動效能變化就一目了然,退化時也能及早發現。

實務上有兩點值得注意:一是同一台裝置連續測量的波動可能不小,建議同一版本跑 5~10 次取中位數;二是首次啟動(冷啟動)與二次啟動(熱啟動)差距明顯,兩者應該分開統計,才有比較意義。
Android 使用 GET / POST 存取 HTTP 伺服器完整實作
曾經我覺得 Android 的網路通訊有點神奇,直到把網路基礎補起來,才發現一切都很有條理。這篇就透過一個小 Demo,把 GET 與 POST 兩種提交方式講清楚。De
Android 百度地圖集成實戰:路線規劃與導航
關於百度地圖的導航功能曾困擾我很長一段時間,現在終於整理出來了,來給大家分享一下。其實並不難,把百度的 API 範例 Demo 下載下來,找到需要的部分即可。使用前當然要
Android 音樂播放器:循環模式與專輯封面倒影實現
功能概述 這次實作 Android 音樂播放器,包含順序播放、隨機播放、單曲循環,以及播放頁面的專輯封面倒影效果。 播放服務與循環模式 PlayService 使用
Android 多線程斷點下載的實作原理與技巧
多線程下載是提升下載速度的常見手段——透過開啟多個線程共同處理同一個任務,讓整體執行效率顯著提升。我們手機裡的應用商店下載器,背後多半就是這樣的機制,而且還支援斷點續傳: