BenchmarkRule

class BenchmarkRule : TestRule
kotlin.Any
   ↳ androidx.benchmark.junit4.BenchmarkRule

JUnit rule for benchmarking code on an Android device.

In Kotlin, benchmark with measureRepeated:


@get:Rule
val benchmarkRule = BenchmarkRule();

@Test
fun myBenchmark() {
    ...
    benchmarkRule.measureRepeated {
        doSomeWork()
    }
    ...
}

In Java, use getState():


@Rule
public BenchmarkRule benchmarkRule = new BenchmarkRule();

@Test
public void myBenchmark() {
    ...
    BenchmarkState state = benchmarkRule.getState();
    while (state.keepRunning()) {
        doSomeWork();
    }
    ...
}

Benchmark results will be output:

  • Summary in AndroidStudio in the test log
  • In JSON format, on the host
  • In simple form in Logcat with the tag "Benchmark"
  • To the instrumentation status result Bundle on the gradle command line

Every test in the Class using this @Rule must contain a single benchmark.

Summary

Nested classes

inner

Handle used for controlling timing during measureRepeated.

Public constructors

Public methods

Statement
apply(base: Statement, description: Description)

BenchmarkState

Object used for benchmarking in Java.

Extension functions

From androidx.benchmark.junit4
Unit

Benchmark a block of code.

Properties

BenchmarkRule.Scope

Public constructors

<init>

BenchmarkRule()

Public methods

apply

fun apply(base: Statement, description: Description): Statement

getState

fun getState(): BenchmarkState

Object used for benchmarking in Java.


@Rule
public BenchmarkRule benchmarkRule = new BenchmarkRule();

@Test
public void myBenchmark() {
    ...
    BenchmarkState state = benchmarkRule.getBenchmarkState();
    while (state.keepRunning()) {
        doSomeWork();
    }
    ...
}
Exceptions
IllegalStateException if the BenchmarkRule isn't correctly applied to a test.

Properties

scope

val scope: BenchmarkRule.Scope