BenchmarkRuleKt

Added in 1.0.0

public final class BenchmarkRuleKt


Summary

Public methods

static final void

Benchmark a block of code.

static final void

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

Public methods

measureRepeated

public static final void measureRepeated(
    @NonNull BenchmarkRule receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull BenchmarkRule.ScopeUnit> block
)

Benchmark a block of code.

@get:Rule
val
benchmarkRule = BenchmarkRule();

@Test
fun
myBenchmark() {
...
benchmarkRule.measureRepeated {
doSomeWork()
}
...
}
Parameters
@ExtensionFunctionType @NonNull Function1<@NonNull BenchmarkRule.ScopeUnit> block

The block of code to benchmark.

measureRepeatedOnMainThread

public static final void measureRepeatedOnMainThread(
    @NonNull BenchmarkRule receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull BenchmarkRule.ScopeUnit> block
)

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
@ExtensionFunctionType @NonNull Function1<@NonNull BenchmarkRule.ScopeUnit> block

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.