自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
主机驱动的指标收集器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
主机驱动的指标收集器在主机上运行,而不是在设备端运行。这些收集器从主机端与设备进行交互,以收集它们瞄准的指标。
指标收集器设计
所有收集器将扩展的基类是 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],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."]]