為應用程式進行基準測試
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
基準測試可用來檢查及監控應用程式效能。您可以定期執行基準測試,協助分析效能問題並偵錯,確保近期異動不會造成迴歸。
Android 提供 Macrobenchmark 和 Microbenchmark 這兩種基準測試程式庫和方法,可分析及測試不同的應用程式情況。
Macrobenchmark
Macrobenchmark 程式庫可測量規模較大的使用者互動,例如啟動作業、UI 操作和動畫。您可以利用這個程式庫直接控制想測試的效能環境,例如控制編譯作業,以及啟動和停止應用程式,方便直接測量實際的應用程式啟動或捲動情形。
Macrobenchmark 程式庫會利用測試期間建構的測試應用程式,從外部插入事件並監控結果。因此,您編寫基準測試時,不需要直接呼叫應用程式程式碼,只要像使用者一樣在應用程式中瀏覽即可。
Microbenchmark
利用 Microbenchmark 程式庫,您可以在迴圈中直接對應用程式程式碼執行基準測試。這個程式庫可測量用來評估最佳情況效能的 CPU 工作,例如為及時 (JIT) 編譯作業暖機、快取磁碟存取權等,您或許可透過內部迴圈或特定熱函式查看這類工作。這個程式庫只能測量可單獨直接呼叫的程式碼。
如果應用程式需要處理複雜的資料結構,或是會在應用程式執行期間多次呼叫需要大量運算的演算法,或許就適合執行基準測試。您也可以測量 UI 的某些部分。舉例來說,您可以測量 RecyclerView
項目繫結成本、版面配置的加載時間,或從效能角度瞭解 View
類別的版面配置和測量傳遞需求量。
不過,您無法衡量接受基準測試評估的情況對整體使用者體驗的改善效果。在某些情況下,基準測試無法說明能否改善卡頓或應用程式啟動時間等瓶頸。因此,請務必先使用 Android 分析器找出這些瓶頸。找到要調查和最佳化的程式碼後,基準測試迴圈就能更簡單又快速地重複執行,產生雜訊較少的結果,讓您專注改善一個方面。
Microbenchmark 程式庫只會回報應用程式的資訊,不會回報整個系統的資訊。因此,這個程式庫最適合分析應用程式本身情況的效能,不適合分析可能涉及整體系統問題的情況。
基準測試程式庫比較
|
Macrobenchmark |
Microbenchmark |
API 版本 |
23 以上版本 |
14 以上版本 |
功能 |
測量高階進入點或互動,例如啟動活動或捲動清單。 |
測量個別功能。 |
範圍 |
對完整應用程式的程序外測試。 |
對 CPU 工作的程序內測試。 |
速度 |
中等疊代速度,可能超過一分鐘。 |
快速疊代速度,通常不到 10 秒。 |
追蹤 |
結果包含分析追蹤記錄。 |
可選用方法取樣和追蹤功能。 |
為您推薦
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","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-08-21 (世界標準時間)。"],[],[],null,["Benchmarking is a way to inspect and monitor the performance of your app. You\ncan regularly run benchmarks to analyze and debug performance problems and help\nensure that you don't introduce regressions in recent changes.\n\nAndroid offers two benchmarking libraries and approaches for analyzing and\ntesting different kinds of situations in your app: Macrobenchmark and\nMicrobenchmark.\n\nMacrobenchmark\n\nThe [Macrobenchmark](/studio/profile/macrobenchmark) library measures larger end-user interactions, such as\nstartup, interacting with the UI, and animations. The library provides direct\ncontrol over the performance environment you're testing. It lets you control\ncompiling and lets you start and stop your app to directly measure actual app\nstartup or scrolling.\n\nThe Macrobenchmark library injects events and monitors results externally from a\ntest app that is built with your tests. Therefore, when writing the benchmarks,\nyou don't call your app code directly and instead navigate within your app as a\nuser.\n\nMicrobenchmark\n\nThe [Microbenchmark](/studio/profile/benchmark) library lets you benchmark app code directly in a loop.\nThis is designed for measuring CPU work that assesses best-case performance---such\nas warmed up Just in Time (JIT) and disk accesses cached---that you might see with\nan inner-loop or a specific hot function. The library can only measure the\ncode that you can call directly in isolation.\n\nIf your app needs to process a complex data structure, or have some specific\ncomputation-heavy algorithm that is called multiple times during the app run,\nthese might be good cases for benchmarking. You can also measure parts of your\nUI. For example, you can measure the cost of the `RecyclerView` item binding,\nhow long it takes to inflate a layout, or how demanding the layout-and-measure\npass of your `View` class is from a performance perspective.\n\nHowever, you aren't able to measure how the benchmarked cases contribute to the\noverall user experience. In some scenarios, benchmarking doesn't tell you if\nyou're improving a bottleneck like jank or app startup time. For this reason,\nit's crucial to identify those bottlenecks first with the [Android Profiler](/studio/profile).\nAfter you find the code you want to investigate and optimize, the benchmarked\nloop can run repeatedly in a quick and easier fashion to create less noisy\nresults, which lets you focus on one area of improvement.\n\nThe Microbenchmark library only reports information about your app, not about\nthe system overall. Therefore, it's best at analyzing performance of situations\nspecific to the app, not ones that might relate to overall system issues.\n\nBenchmark library comparison\n\n| | Macrobenchmark | Microbenchmark |\n|-------------|-----------------------------------------------------------------------------------------------|---------------------------------------------------|\n| API version | 23 and later | 14 and later |\n| Function | Measure high-level entry points or interactions, such as activity launch or scrolling a list. | Measure individual functions. |\n| Scope | Out-of-process test of full app. | In-process test of CPU work. |\n| Speed | Medium iteration speed. It can exceed a minute. | Fast iteration speed. Often less than 10 seconds. |\n| Tracing | Results come with profiling traces. | Optional method sampling and tracing. |\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Create Baseline Profiles {:#creating-profile-rules}](/topic/performance/baselineprofiles/create-baselineprofile)\n- [JankStats Library](/topic/performance/jankstats)\n- [Overview of measuring app performance](/topic/performance/measuring-performance)"]]