TraceMetric

@ExperimentalMetricApi
public abstract class TraceMetric extends Metric

Known direct subclasses
MemoryCountersMetric

Captures the number of page faults over time for a target package name.

MemoryUsageMetric

Metric for tracking the memory usage of the target application.


Metric which captures results from a Perfetto trace with custom PerfettoTraceProcessor queries.

This is a more customizable version of TraceSectionMetric which can perform arbitrary queries against the captured PerfettoTrace.

Sample metric which finds the duration of the first "activityResume" trace section for the traced package:

class ActivityResumeMetric : TraceMetric() {
override fun getResult(
captureInfo: CaptureInfo,
traceSession: PerfettoTraceProcessor.Session
): Result {
val rowSequence = traceSession.query(
"""
SELECT
slice.name as name,
slice.ts as ts,
slice.dur as dur
FROM slice
INNER JOIN thread_track on slice.track_id = thread_track.id
INNER JOIN thread USING(utid)
INNER JOIN process USING(upid)
WHERE
process.name LIKE ${captureInfo.targetPackageName}
AND slice.name LIKE "activityResume"
""".trimIndent()
)
// this metric queries a single slice type to produce submetrics, but could be extended
// to capture timing of every component of activity lifecycle
val activityResultNs = rowSequence.firstOrNull()?.double("dur")
return if (activityResultMs != null) {
Result("activityResumeMs", activityResultNs / 1_000_000.0)
} else {
Result()
}
}
}

Summary

Public constructors

Public methods

abstract @NonNull List<@NonNull Metric.Measurement>
getResult(
    @NonNull Metric.CaptureInfo captureInfo,
    @NonNull PerfettoTraceProcessor.Session traceSession
)

Get the metric result for a given iteration given information about the target process and a TraceProcessor session

Public constructors

TraceMetric

Added in 1.2.0
public TraceMetric()

Public methods

getResult

Added in 1.2.0
public abstract @NonNull List<@NonNull Metric.MeasurementgetResult(
    @NonNull Metric.CaptureInfo captureInfo,
    @NonNull PerfettoTraceProcessor.Session traceSession
)

Get the metric result for a given iteration given information about the target process and a TraceProcessor session