# 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)

*** ** * ** ***


```
public final class TraceProcessor.Session
```

<br />

*** ** * ** ***

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

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

## Summary

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

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.sequences/-sequence/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/Row>` | `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session#query(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html query)` Computes the given query on the currently loaded trace. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session#queryMetricsJson(kotlin.collections.List)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html> metrics)` Computes the given metrics, returning the results as JSON text. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull byte[]` | `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session#queryMetricsProtoBinary(kotlin.collections.List)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html> metrics)` Computes the given metrics, returning the results as a binary proto. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session#queryMetricsProtoText(kotlin.collections.List)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html> metrics)` Computes the given metrics, returning the result as proto text. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull byte[]` | `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session#rawQuery(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html query)` 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`. |

## Public methods

### query

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

```
public final @NonNull Sequence<@NonNull Row> query(@NonNull String query)
```

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/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:

```
// 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/androidx/benchmark/traceprocessor/TraceProcessor` |   |
| `https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session` |   |

### queryMetricsJson

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

```
public final @NonNull String queryMetricsJson(@NonNull List<@NonNull String> metrics)
```

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

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

```
public final @NonNull byte[] queryMetricsProtoBinary(@NonNull List<@NonNull String> metrics)
```

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

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

```
public final @NonNull String queryMetricsProtoText(@NonNull List<@NonNull String> metrics)
```

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

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

```
public final @NonNull byte[] rawQuery(@NonNull String query)
```

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/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/androidx/benchmark/traceprocessor/TraceProcessor.Session#query(kotlin.String)` |   |