MicrobenchmarkScope


open class MicrobenchmarkScope

Known direct subclasses
BenchmarkRule.Scope

Handle used for controlling measurement during measureRepeated.


Scope handle for pausing/resuming microbenchmark measurement.

This is functionally an equivalent to BenchmarkRule.Scope which will work without the JUnit dependency.

Summary

Public functions

Unit

Pause measurement until the next call to resumeMeasurement.

Unit

Resume measurement after a call to pauseMeasurement

inline T
<T : Any?> runWithMeasurementDisabled(block: () -> T)

Disable measurement for a block of code.

Public functions

pauseMeasurement

Added in 1.4.0-alpha10
fun pauseMeasurement(): Unit

Pause measurement until the next call to resumeMeasurement.

resumeMeasurement must be called before exiting the measurement loop.

Re-entrant pausing is not supported.

Kotlin callers should generally instead use runWithMeasurementDisabled.

resumeMeasurement

Added in 1.4.0-alpha10
fun resumeMeasurement(): Unit

Resume measurement after a call to pauseMeasurement

Kotlin callers should generally instead use runWithMeasurementDisabled.

runWithMeasurementDisabled

Added in 1.4.0-alpha10
inline fun <T : Any?> runWithMeasurementDisabled(block: () -> T): T

Disable measurement for a block of code.

Used for disabling timing/measurement for work that isn't part of the benchmark:

  • When constructing per-loop randomized inputs for operations with caching,

  • Controlling which parts of multi-stage work are measured (e.g. View measure/layout)

  • Per-loop verification

import androidx.benchmark.junit4.measureRepeated

@Test
fun bitmapProcessing() =
    benchmarkRule.measureRepeated {
        val input: Bitmap = runWithMeasurementDisabled { constructTestBitmap() }
        processBitmap(input)
    }