BenchmarkState

public final class BenchmarkState


Control object for benchmarking in the code in Java.

Query a state object with androidx.benchmark.junit4.BenchmarkRule.getState, and use it to measure a block of Java with BenchmarkState.keepRunning:

@Rule
public BenchmarkRule benchmarkRule = new BenchmarkRule();

@Test
public void sampleMethod() {
BenchmarkState state = benchmarkRule.getState();

int[] src = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
while (state.keepRunning()) {
int[] dest = new int[src.length];
System.arraycopy(src, 0, dest, 0, src.length);
}
}
See also
BenchmarkRule

#getState()

Summary

Public methods

final boolean

Returns true if the benchmark needs more samples - use this as the condition of a while loop.

final void

Stops the benchmark timer.

final void

Resumes the benchmark timer.

Public methods

keepRunning

public final boolean keepRunning()

Returns true if the benchmark needs more samples - use this as the condition of a while loop.

while (state.keepRunning()) {
int[] dest = new int[src.length];
System.arraycopy(src, 0, dest, 0, src.length);
}

pauseTiming

public final void pauseTiming()

Stops the benchmark timer.

This method can be called only when the timer is running.

@Test
public void bitmapProcessing() {
final BenchmarkState state = mBenchmarkRule.getState();
while (state.keepRunning()) {
state.pauseTiming();
// disable timing while constructing test input
Bitmap input = constructTestBitmap();
state.resumeTiming();

processBitmap(input);
}
}
Throws
kotlin.IllegalStateException

if the benchmark is already paused.

See also
resumeTiming

resumeTiming

public final void resumeTiming()

Resumes the benchmark timer.

This method can be called only when the timer is stopped.

@Test
public void bitmapProcessing() {
final BenchmarkState state = mBenchmarkRule.getState();
while (state.keepRunning()) {
state.pauseTiming();
// disable timing while constructing test input
Bitmap input = constructTestBitmap();
state.resumeTiming();

processBitmap(input);
}
}

@throws [IllegalStateException] if the benchmark is already running.
See also
pauseTiming