androidx.benchmark.junit4


Classes

AndroidBenchmarkRunner

Instrumentation runner for benchmarks, used to increase stability of measurements and minimize interference.

BenchmarkRule

JUnit rule for benchmarking code on an Android device.

BenchmarkRule.Scope

Handle used for controlling timing during measureRepeated.

PerfettoTraceRule

Add this rule to record a Perfetto trace for each test on Android Lollipop (API 21)+ devices.

Extension functions summary

inline Unit

Benchmark a block of code.

inline Unit

Benchmark a block of code, which runs on the main thread, and can safely interact with UI.

Extension functions

measureRepeated

inline fun BenchmarkRule.measureRepeated(crossinline block: BenchmarkRule.Scope.() -> Unit): Unit

Benchmark a block of code.

@get:Rule
val benchmarkRule = BenchmarkRule();

@Test
fun myBenchmark() {
...
benchmarkRule.measureRepeated {
doSomeWork()
}
...
}
Parameters
crossinline block: BenchmarkRule.Scope.() -> Unit

The block of code to benchmark.

measureRepeatedOnMainThread

inline fun BenchmarkRule.measureRepeatedOnMainThread(crossinline block: BenchmarkRule.Scope.() -> Unit): Unit

Benchmark a block of code, which runs on the main thread, and can safely interact with UI.

While @UiThreadRule works for a standard test, it doesn't work for benchmarks of arbitrary duration, as they may run for much more than 5 seconds and suffer ANRs, especially in continuous runs.

@get:Rule
val benchmarkRule = BenchmarkRule();

@Test
fun myBenchmark() {
...
benchmarkRule.measureRepeatedOnMainThread {
doSomeWorkOnMainThread()
}
...
}
Parameters
crossinline block: BenchmarkRule.Scope.() -> Unit

The block of code to benchmark.

Throws
java.lang.Throwable

when an exception is thrown on the main thread.

kotlin.IllegalStateException

if a hard deadline is exceeded while the block is running on the main thread.