Skip to content

Commit

Permalink
Add custom traces + TraceSectionMetric
Browse files Browse the repository at this point in the history
  • Loading branch information
mlykotom committed Nov 24, 2023
1 parent d91c308 commit b4469e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTagsAsResourceId
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.tracing.trace
import com.example.macrobenchmark.target.recyclerview.Entry
import com.example.macrobenchmark.target.util.ClickTrace

Expand Down Expand Up @@ -90,23 +91,25 @@ class ComposeActivity : ComponentActivity() {
value = value,
onValueChange = { value = it },
placeholder = { Text("Enter text here") }
)
)

LazyColumn(
modifier = Modifier
.testTag("myLazyColumn")
) {
items(data, key = { it.contents }) { item ->
EntryRow(entry = item,
Modifier
EntryRow(
entry = item,
modifier = Modifier
.padding(8.dp)
.clickable {
ClickTrace.onClickPerformed()
AlertDialog
.Builder(this@ComposeActivity)
.setMessage("Item clicked")
.show()
})
}
)
}
}
}
Expand All @@ -124,7 +127,7 @@ class ComposeActivity : ComponentActivity() {
}

@Composable
private fun EntryRow(entry: Entry, modifier: Modifier = Modifier) {
private fun EntryRow(entry: Entry, modifier: Modifier = Modifier) = trace("EntryRow") {
Card(modifier = modifier) {
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package com.example.macrobenchmark.benchmark.frames
import android.content.Intent
import android.graphics.Point
import androidx.benchmark.macro.CompilationMode
import androidx.benchmark.macro.ExperimentalMetricApi
import androidx.benchmark.macro.FrameTimingMetric
import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.TraceSectionMetric
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
Expand Down Expand Up @@ -68,12 +70,25 @@ class FrameTimingBenchmark {
}
// [END macrobenchmark_control_your_app]

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

0 comments on commit b4469e5

Please sign in to comment.