MetricCapture
@ExperimentalBenchmarkConfigApi
abstract class MetricCapture
TimeCapture |
Time metric, which reports time in nanos, based on the time passed to |
Microbenchmark metric.
Note that the API is designed around low overhead, even in the case of multiple submetrics (such as cpu perf event counters) that must be started/stopped together for efficiency.
This class may be initialized on a different thread from where measurement occurs, but all capture methods must be invoked from the same thread.
import androidx.benchmark.MetricCapture /** * Sample shows how to collect multiple continuously running counters as metrics which measure * difference. * * This is similar to how [androidx.benchmark.TimeCapture] produces `timeNs`. */ class MyMetricCapture : MetricCapture(listOf("foo", "bar")) { private var currentStartedFoo = 0L private var currentPausedStartedFoo = 0L private var currentTotalPausedFoo = 0L private var currentStartedBar = 0L private var currentPausedStartedBar = 0L private var currentTotalPausedBar = 0L override fun captureStart(timeNs: Long) { // reset paused state, capture current currentTotalPausedBar = 0 currentTotalPausedFoo = 0 currentStartedFoo = getCurrentFoo() currentStartedBar = getCurrentBar() } override fun captureStop(timeNs: Long, output: LongArray, offset: Int) { output[offset + 0] = getCurrentFoo() - currentStartedFoo - currentTotalPausedFoo output[offset + 1] = getCurrentBar() - currentStartedBar - currentTotalPausedBar } override fun capturePaused() { currentPausedStartedFoo = getCurrentFoo() currentPausedStartedBar = getCurrentBar() } override fun captureResumed() { currentTotalPausedFoo += getCurrentFoo() - currentPausedStartedFoo currentTotalPausedBar += getCurrentBar() - currentPausedStartedBar } }
Summary
Public constructors |
|---|
MetricCapture(names: List<String>) |
Public functions |
|
|---|---|
abstract Unit |
Pause data collection. |
abstract Unit |
Resume data collection |
abstract Unit |
captureStart(timeNs: Long)Starts collecting data for a run. |
abstract Unit |
captureStop(timeNs: Long, output: LongArray, offset: Int)Mark the end of a run, and store offset metrics in the output array, per sub metric. |
open operator Boolean |
|
open Int |
hashCode() |
Public constructors
Public functions
captureStart
abstract fun captureStart(timeNs: Long): Unit
Starts collecting data for a run.
Called at the start of each run.
| Parameters | |
|---|---|
timeNs: Long |
Current time, just before starting metrics. Can be used directly to drive a timing metric produced. |
captureStop
abstract fun captureStop(timeNs: Long, output: LongArray, offset: Int): Unit
Mark the end of a run, and store offset metrics in the output array, per sub metric.
To output values, store them in the output array offset by both the parameter offset, and their submetric index.
| Parameters | |
|---|---|
timeNs: Long |
Time of metric capture start, in monotonic time ( |
output: LongArray |
LongArray sized to hold all simultaneous sub metric outputs, use |
offset: Int |
Offset into the output array to start writing sub metrics. |
Public properties
names
val names: List<String>
List of names of metrics produced by this MetricCapture.
The length of this list defines how many metrics will be produced by captureStart and captureStop.