# BenchmarkRuleKt

Added in 1.0.0

*** ** * ** ***


```
public final class BenchmarkRuleKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final void` | `https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRuleKt#(androidx.benchmark.junit4.BenchmarkRule).measureRepeated(kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRule receiver, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRule.Scope, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` Benchmark a block of code. |
| `static final void` | `https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRuleKt#(androidx.benchmark.junit4.BenchmarkRule).measureRepeatedOnMainThread(kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRule receiver, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRule.Scope, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` Benchmark a block of code, which runs on the main thread, and can safely interact with UI. |

## Public methods

### measureRepeated

Artifact: [androidx.benchmark:benchmark-junit4](https://developer.android.com/jetpack/androidx/releases/benchmark) [View Source](https://cs.android.com/search?q=file:androidx/benchmark/junit4/BenchmarkRule.kt+function:measureRepeated) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.0.0)

```
public static final void measureRepeated(
    @NonNull BenchmarkRule receiver,
    @NonNull Function1<@NonNull BenchmarkRule.Scope, Unit> block
)
```

Benchmark a block of code.

```kotlin
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4

@RunWith(AndroidJUnit4::class)
class MyBenchmark {
    @get:Rule val benchmarkRule = BenchmarkRule()

    @Test
    fun measureWork() {
        benchmarkRule.measureRepeated { doSomeWork() }
    }
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRule.Scope, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block` | The block of code to benchmark. |

### measureRepeatedOnMainThread

Artifact: [androidx.benchmark:benchmark-junit4](https://developer.android.com/jetpack/androidx/releases/benchmark) [View Source](https://cs.android.com/search?q=file:androidx/benchmark/junit4/BenchmarkRule.kt+function:measureRepeatedOnMainThread) Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.3.0)

```
public static final void measureRepeatedOnMainThread(
    @NonNull BenchmarkRule receiver,
    @NonNull Function1<@NonNull BenchmarkRule.Scope, Unit> 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.

```kotlin
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.benchmark.junit4.measureRepeatedOnMainThread
import androidx.test.ext.junit.runners.AndroidJUnit4

@RunWith(AndroidJUnit4::class)
class MainThreadBenchmark {
    @get:Rule val benchmarkRule = BenchmarkRule()

    @Test
    fun measureWork() {
        benchmarkRule.measureRepeatedOnMainThread {
            // this block is run on the main thread
            doSomeWorkOnMainThread()
        }
    }
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRule.Scope, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block` | The block of code to benchmark. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/Throwable.html` | when an exception is thrown on the main thread. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if a hard deadline is exceeded while the block is running on the main thread. |