# androidx.benchmark

*** ** * ** ***

## Classes

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/benchmark/BenchmarkState` | Control object for microbenchmarking in Java. |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/ExperimentalConfig` | Experimental configuration options for a benchmark. |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/MemoryProfilingConfig` | Configuration for memory heap profiling during macrobenchmark execution. |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/MetricCapture` | Microbenchmark metric. |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/MicrobenchmarkConfig` | Experimental config object for microbenchmarks for defining custom metrics, tracing behavior, and profiling, which overrides options set in [instrumentation arguments](https://developer.android.com/topic/performance/benchmarking/microbenchmark-instrumentation-args). |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/MicrobenchmarkScope` | Scope handle for pausing/resuming microbenchmark measurement. |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/ProfilerConfig` | Profiler configuration object. |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/ProfilerConfig.MethodTracing` |   |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/ProfilerConfig.StackSampling` |   |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/StartupInsightsConfig` | Configuration for Startup Insights. |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/TimeCapture` | Time metric, which reports time in nanos, based on the time passed to `https://developer.android.com/reference/kotlin/androidx/benchmark/TimeCapture#captureStop(kotlin.Long,kotlin.LongArray,kotlin.Int)`. |

## Objects

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/benchmark/BlackHole` | Function calls which can be used to prevent optimization of results. |

## Annotations

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/benchmark/ExperimentalBenchmarkConfigApi` | Annotates declarations that are considered experimental within the Benchmark API, and are likely to change before becoming stable. |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/ExperimentalBenchmarkStateApi` | Annotation indicating experimental API primarily intended to allow microbenchmarks independent of the BenchmarkRule JUnit4 API. |

## Enums

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/benchmark/InProcessTracingMode` | Configuration for in-process tracing. |

## Extension functions summary

|---|---|
| `T` | `@https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/ExperimentalTraceProcessorApi <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Companion.https://developer.android.com/reference/kotlin/androidx/benchmark/package-summary#(androidx.benchmark.traceprocessor.TraceProcessor.Companion).runServer(kotlin.Long,kotlin.Function1)(timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, block: https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.() -> T)` Starts a Perfetto TraceProcessor shell server in http mode. |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Handle` | `@https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/ExperimentalTraceProcessorApi https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Companion.https://developer.android.com/reference/kotlin/androidx/benchmark/package-summary#(androidx.benchmark.traceprocessor.TraceProcessor.Companion).startServer(kotlin.Long)(timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Starts a Perfetto TraceProcessor shell server in http mode. |

## Extension functions

### TraceProcessor.Companion.runServer

Artifact: [androidx.benchmark:benchmark-common](https://developer.android.com/jetpack/androidx/releases/benchmark) [View Source](https://cs.android.com/search?q=file:androidx/benchmark/TraceProcessorExtensions.kt+function:runServer) Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.5.0)

```
@ExperimentalTraceProcessorApi
fun <T : Any?> TraceProcessor.Companion.runServer(
    timeoutMs: Long = DEFAULT_TIMEOUT_MS,
    block: TraceProcessor.() -> T
): T
```

Starts a Perfetto TraceProcessor shell server in http mode.

The server is stopped after the block is complete.

```kotlin
import androidx.benchmark.runServer
import androidx.benchmark.traceprocessor.PerfettoTrace
import androidx.benchmark.traceprocessor.TraceProcessor

// Collect the duration of all slices named "activityStart" in the trace
val activityStartDurNs = TraceProcessor.runServer {
    loadTrace(PerfettoTrace("/path/to/trace.perfetto-trace")) {
            query("SELECT dur FROM slice WHERE name = 'activityStart'").map {
                it.long("dur")
            }
        }
        .toList()
}
return activityStartDurNs
```

| Parameters |
|---|---|
| `timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html = DEFAULT_TIMEOUT_MS` | maximum duration in milliseconds for waiting for operations like loading the server, or querying a trace. |
| `block: https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.() -> T` | Command to execute using trace processor |

### TraceProcessor.Companion.startServer

Artifact: [androidx.benchmark:benchmark-common](https://developer.android.com/jetpack/androidx/releases/benchmark) [View Source](https://cs.android.com/search?q=file:androidx/benchmark/TraceProcessorExtensions.kt+function:startServer) Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.5.0)

```
@ExperimentalTraceProcessorApi
fun TraceProcessor.Companion.startServer(
    timeoutMs: Long = DEFAULT_TIMEOUT_MS
): TraceProcessor.Handle
```

Starts a Perfetto TraceProcessor shell server in http mode.

```kotlin
import androidx.benchmark.startServer
import androidx.benchmark.traceprocessor.PerfettoTrace
import androidx.benchmark.traceprocessor.TraceProcessor

// Collect the duration of all slices named "activityStart" in the trace
val activityStartDurNs =
    TraceProcessor.startServer().use {
        it.traceProcessor.startSession(PerfettoTrace("/path/to/trace.perfetto-trace")).use {
            it.session
                .query("SELECT dur FROM slice WHERE name = 'activityStart'")
                .map { it.long("dur") }
                .toList()
        }
    }
return activityStartDurNs
```

| Parameters |
|---|---|
| `timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html = DEFAULT_TIMEOUT_MS` | maximum duration in milliseconds for waiting for operations like loading the server, or querying a trace. |