Skip to content

Commit a6598e0

Browse files
committed
feat(nav3): Use the SerialName mapper in the Android samples
The routes are @serializable with the Nav2 sample's route names as their serial names. The single-stack sample names them with BackStackEntryMapper.SerialName and adds each route's own arguments, and the multi-root samples use SerialName directly. The performance scenario can run SerialName itself, to measure its name lookups, as well as the custom mapper.
1 parent c8bea42 commit a6598e0

6 files changed

Lines changed: 225 additions & 70 deletions

File tree

‎sentry-android-integration-tests/build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ plugins {
22
alias(libs.plugins.android.application) apply false
33
alias(libs.plugins.kotlin.android) apply false
44
alias(libs.plugins.kotlin.compose) apply false
5+
alias(libs.plugins.kotlin.serialization) apply false
56
alias(libs.plugins.errorprone) apply false
67
alias(libs.plugins.detekt) apply false
78
alias(libs.plugins.gradle.versions) apply false

‎sentry-android-integration-tests/sentry-samples-android/build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ plugins {
1313
id("io.sentry.jdk-target")
1414
alias(libs.plugins.kotlin.android)
1515
alias(libs.plugins.kotlin.compose)
16+
alias(libs.plugins.kotlin.serialization)
1617
alias(libs.plugins.ksp)
1718
alias(libs.plugins.sentry) apply false
1819
alias(libs.plugins.sqldelight)

‎sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Activity.kt‎

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import androidx.compose.ui.semantics.semantics
4343
import androidx.compose.ui.semantics.testTagsAsResourceId
4444
import androidx.navigation3.runtime.entryProvider
4545
import androidx.navigation3.ui.NavDisplay
46+
import io.sentry.compose.navigation3.BackStackEntryMapper
4647
import io.sentry.compose.navigation3.SentryBackStackEntry
4748
import io.sentry.compose.navigation3.SentryNavEffect
4849
import io.sentry.compose.navigation3.SentryNavOptions
@@ -232,51 +233,67 @@ private fun Nav3SampleApp(
232233
}
233234
val performanceExtractorMode = performanceState.extractorMode
234235
val performanceArgumentMode = performanceState.argumentMode
235-
val backStackEntryMapper =
236+
val performanceMapperMode = performanceState.mapperMode
237+
val backStackEntryMapper: BackStackEntryMapper<Nav3Route> =
236238
remember(
237239
isPerformanceScenario,
238240
performanceExtractorMode,
239241
performanceArgumentMode,
242+
performanceMapperMode,
240243
integrationMode.includeArguments,
241244
) {
242-
{ route: Nav3Route ->
243-
val name =
244-
if (isPerformanceScenario) {
245+
if (!isPerformanceScenario) {
246+
// Names each route after its serial name, and adds the arguments the route selects.
247+
BackStackEntryMapper { route ->
248+
BackStackEntryMapper.SerialName.map(route)?.let { entry ->
249+
SentryBackStackEntry(name = entry.name, arguments = route.arguments)
250+
}
251+
}
252+
} else if (performanceMapperMode == Nav3PerformanceMapperMode.SERIAL_NAME) {
253+
// Times each call to the built-in mapper, which records no arguments. Its result is
254+
// returned as is, so a null keeps the reason the mapper reported.
255+
BackStackEntryMapper { route ->
256+
performanceState.recordNameExtraction("Nav3Stress.serialNameMapping") {
257+
BackStackEntryMapper.SerialName.map(route)
258+
}
259+
}
260+
} else {
261+
// A custom mapper, so the performance scenario can time name and argument extraction
262+
// separately and substitute synthetic arguments. It names routes without SerialName, so
263+
// that its timings are a baseline for SerialName's.
264+
BackStackEntryMapper { route ->
265+
val name =
245266
performanceState.recordNameExtraction("Nav3Stress.nameExtraction") {
246267
consumeNav3PerformanceExtractorWork(
247268
performanceExtractorMode,
248269
route.performanceSeed,
249270
)
250271
route.routeName
251272
}
252-
} else {
253-
route.routeName
254-
}
255273

256-
val arguments =
257-
if (isPerformanceScenario && !integrationMode.includeArguments) {
258-
null
259-
} else if (isPerformanceScenario) {
260-
performanceState.recordArgumentExtraction("Nav3Stress.argumentsExtraction") {
261-
consumeNav3PerformanceExtractorWork(
262-
performanceExtractorMode,
263-
route.performanceSeed,
264-
)
265-
if (route is Nav3Route.Performance) {
266-
nav3PerformanceArguments(
267-
performanceArgumentMode,
268-
route.index,
269-
route.generation,
274+
val arguments =
275+
if (!integrationMode.includeArguments) {
276+
null
277+
} else {
278+
performanceState.recordArgumentExtraction("Nav3Stress.argumentsExtraction") {
279+
consumeNav3PerformanceExtractorWork(
280+
performanceExtractorMode,
281+
route.performanceSeed,
270282
)
271-
} else {
272-
route.arguments
283+
if (route is Nav3Route.Performance) {
284+
nav3PerformanceArguments(
285+
performanceArgumentMode,
286+
route.index,
287+
route.generation,
288+
)
289+
} else {
290+
route.arguments
291+
}
273292
}
274293
}
275-
} else {
276-
route.arguments
277-
}
278294

279-
SentryBackStackEntry(name = name, arguments = arguments)
295+
SentryBackStackEntry(name = name, arguments = arguments)
296+
}
280297
}
281298
}
282299

‎sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3MultiActivity.kt‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import androidx.navigation3.ui.NavDisplay
5454
import androidx.navigationevent.compose.LocalNavigationEventDispatcherOwner
5555
import androidx.navigationevent.compose.rememberNavigationEventDispatcherOwner
5656
import io.sentry.compose.SentryModifier.sentryTag
57-
import io.sentry.compose.navigation3.SentryBackStackEntry
57+
import io.sentry.compose.navigation3.BackStackEntryMapper
5858
import io.sentry.compose.navigation3.SentryNavEffect
5959
import io.sentry.compose.navigation3.SentryNavOptions
6060
import io.sentry.samples.android.navigation.common.NavigationSampleConfig
@@ -307,9 +307,7 @@ internal fun MultiRootNavDisplay(
307307
// Keep this before the NavDisplay below: TracedNav3Route's resume tagging relies on it.
308308
SentryNavEffect(
309309
backStack = backStack,
310-
backStackEntryMapper = { route ->
311-
SentryBackStackEntry(name = route.routeName, arguments = route.arguments)
312-
},
310+
backStackEntryMapper = BackStackEntryMapper.SerialName,
313311
options = options,
314312
)
315313
val onBack: () -> Unit = {

‎sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3PerformanceControls.kt‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ internal class Nav3PerformanceState(
5252
var autoNavigate by mutableStateOf(false)
5353
var extractorMode by mutableStateOf(Nav3PerformanceExtractorMode.NORMAL)
5454
var argumentMode by mutableStateOf(Nav3PerformanceArgumentMode.FLAT)
55+
var mapperMode by mutableStateOf(Nav3PerformanceMapperMode.CUSTOM)
5556
var integrationMode by mutableStateOf(Nav3PerformanceIntegrationMode.FULL_STACK)
5657
private set
5758

@@ -321,6 +322,7 @@ internal class Nav3PerformanceState(
321322
appendLine("status=$performanceStatus")
322323
appendLine("route=$currentRoute")
323324
appendLine("tracked_stack=$backStack")
325+
appendLine("mapper_mode=${mapperMode.label}")
324326
appendLine("recomposition_requests=$recompositionRequests")
325327
appendLine("navigation_mutations=$navigationMutations")
326328
appendLine("destination_changes=$destinationChanges")
@@ -408,7 +410,7 @@ internal class Nav3PerformanceState(
408410
pendingOperation = null
409411
}
410412

411-
fun recordNameExtraction(sectionName: String, block: () -> String): String {
413+
fun <T> recordNameExtraction(sectionName: String, block: () -> T): T {
412414
val startedAt = System.nanoTime()
413415
Trace.beginSection(sectionName)
414416
try {
@@ -512,6 +514,17 @@ internal enum class Nav3PerformanceArgumentMode(val label: String) {
512514
LARGE("Large"),
513515
}
514516

517+
/**
518+
* Selects the performance scenario's mapper.
519+
*
520+
* [CUSTOM] provides synthetic arguments and separate extractor timings. [SERIAL_NAME] uses the
521+
* built-in mapper, which extracts names only; each of its calls is timed as name extraction.
522+
*/
523+
internal enum class Nav3PerformanceMapperMode(val label: String) {
524+
CUSTOM("Custom"),
525+
SERIAL_NAME("SerialName"),
526+
}
527+
515528
internal enum class Nav3PerformanceIntegrationMode(
516529
val label: String,
517530
val captureBackStack: Boolean,
@@ -874,19 +887,33 @@ internal fun Nav3PerformancePanel(
874887

875888
if (showExtractorControls) {
876889
PerfCard(title = "Extractor Inputs") {
890+
val customExtractorControlsEnabled =
891+
!state.performanceRunActive && state.mapperMode == Nav3PerformanceMapperMode.CUSTOM
892+
893+
PerfModeRow(
894+
label = "Mapper",
895+
selectedLabel = state.mapperMode.label,
896+
enabled = !state.performanceRunActive,
897+
options =
898+
Nav3PerformanceMapperMode.entries.map { mode ->
899+
mode.label to { state.mapperMode = mode }
900+
},
901+
)
902+
877903
PerfModeRow(
878904
label = "Extractor cost",
879905
selectedLabel = state.extractorMode.label,
880-
enabled = !state.performanceRunActive,
906+
enabled = customExtractorControlsEnabled,
881907
options =
882908
Nav3PerformanceExtractorMode.entries.map { mode ->
883909
mode.label to { state.extractorMode = mode }
884910
},
885911
)
912+
886913
PerfModeRow(
887914
label = "Argument shape",
888915
selectedLabel = state.argumentMode.label,
889-
enabled = !state.performanceRunActive,
916+
enabled = customExtractorControlsEnabled,
890917
options =
891918
Nav3PerformanceArgumentMode.entries.map { mode ->
892919
mode.label to { state.argumentMode = mode }
@@ -901,16 +928,19 @@ internal fun Nav3PerformancePanel(
901928
state.recompositionRequests.toString(),
902929
tag = navPerformanceTag("recomposition_requests"),
903930
)
931+
904932
PerfInfoRow(
905933
"Navigation mutations",
906934
state.navigationMutations.toString(),
907935
tag = navPerformanceTag("navigation_mutations"),
908936
)
937+
909938
PerfInfoRow(
910939
"Destination changes",
911940
state.destinationChanges.toString(),
912941
tag = navPerformanceTag("destination_changes"),
913942
)
943+
914944
if (showExtractorControls) {
915945
if (state.performanceRunActive) {
916946
Text("Metrics are published when the fixed run completes.")
@@ -972,13 +1002,15 @@ internal fun Nav3PerformancePanel(
9721002
)
9731003
}
9741004
}
1005+
9751006
Button(
9761007
enabled = !state.performanceRunActive,
9771008
onClick = { state.resetCounters() },
9781009
modifier = Modifier.fillMaxWidth(),
9791010
) {
9801011
Text("Reset Counters")
9811012
}
1013+
9821014
Button(
9831015
enabled = !state.performanceRunActive,
9841016
onClick = { state.publishDiagnostics(currentRoute = currentRoute, backStack = backStack) },
@@ -1243,7 +1275,7 @@ private fun Nav3PerformanceState.argumentsExtractorAverageMicros(): String =
12431275
averageMicros(argumentsExtractorNanos, argumentsExtractorCalls)
12441276

12451277
private fun averageMicros(totalNanos: Long, count: Int): String =
1246-
if (count == 0) "0 us" else "${totalNanos / count / 1_000} us"
1278+
if (count == 0) "0 us" else String.format(Locale.US, "%.2f us", totalNanos / count / 1_000.0)
12471279

12481280
private fun formatNanos(durationNanos: Long): String =
12491281
if (durationNanos < 1_000_000L) {

0 commit comments

Comments
 (0)