Skip to content

Commit b4469e5

Browse files
committed
Add custom traces + TraceSectionMetric
1 parent d91c308 commit b4469e5

File tree

2 files changed

+24
-6
lines changed
  • MacrobenchmarkSample

2 files changed

+24
-6
lines changed

MacrobenchmarkSample/app/src/main/java/com/example/macrobenchmark/target/activity/clicklatency/ComposeActivity.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import androidx.compose.ui.semantics.semantics
4848
import androidx.compose.ui.semantics.testTagsAsResourceId
4949
import androidx.compose.ui.tooling.preview.Preview
5050
import androidx.compose.ui.unit.dp
51+
import androidx.tracing.trace
5152
import com.example.macrobenchmark.target.recyclerview.Entry
5253
import com.example.macrobenchmark.target.util.ClickTrace
5354

@@ -90,23 +91,25 @@ class ComposeActivity : ComponentActivity() {
9091
value = value,
9192
onValueChange = { value = it },
9293
placeholder = { Text("Enter text here") }
93-
)
94+
)
9495

9596
LazyColumn(
9697
modifier = Modifier
9798
.testTag("myLazyColumn")
9899
) {
99100
items(data, key = { it.contents }) { item ->
100-
EntryRow(entry = item,
101-
Modifier
101+
EntryRow(
102+
entry = item,
103+
modifier = Modifier
102104
.padding(8.dp)
103105
.clickable {
104106
ClickTrace.onClickPerformed()
105107
AlertDialog
106108
.Builder(this@ComposeActivity)
107109
.setMessage("Item clicked")
108110
.show()
109-
})
111+
}
112+
)
110113
}
111114
}
112115
}
@@ -124,7 +127,7 @@ class ComposeActivity : ComponentActivity() {
124127
}
125128

126129
@Composable
127-
private fun EntryRow(entry: Entry, modifier: Modifier = Modifier) {
130+
private fun EntryRow(entry: Entry, modifier: Modifier = Modifier) = trace("EntryRow") {
128131
Card(modifier = modifier) {
129132
Row(verticalAlignment = Alignment.CenterVertically) {
130133
Text(

MacrobenchmarkSample/macrobenchmark/src/main/java/com/example/macrobenchmark/benchmark/frames/FrameTimingBenchmark.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package com.example.macrobenchmark.benchmark.frames
1919
import android.content.Intent
2020
import android.graphics.Point
2121
import androidx.benchmark.macro.CompilationMode
22+
import androidx.benchmark.macro.ExperimentalMetricApi
2223
import androidx.benchmark.macro.FrameTimingMetric
2324
import androidx.benchmark.macro.StartupMode
25+
import androidx.benchmark.macro.TraceSectionMetric
2426
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
2527
import androidx.test.ext.junit.runners.AndroidJUnit4
2628
import androidx.test.filters.LargeTest
@@ -68,12 +70,25 @@ class FrameTimingBenchmark {
6870
}
6971
// [END macrobenchmark_control_your_app]
7072

73+
@OptIn(ExperimentalMetricApi::class)
7174
@Test
7275
fun scrollComposeList() {
7376
benchmarkRule.measureRepeated(
7477
// [START_EXCLUDE]
7578
packageName = TARGET_PACKAGE,
76-
metrics = listOf(FrameTimingMetric()),
79+
metrics = listOf(
80+
FrameTimingMetric(),
81+
// Measure custom trace sections by name EntryRow (which is added to the EntryRow composable).
82+
// Mode.Sum measure combined duration and also how many times it occurred in the trace.
83+
// This way, you can estimate whether a composable recomposes more than it should.
84+
TraceSectionMetric("EntryRow", TraceSectionMetric.Mode.Sum),
85+
// This trace section takes into account the SQL wildcard character %,
86+
// which can find trace sections without knowing the full name.
87+
// This way, you can measure composables produced by the composition tracing
88+
// and measure how long they took and how many times they recomposed.
89+
// WARNING: This metric only shows results when running with composition tracing, otherwise it won't be visible in the outputs.
90+
TraceSectionMetric("%.EntryRow%", TraceSectionMetric.Mode.Sum),
91+
),
7792
// Try switching to different compilation modes to see the effect
7893
// it has on frame timing metrics.
7994
compilationMode = CompilationMode.None(),

0 commit comments

Comments
 (0)