自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
主機導向指標收集器
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
主機導向指標收集器會在主機上執行,而非在裝置端執行。這些指標會從主機端與裝置互動,收集指定的指標。
指標收集器設計
所有收集器擴充的基礎類別是 BaseDeviceMetricCollector
,可提供相同的共用基本功能:
收集器會遵循結果回報器模式,因為它會與主機上的測試執行作業同步。換句話說,如果測試由主機驅動,收集器會在測試進入下一個執行步驟前執行。
舉例來說,如果收集器在 testEnded
上執行,收集器會在執行 testStart
進行下一個測試之前執行。
實作主機導向指標收集器
在基礎類別 BaseDeviceMetricCollector
上導入時,您可以決定在生命週期中收集指標的時間點:
- 測試開始時:
onTestRunStart
- 測試案例開始時:
onTestStart
- 測試案例結束時:
onTestEnd
- 測試結束後:
onTestRunEnd
除了同步方法之外,TF 也提供基礎類別,可實作定期非同步收集作業,也就是 ScheduledDeviceMetricCollector
,這個類別會提供要實作的 collect
方法,以便定期執行。
您可以透過選項自訂週期。
XML 設定
物件代碼為 metrics_collector
,例如:
<metrics_collector class="com.android.tradefed.device.metric.AtraceCollector">
<option name="categories" value="freq"/>
</metrics_collector>
建議
請查看現有的收集器清單,確認您不會重複工作。我們會盡量確保可重複使用性,因此讓每個收集器執行單一類型的收集作業,可在測試執行期間更頻繁地混合及比對不同的收集器。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Host-driven metrics collectors run on the host and not on the device side. They\ninteract with the device from the host side to collect the metrics they are\ntargeting.\n\nMetrics collector design\n------------------------\n\nThe base class that all collectors extend is\n[`BaseDeviceMetricCollector`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/device/metric/BaseDeviceMetricCollector.java),\nwhich helps provide the same shared basic functionalities:\n\n- Filtering\n- Disabling\n- Collection on test cases versus test runs\n\nCollectors follow a [result reporter](/docs/core/tests/tradefed/architecture/result-reporter)\nmodel because they synchronize with the test execution on the host. In other\nwords, if tests are host-driven, collectors are executed before the test\nproceeds to the next execution step.\n\nFor example, if the collector executes on `testEnded`, the collectors execute\nbefore the execution proceeds to the next test with `testStart`.\n\nImplement a host-driven metrics collector\n-----------------------------------------\n\nWhen implementing on top of the base class `BaseDeviceMetricCollector` you\ncan decide when to collect your metrics during the lifecycle:\n\n- When a test run starts: `onTestRunStart`\n- When a test case starts: `onTestStart`\n- When a test case ends: `onTestEnd`\n- When a test run ends: `onTestRunEnd`\n\nPerform asynchronous collection\n-------------------------------\n\nIn addition to the synchronous methods, TF provides a base class to implement\nthat performs periodic asynchronous collection,\n[`ScheduledDeviceMetricCollector`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/test_framework/com/android/tradefed/device/metric/ScheduledDeviceMetricCollector.java),\nwhich provides a `collect` method to be implemented that runs\nperiodically.\n\nThe period is customizable by options.\n\nXML configuration\n-----------------\n\nThe object tag is `metrics_collector`, for example: \n\n \u003cmetrics_collector class=\"com.android.tradefed.device.metric.AtraceCollector\"\u003e\n \u003coption name=\"categories\" value=\"freq\"/\u003e\n \u003c/metrics_collector\u003e\n\nRecommendations\n---------------\n\nLook at the [existing list of collectors](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/test_framework/com/android/tradefed/device/metric)\nto ensure you aren't duplicating work. We try to ensure maximum reusability, so\nhaving each collector performing a single type of collection allows more mixing\nand matching of different collectors during test execution."]]