Starting March 27, 2025, we recommend using android-latest-release
instead of aosp-main
to build and contribute to AOSP. For more information, see Changes to AOSP.
Host-driven metrics collectors
Stay organized with collections
Save and categorize content based on your preferences.
Host-driven metrics collectors run on the host and not on the device side. They
interact with the device from the host side to collect the metrics they are
targeting.
Metrics collector design
The base class that all collectors extend is
BaseDeviceMetricCollector
,
which helps provide the same shared basic functionalities:
- Filtering
- Disabling
- Collection on test cases versus test runs
Collectors follow a result reporter
model because they synchronize with the test execution on the host. In other
words, if tests are host-driven, collectors are executed before the test
proceeds to the next execution step.
For example, if the collector executes on testEnded
, the collectors execute
before the execution proceeds to the next test with testStart
.
Implement a host-driven metrics collector
When implementing on top of the base class BaseDeviceMetricCollector
you
can decide when to collect your metrics during the lifecycle:
- When a test run starts:
onTestRunStart
- When a test case starts:
onTestStart
- When a test case ends:
onTestEnd
- When a test run ends:
onTestRunEnd
In addition to the synchronous methods, TF provides a base class to implement
that performs periodic asynchronous collection,
ScheduledDeviceMetricCollector
,
which provides a collect
method to be implemented that runs
periodically.
The period is customizable by options.
XML configuration
The object tag is metrics_collector
, for example:
<metrics_collector class="com.android.tradefed.device.metric.AtraceCollector">
<option name="categories" value="freq"/>
</metrics_collector>
Recommendations
Look at the existing list of collectors
to ensure you aren't duplicating work. We try to ensure maximum reusability, so
having each collector performing a single type of collection allows more mixing
and matching of different collectors during test execution.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-06-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-18 UTC."],[],[],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."]]