MacrobenchmarkRule

public final class MacrobenchmarkRule


JUnit rule for benchmarking large app operations like startup.

Summary

Public constructors

Public methods

@NonNull Statement
apply(Statement base, Description description)
final void
measureRepeated(
    String packageName,
    List<Metric> metrics,
    CompilationMode compilationMode,
    StartupMode startupMode,
    @IntRange(from = 1) int iterations,
    @ExtensionFunctionType Function1<MacrobenchmarkScopeUnit> setupBlock,
    @ExtensionFunctionType Function1<MacrobenchmarkScopeUnit> measureBlock
)

Measure behavior of the specified packageName given a set of metrics.

Public constructors

MacrobenchmarkRule

public final MacrobenchmarkRule()

Public methods

apply

public @NonNull Statement apply(Statement base, Description description)

measureRepeated

public final void measureRepeated(
    String packageName,
    List<Metric> metrics,
    CompilationMode compilationMode,
    StartupMode startupMode,
    @IntRange(from = 1) int iterations,
    @ExtensionFunctionType Function1<MacrobenchmarkScopeUnit> setupBlock,
    @ExtensionFunctionType Function1<MacrobenchmarkScopeUnit> measureBlock
)

Measure behavior of the specified packageName given a set of metrics.

This performs a macrobenchmark with the below control flow:

    resetAppCompilation()
compile(compilationMode)
repeat(iterations) {
setupBlock()
captureTrace {
measureBlock()
}
}
Parameters
String packageName

Package name of the app being measured.

List<Metric> metrics

List of metrics to measure.

CompilationMode compilationMode

Mode of compilation used before capturing measurement, such as CompilationMode.Partial, defaults to CompilationMode.DEFAULT.

StartupMode startupMode

Optional mode to force app launches performed with MacrobenchmarkScope.startActivityAndWait (and similar variants) to be of the assigned type. For example, COLD launches kill the process before the measureBlock, to ensure startups will go through full process creation. Generally, leave as null for non-startup benchmarks.

@IntRange(from = 1) int iterations

Number of times the measureBlock will be run during measurement. Note that total iteration count may not match, due to warmup iterations needed for the compilationMode.

@ExtensionFunctionType Function1<MacrobenchmarkScopeUnit> setupBlock

The block performing app actions each iteration, prior to the measureBlock. For example, navigating to a UI where scrolling will be measured.

@ExtensionFunctionType Function1<MacrobenchmarkScopeUnit> measureBlock

The block performing app actions to benchmark each iteration.