設定效能測試環境
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以記錄短時間內的裝置活動,以及收集應用程式啟動期間的追蹤記錄,有助於找出潛在的瓶頸,改善整體的應用程式效能。本頁面說明如何設定效能測試環境。
使用 Macrobenchmark 程式庫
Macrobenchmark 程式庫可測量規模較大的使用者互動,例如啟動、與使用者介面互動和動畫。這個程式庫可讓您直接控制要測試的效能環境。藉由控制應用程式的編譯、啟動與停止作業,您可以直接測量應用程式的精確啟動時間,設法盡量減少每次測試執行的噪音和差異。
使用中階裝置找出潛在的效能問題
測試您重視的每一種裝置的效能。在搭載快速元件的高階裝置上,我們可能難以察覺在比較老舊的低 RAM 裝置上會出現的效能問題。低階裝置在載入資料或執行程式碼時可能需要較長時間,所以比較容易找出瓶頸。針對低階裝置進行最佳化效能調整,通常也有助於提升高階裝置的最佳化效果。
減少噪音
- 網路:使用強大穩定的 Wi-Fi 網路,在不同網速下測試您的應用程式 (或程序)。如果應用程式啟動時間包含網路要求,這是可能出現變化的地方,請特別注意。
- RAM 用量:測試應用程式啟動效能時,不要在裝置背景中執行任何其他應用程式。
- 電池:請確保裝置電力充足,避免任何硬體的低功耗效能節流。
運用發布子版本進行測試
請使用發布子版本來測試效能。偵錯版本沒有編譯最佳化措施,且會大幅影響效能,因此不適合用於效能偵錯。
不過,您可以使用未經模糊處理的發布子版本來辨識類別和作業名稱。具體來說,我們建議您在 Proguard 檔案中使用 -dontobfuscate
啟用壓縮 (R8) 並停用模糊處理。如果版本未經過模糊處理,就能更輕鬆地識別版面配置、資產和資源。
請務必在資訊清單中加入可分析的標記,以便在無法進行偵錯的建構作業中看到您的自訂事件。這個標記適用於 Android 10 (API 級別 29) 以上版本。
新增應用程式作業的自訂追蹤記錄
在應用程式中新增自訂追蹤記錄,即可更輕鬆地瞭解應用程式執行了哪些作業,並與其他資料庫進行比較。如此一來,您就能更全面地掌握應用程式在任何時間的動向。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Set up your environment for performance testing\n\nYou can identify potential bottlenecks and improve overall app performance by\nrecording device activity over a short period of time and [collecting traces of\nyour app's startup period](/topic/performance/tracing). This page shows how to\nset up your environment for performance testing.\n\nUse the Macrobenchmark library\n------------------------------\n\nThe [Macrobenchmark\nlibrary](/topic/performance/benchmarking/macrobenchmark-overview) measures\nlarger end-user interactions, such as startup, interacting with the UI, and\nanimations. The library provides direct control over the performance environment\nyou're testing. It lets you control compiling, starting, and stopping your app\nto directly measure precise app startup time. It also works to minimize the\nnoise and differences between test runs.\n\nUse mid-range devices to identify potential performance issues\n--------------------------------------------------------------\n\nTest performance on each device type you care about. High-end devices with fast\ncomponents can hide performance problems on earlier, slower, or low RAM devices.\nLower-end devices can take longer to load data or run code, making it easier to\nidentify bottlenecks. Optimizing performance for low-end devices usually also\nbenefits optimization for high-end devices.\n\nReduce noise\n------------\n\n- Network: test your apps or processes with strong and stable internet Wi-Fi speeds. If the app startup time includes a network request, note this as a place where variability might occur.\n- RAM usage: don't have any other apps running in the background of your device while testing app startup performance.\n- Battery: ensure your device is charged to avoid any hardware-specific low power performance throttling.\n\nTest on release builds\n----------------------\n\nUse release builds to test performance. Debug builds are [unsuitable for\nperformance\ndebugging](/studio/profile/measuring-performance#apk-considerations), as they\ndon't provide compilation optimization and significantly impact performance.\n\nHowever, it's okay to use an unobfuscated release build to identify classes and\noperation names. Specifically, we recommend enabling [minify\n(R8)](/studio/build/shrink-code) and disabling obfuscation, with\n[`-dontobfuscate`](/studio/build/shrink-code#obfuscate) in the proguard file.\nIt's easier to identify layouts, assets, and resources if the build is\nunobfuscated.\n\nMake sure you include the\n[profileable](/guide/topics/manifest/profileable-element) flag in the manifest\nso that your custom events are visible in non-debuggable builds. This flag is\navailable on Android 10 (API level 29) and later.\n\nAdd custom traces to your app operations\n----------------------------------------\n\nAdd [custom traces](/topic/performance/tracing/custom-events) within your app to\nmake it easier to identify what operations are performed by your app compared to\nother libraries. This helps give you more context about what the app is doing at\nall times."]]