Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support async spans in local runner #67

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import androidx.benchmark.perfetto.Row
/**
* While these are available in PerfettoTraceProcessor, they're marked as internal to the library
* group, preventing us from leveraging them.
* These are simply a copy of those at 1.2.0-beta05, very slightly modified to avoid naming confusion:
* These are simply a copy of those at 1.2.0-beta05, slightly modified to avoid naming confusion and
* for a few custom helpers. Original source:
* https://github.com/androidx/androidx/blob/f6df7df4bb215d31187a32dea874edd43eb9506f/benchmark/benchmark-macro/src/main/java/androidx/benchmark/perfetto/PerfettoTraceProcessor.kt#L241
*/
@OptIn(ExperimentalPerfettoTraceProcessorApi::class)
fun PerfettoTraceProcessor.Session.querySpans(
spanNames: List<String>,
packageName: String?,
): List<Slice> {
val whereClause = spanNames
val spansWhereClause = spanNames
.joinToString(
separator = " OR ",
prefix = if (packageName == null) {
Expand All @@ -27,7 +28,8 @@ fun PerfettoTraceProcessor.Session.querySpans(
) {
"slice.name LIKE \"$it\""
}
val innerJoins = if (packageName != null) {

val syncSpanInnerJoins = if (packageName != null) {
"""
INNER JOIN thread_track on slice.track_id = thread_track.id
INNER JOIN thread USING(utid)
Expand All @@ -37,13 +39,30 @@ fun PerfettoTraceProcessor.Session.querySpans(
""
}

// Async spans are written to a different "track" (process_track)
// than standard Trace spans (thread_track).
val asyncSpanInnerJoins = if (packageName != null) {
"""
INNER JOIN process_track on slice.track_id = process_track.id
INNER JOIN process USING(upid)
""".trimMargin()
} else {
""
}

return query(
query = """
SELECT slice.name,ts,dur
FROM slice
$innerJoins
WHERE $whereClause
ORDER BY ts
$syncSpanInnerJoins
WHERE $spansWhereClause

UNION

SELECT slice.name,ts,dur
FROM slice
$asyncSpanInnerJoins
WHERE $spansWhereClause
""".trimMargin()
).toSlices()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.emergetools.performance.sample

import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.os.Trace
import androidx.activity.ComponentActivity
Expand All @@ -11,6 +13,9 @@ class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
Trace.beginSection("MainActivity.onCreate")
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
Trace.beginAsyncSection("MainActivity.onCreateAsync", 1)
}
super.onCreate(savedInstanceState)

setContent {
Expand All @@ -23,5 +28,8 @@ class MainActivity : ComponentActivity() {
}

Trace.endSection()
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
Trace.endAsyncSection("MainActivity.onCreateAsync", 1)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ExamplePerformanceTest {
* app during the test. Each span will have a separate conclusion & flamegraph comparison
* available in the Emerge UI.
*/
@EmergeTest(spans = ["MainActivity.onCreate"])
@EmergeTest(spans = ["MainActivity.onCreate", "MainActivity.onCreateAsync"])
fun myDeeplinkStartupTest() {
Relax(APP_PACKAGE_NAME) {
launchWithLink("emg://emergetools.com/")
Expand Down