diff --git a/sentry-android-integration-tests/build.gradle.kts b/sentry-android-integration-tests/build.gradle.kts index 34b83c2c067..16be8b4011d 100644 --- a/sentry-android-integration-tests/build.gradle.kts +++ b/sentry-android-integration-tests/build.gradle.kts @@ -2,6 +2,7 @@ plugins { alias(libs.plugins.android.application) apply false alias(libs.plugins.kotlin.android) apply false alias(libs.plugins.kotlin.compose) apply false + alias(libs.plugins.kotlin.serialization) apply false alias(libs.plugins.errorprone) apply false alias(libs.plugins.detekt) apply false alias(libs.plugins.gradle.versions) apply false diff --git a/sentry-android-integration-tests/sentry-samples-android/build.gradle.kts b/sentry-android-integration-tests/sentry-samples-android/build.gradle.kts index aee693598cb..1ad562d1258 100644 --- a/sentry-android-integration-tests/sentry-samples-android/build.gradle.kts +++ b/sentry-android-integration-tests/sentry-samples-android/build.gradle.kts @@ -13,6 +13,7 @@ plugins { id("io.sentry.jdk-target") alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.compose) + alias(libs.plugins.kotlin.serialization) alias(libs.plugins.ksp) alias(libs.plugins.sentry) apply false alias(libs.plugins.sqldelight) diff --git a/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Activity.kt b/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Activity.kt index 807fedad09a..67a8100a6ba 100644 --- a/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Activity.kt +++ b/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Activity.kt @@ -43,6 +43,7 @@ import androidx.compose.ui.semantics.semantics import androidx.compose.ui.semantics.testTagsAsResourceId import androidx.navigation3.runtime.entryProvider import androidx.navigation3.ui.NavDisplay +import io.sentry.compose.navigation3.BackStackEntryMapper import io.sentry.compose.navigation3.SentryBackStackEntry import io.sentry.compose.navigation3.SentryNavEffect import io.sentry.compose.navigation3.SentryNavOptions @@ -232,16 +233,36 @@ private fun Nav3SampleApp( } val performanceExtractorMode = performanceState.extractorMode val performanceArgumentMode = performanceState.argumentMode - val backStackEntryMapper = + val performanceMapperMode = performanceState.mapperMode + val backStackEntryMapper: BackStackEntryMapper = remember( isPerformanceScenario, performanceExtractorMode, performanceArgumentMode, + performanceMapperMode, integrationMode.includeArguments, ) { - { route: Nav3Route -> - val name = - if (isPerformanceScenario) { + if (!isPerformanceScenario) { + // Names each route after its serial name, and adds the arguments the route selects. + BackStackEntryMapper { route -> + BackStackEntryMapper.SerialName.map(route)?.let { entry -> + SentryBackStackEntry(name = entry.name, arguments = route.arguments) + } + } + } else if (performanceMapperMode == Nav3PerformanceMapperMode.SERIAL_NAME) { + // Times each call to the built-in mapper, which records no arguments. Its result is + // returned as is, so a null keeps the reason the mapper reported. + BackStackEntryMapper { route -> + performanceState.recordNameExtraction("Nav3Stress.serialNameMapping") { + BackStackEntryMapper.SerialName.map(route) + } + } + } else { + // A custom mapper, so the performance scenario can time name and argument extraction + // separately and substitute synthetic arguments. It names routes without SerialName, so + // that its timings are a baseline for SerialName's. + BackStackEntryMapper { route -> + val name = performanceState.recordNameExtraction("Nav3Stress.nameExtraction") { consumeNav3PerformanceExtractorWork( performanceExtractorMode, @@ -249,34 +270,30 @@ private fun Nav3SampleApp( ) route.routeName } - } else { - route.routeName - } - val arguments = - if (isPerformanceScenario && !integrationMode.includeArguments) { - null - } else if (isPerformanceScenario) { - performanceState.recordArgumentExtraction("Nav3Stress.argumentsExtraction") { - consumeNav3PerformanceExtractorWork( - performanceExtractorMode, - route.performanceSeed, - ) - if (route is Nav3Route.Performance) { - nav3PerformanceArguments( - performanceArgumentMode, - route.index, - route.generation, + val arguments = + if (!integrationMode.includeArguments) { + null + } else { + performanceState.recordArgumentExtraction("Nav3Stress.argumentsExtraction") { + consumeNav3PerformanceExtractorWork( + performanceExtractorMode, + route.performanceSeed, ) - } else { - route.arguments + if (route is Nav3Route.Performance) { + nav3PerformanceArguments( + performanceArgumentMode, + route.index, + route.generation, + ) + } else { + route.arguments + } } } - } else { - route.arguments - } - SentryBackStackEntry(name = name, arguments = arguments) + SentryBackStackEntry(name = name, arguments = arguments) + } } } diff --git a/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3MultiActivity.kt b/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3MultiActivity.kt index 4309cc66f08..6c9ccb0e886 100644 --- a/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3MultiActivity.kt +++ b/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3MultiActivity.kt @@ -54,7 +54,7 @@ import androidx.navigation3.ui.NavDisplay import androidx.navigationevent.compose.LocalNavigationEventDispatcherOwner import androidx.navigationevent.compose.rememberNavigationEventDispatcherOwner import io.sentry.compose.SentryModifier.sentryTag -import io.sentry.compose.navigation3.SentryBackStackEntry +import io.sentry.compose.navigation3.BackStackEntryMapper import io.sentry.compose.navigation3.SentryNavEffect import io.sentry.compose.navigation3.SentryNavOptions import io.sentry.samples.android.navigation.common.NavigationSampleConfig @@ -307,9 +307,7 @@ internal fun MultiRootNavDisplay( // Keep this before the NavDisplay below: TracedNav3Route's resume tagging relies on it. SentryNavEffect( backStack = backStack, - backStackEntryMapper = { route -> - SentryBackStackEntry(name = route.routeName, arguments = route.arguments) - }, + backStackEntryMapper = BackStackEntryMapper.SerialName, options = options, ) val onBack: () -> Unit = { diff --git a/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3PerformanceControls.kt b/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3PerformanceControls.kt index e923948060f..e10cce8bf96 100644 --- a/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3PerformanceControls.kt +++ b/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3PerformanceControls.kt @@ -52,6 +52,7 @@ internal class Nav3PerformanceState( var autoNavigate by mutableStateOf(false) var extractorMode by mutableStateOf(Nav3PerformanceExtractorMode.NORMAL) var argumentMode by mutableStateOf(Nav3PerformanceArgumentMode.FLAT) + var mapperMode by mutableStateOf(Nav3PerformanceMapperMode.CUSTOM) var integrationMode by mutableStateOf(Nav3PerformanceIntegrationMode.FULL_STACK) private set @@ -321,6 +322,7 @@ internal class Nav3PerformanceState( appendLine("status=$performanceStatus") appendLine("route=$currentRoute") appendLine("tracked_stack=$backStack") + appendLine("mapper_mode=${mapperMode.label}") appendLine("recomposition_requests=$recompositionRequests") appendLine("navigation_mutations=$navigationMutations") appendLine("destination_changes=$destinationChanges") @@ -408,7 +410,7 @@ internal class Nav3PerformanceState( pendingOperation = null } - fun recordNameExtraction(sectionName: String, block: () -> String): String { + fun recordNameExtraction(sectionName: String, block: () -> T): T { val startedAt = System.nanoTime() Trace.beginSection(sectionName) try { @@ -512,6 +514,17 @@ internal enum class Nav3PerformanceArgumentMode(val label: String) { LARGE("Large"), } +/** + * Selects the performance scenario's mapper. + * + * [CUSTOM] provides synthetic arguments and separate extractor timings. [SERIAL_NAME] uses the + * built-in mapper, which extracts names only; each of its calls is timed as name extraction. + */ +internal enum class Nav3PerformanceMapperMode(val label: String) { + CUSTOM("Custom"), + SERIAL_NAME("SerialName"), +} + internal enum class Nav3PerformanceIntegrationMode( val label: String, val captureBackStack: Boolean, @@ -874,19 +887,33 @@ internal fun Nav3PerformancePanel( if (showExtractorControls) { PerfCard(title = "Extractor Inputs") { + val customExtractorControlsEnabled = + !state.performanceRunActive && state.mapperMode == Nav3PerformanceMapperMode.CUSTOM + + PerfModeRow( + label = "Mapper", + selectedLabel = state.mapperMode.label, + enabled = !state.performanceRunActive, + options = + Nav3PerformanceMapperMode.entries.map { mode -> + mode.label to { state.mapperMode = mode } + }, + ) + PerfModeRow( label = "Extractor cost", selectedLabel = state.extractorMode.label, - enabled = !state.performanceRunActive, + enabled = customExtractorControlsEnabled, options = Nav3PerformanceExtractorMode.entries.map { mode -> mode.label to { state.extractorMode = mode } }, ) + PerfModeRow( label = "Argument shape", selectedLabel = state.argumentMode.label, - enabled = !state.performanceRunActive, + enabled = customExtractorControlsEnabled, options = Nav3PerformanceArgumentMode.entries.map { mode -> mode.label to { state.argumentMode = mode } @@ -901,16 +928,19 @@ internal fun Nav3PerformancePanel( state.recompositionRequests.toString(), tag = navPerformanceTag("recomposition_requests"), ) + PerfInfoRow( "Navigation mutations", state.navigationMutations.toString(), tag = navPerformanceTag("navigation_mutations"), ) + PerfInfoRow( "Destination changes", state.destinationChanges.toString(), tag = navPerformanceTag("destination_changes"), ) + if (showExtractorControls) { if (state.performanceRunActive) { Text("Metrics are published when the fixed run completes.") @@ -972,6 +1002,7 @@ internal fun Nav3PerformancePanel( ) } } + Button( enabled = !state.performanceRunActive, onClick = { state.resetCounters() }, @@ -979,6 +1010,7 @@ internal fun Nav3PerformancePanel( ) { Text("Reset Counters") } + Button( enabled = !state.performanceRunActive, onClick = { state.publishDiagnostics(currentRoute = currentRoute, backStack = backStack) }, @@ -1243,7 +1275,7 @@ private fun Nav3PerformanceState.argumentsExtractorAverageMicros(): String = averageMicros(argumentsExtractorNanos, argumentsExtractorCalls) private fun averageMicros(totalNanos: Long, count: Int): String = - if (count == 0) "0 us" else "${totalNanos / count / 1_000} us" + if (count == 0) "0 us" else String.format(Locale.US, "%.2f us", totalNanos / count / 1_000.0) private fun formatNanos(durationNanos: Long): String = if (durationNanos < 1_000_000L) { diff --git a/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Routes.kt b/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Routes.kt index 8dc18c45941..d292324c7e2 100644 --- a/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Routes.kt +++ b/sentry-android-integration-tests/sentry-samples-android/src/main/java/io/sentry/samples/android/navigation/nav3/Nav3Routes.kt @@ -11,8 +11,15 @@ import io.sentry.samples.android.navigation.common.RouteNames import io.sentry.samples.android.navigation.common.RouteSpec import io.sentry.samples.android.navigation.common.RouteSpecs import io.sentry.samples.android.navigation.common.displayRoute +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable -internal fun Nav3Route.displayRoute(): String = routeSpec().displayRoute(arguments) +internal fun Nav3Route.displayRoute(): String = + when (this) { + // Shows the index and generation that tell the performance scenario's routes apart. + is Nav3Route.Performance -> "/$previewName" + else -> routeSpec().displayRoute(arguments) + } internal fun Nav3Route.routeSpec(): RouteSpec = when (this) { @@ -228,8 +235,26 @@ internal fun SnapshotStateList.replacePerformanceTop(generation: Int) set(lastIndex, Nav3Route.Performance(index = lastIndex, generation = generation)) } +/** + * The sample's serializable back stack keys. Each `@SerialName` supplies its Sentry route name, + * using the Nav2 sample's [RouteNames] for shared routes. Only constructor properties are + * serialized. + * + * [routeName] and [arguments] mirror the sample's mapper output so screens, tags, and transaction + * checks can read them without invoking the mapper. Mapping stays in `SentryNavEffect`, keeping + * setup and diagnostics out of composition and screens usable when mapping produces `/unknown`. + */ internal sealed interface Nav3Route { + /** + * The route's name as Sentry records it, without the leading `/`. Each route returns the constant + * it passes to `@SerialName`. + */ val routeName: String + + /** + * The route's arguments as the sample's mapper records them: each property under the Nav2 + * sample's [NavArgs] name, with a `null` campaign left out, as in the Nav2 sample. + */ val arguments: Map get() = emptyMap() @@ -239,87 +264,168 @@ internal sealed interface Nav3Route { val performanceSeed: Int get() = hashCode() + @Serializable + @SerialName(RouteNames.LANDING) data object Landing : Nav3Route { - override val routeName: String = RouteNames.LANDING + override val routeName: String + get() = RouteNames.LANDING } + @Serializable + @SerialName(RouteNames.HOME) data object SingleStack : Nav3Route { - override val routeName: String = RouteNames.HOME + override val routeName: String + get() = RouteNames.HOME } + @Serializable + @SerialName(RouteNames.CUSTOM) data object Custom : Nav3Route { - override val routeName: String = "Custom" + override val routeName: String + get() = RouteNames.CUSTOM } + @Serializable + @SerialName(MultiActivity.NAME) data object MultiActivity : Nav3Route { - override val routeName: String = "MultiActivity" + const val NAME = "MultiActivity" + + override val routeName: String + get() = NAME } + @Serializable + @SerialName(MultiFragment.NAME) data object MultiFragment : Nav3Route { - override val routeName: String = "MultiFragment" + const val NAME = "MultiFragment" + + override val routeName: String + get() = NAME } + @Serializable + @SerialName(FragmentOverlay.NAME) data object FragmentOverlay : Nav3Route { - override val routeName: String = "FragmentOverlay" + const val NAME = "FragmentOverlay" + + override val routeName: String + get() = NAME } + @Serializable + @SerialName(ViewPager2.NAME) data object ViewPager2 : Nav3Route { - override val routeName: String = "ViewPager2" + const val NAME = "ViewPager2" + + override val routeName: String + get() = NAME } + @Serializable + @SerialName(RouteNames.DEEP_LINK) data object DeepLink : Nav3Route { - override val routeName: String = RouteNames.DEEP_LINK + override val routeName: String + get() = RouteNames.DEEP_LINK } + @Serializable + @SerialName(RouteNames.PRODUCT_LIST) data object ProductList : Nav3Route { - override val routeName: String = RouteNames.PRODUCT_LIST + override val routeName: String + get() = RouteNames.PRODUCT_LIST } + @Serializable + @SerialName(RouteNames.PRODUCT_DETAIL) data class ProductDetail( val productId: String, val source: String, val campaign: String? = null, ) : Nav3Route { - override val routeName: String = RouteNames.PRODUCT_DETAIL - override val arguments: Map = - mapOf( - NavArgs.PRODUCT_ID to productId, - NavArgs.SOURCE to source, - NavArgs.CAMPAIGN to campaign, - ) - .filterValues { it != null } - override val previewName: String = "ProductDetail($productId)" + override val routeName: String + get() = RouteNames.PRODUCT_DETAIL + + override val arguments: Map + get() = buildMap { + put(NavArgs.PRODUCT_ID, productId) + put(NavArgs.SOURCE, source) + campaign?.let { put(NavArgs.CAMPAIGN, it) } + } + + override val previewName: String + get() = "ProductDetail($productId)" } + @Serializable + @SerialName(RouteNames.CHECKOUT) data class Checkout(val productId: String) : Nav3Route { - override val routeName: String = RouteNames.CHECKOUT - override val arguments: Map = mapOf(NavArgs.PRODUCT_ID to productId) - override val previewName: String = "Checkout($productId)" + override val routeName: String + get() = RouteNames.CHECKOUT + + override val arguments: Map + get() = mapOf(NavArgs.PRODUCT_ID to productId) + + override val previewName: String + get() = "Checkout($productId)" } + @Serializable + @SerialName(RouteNames.CONFIRMATION) data class Confirmation(val orderId: String) : Nav3Route { - override val routeName: String = RouteNames.CONFIRMATION - override val arguments: Map = mapOf(NavArgs.ORDER_ID to orderId) - override val previewName: String = "Confirmation($orderId)" + override val routeName: String + get() = RouteNames.CONFIRMATION + + override val arguments: Map + get() = mapOf(NavArgs.ORDER_ID to orderId) + + override val previewName: String + get() = "Confirmation($orderId)" } + @Serializable + @SerialName(RouteNames.PROMO_DIALOG) data class PromoDialog(val promoId: String) : Nav3Route { - override val routeName: String = RouteNames.PROMO_DIALOG - override val arguments: Map = mapOf(NavArgs.PROMO_ID to promoId) - override val previewName: String = "PromoDialog($promoId)" + override val routeName: String + get() = RouteNames.PROMO_DIALOG + + override val arguments: Map + get() = mapOf(NavArgs.PROMO_ID to promoId) + + override val previewName: String + get() = "PromoDialog($promoId)" } + @Serializable + @SerialName(RouteNames.SHARE_SHEET) data class ShareSheet(val productId: String) : Nav3Route { - override val routeName: String = RouteNames.SHARE_SHEET - override val arguments: Map = mapOf(NavArgs.PRODUCT_ID to productId) - override val previewName: String = "ShareSheet($productId)" + override val routeName: String + get() = RouteNames.SHARE_SHEET + + override val arguments: Map + get() = mapOf(NavArgs.PRODUCT_ID to productId) + + override val previewName: String + get() = "ShareSheet($productId)" } + @Serializable + @SerialName(Performance.NAME) data class Performance(val index: Int, val generation: Int) : Nav3Route { - override val routeName: String = "Performance" - override val arguments: Map = mapOf("index" to index, "generation" to generation) - override val previewName: String = "Performance($index:$generation)" - override val performanceSeed: Int = 31 * index + generation + override val routeName: String + get() = NAME + + override val arguments: Map + get() = mapOf("index" to index, "generation" to generation) + + override val previewName: String + get() = "Performance($index:$generation)" + + override val performanceSeed: Int + get() = 31 * index + generation + + companion object { + const val NAME = "Performance" + } } }