# TraceProcessor.Session

Artifact: [androidx.benchmark:benchmark-traceprocessor](https://developer.android.com/jetpack/androidx/releases/benchmark) [View Source](https://cs.android.com/search?q=file:androidx/benchmark/traceprocessor/TraceProcessor.kt+class:androidx.benchmark.traceprocessor.TraceProcessor.Session) Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

*** ** * ** ***

Android/JVM
- android

  ```
  class TraceProcessor.Session
  ```

*** ** * ** ***

Handle to query sql data from a `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/PerfettoTrace`.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session#query(kotlin.String)` |   |

## Summary

| ### Nested types |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session.Handle : https://developer.android.com/reference/java/lang/AutoCloseable.html` |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.sequences/-sequence/index.html<https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/Row>` | `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session#query(kotlin.String)(query: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Computes the given query on the currently loaded trace. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session#queryMetricsJson(kotlin.collections.List)(metrics: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>)` Computes the given metrics, returning the results as JSON text. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html` | `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session#queryMetricsProtoBinary(kotlin.collections.List)(metrics: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>)` Computes the given metrics, returning the results as a binary proto. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session#queryMetricsProtoText(kotlin.collections.List)(metrics: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>)` Computes the given metrics, returning the result as proto text. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html` | `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session#rawQuery(kotlin.String)(query: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Computes the given query on the currently loaded trace, returning the resulting protobuf bytes as a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html`. | android |

## Public functions

### query

android Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

```
fun query(query: String): Sequence<Row>
```

Computes the given query on the currently loaded trace.

Each row returned by a query is returned by the `Sequence` as a `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/Row`. To extract data from a `Row`, query by column name. The following example does this for name, timestamp, and duration of slices:

```kotlin
// Runs the provided callback on each activityStart instance in the trace,
// providing name, start timestamp (in ns) and duration (in ns)
fun TraceProcessor.Session.forEachActivityStart(callback: (String, Long, Long) -> Unit) {
    query("SELECT name,ts,dur FROM slice WHERE name LIKE \"activityStart\"").forEach {
        callback(it.string("name"), it.long("ts"), it.long("dur")
        // or, used as a map:
        //callback(it["name"] as String, it["ts"] as Long, it["dur"] as Long)
    }
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor` |   |
| `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session` |   |

### queryMetricsJson

android Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

```
fun queryMetricsJson(metrics: List<String>): String
```

Computes the given metrics, returning the results as JSON text.

The proto format definition for these metrics can be found [here](https://cs.android.com/android/platform/superproject/main/+/main:external/perfetto/protos/perfetto/metrics/).

See [perfetto metric docs](https://perfetto.dev/docs/quickstart/trace-analysis#trace-based-metrics) for an overview on trace based metrics.

### queryMetricsProtoBinary

android Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

```
fun queryMetricsProtoBinary(metrics: List<String>): ByteArray
```

Computes the given metrics, returning the results as a binary proto.

The proto format definition for decoding this binary format can be found [here](https://cs.android.com/android/platform/superproject/main/+/main:external/perfetto/protos/perfetto/metrics/).

See [perfetto metric docs](https://perfetto.dev/docs/quickstart/trace-analysis#trace-based-metrics) for an overview on trace based metrics.

### queryMetricsProtoText

android Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

```
fun queryMetricsProtoText(metrics: List<String>): String
```

Computes the given metrics, returning the result as proto text.

The proto format definition for these metrics can be found [here](https://cs.android.com/android/platform/superproject/main/+/main:external/perfetto/protos/perfetto/metrics/).

See [perfetto metric docs](https://perfetto.dev/docs/quickstart/trace-analysis#trace-based-metrics) for an overview on trace based metrics.

### rawQuery

android Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

```
fun rawQuery(query: String): ByteArray
```

Computes the given query on the currently loaded trace, returning the resulting protobuf bytes as a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html`.

Use `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session#query(kotlin.String)` if you do not wish to parse the Proto result yourself.

The `QueryResult` protobuf definition can be found [in the Perfetto project](https://github.com/google/perfetto/blob/master/protos/perfetto/trace_processor/trace_processor.proto), which can be used to decode the result returned here with a protobuf parsing library.

Note that this method does not check for errors in the protobuf, that is the caller's responsibility.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/benchmark/traceprocessor/TraceProcessor.Session#query(kotlin.String)` |   |