Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@
android:exported="false"
android:theme="@style/AppTheme.Main" />

<!-- Exported so adb can start performance runs, in release builds too. See Nav3PerformanceLaunch. -->
<activity
android:name=".navigation.nav3.Nav3Activity"
android:exported="false"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.Main" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@ class Nav3Activity : ComponentActivity() {

NavigationTransactionHistory.install()

// A recreated Activity doesn't repeat the requested run.
val performanceLaunch = if (savedInstanceState == null) intent.nav3PerformanceLaunch() else null

setContent {
Nav3SampleTheme {
Nav3SampleApp(
performanceState = performanceState,
performanceLaunch = performanceLaunch,
configuration = configuration,
transactions = NavigationTransactionHistory.transactions,
showActivityUiLoadTransactionDelayMessage = showActivityUiLoadTransactionDelayMessage,
Expand Down Expand Up @@ -161,6 +165,7 @@ class Nav3Activity : ComponentActivity() {
@Composable
private fun Nav3SampleApp(
performanceState: Nav3PerformanceState,
performanceLaunch: Nav3PerformanceLaunch?,
configuration: NavigationSampleConfig,
transactions: List<NavigationTransactionTrace>,
showActivityUiLoadTransactionDelayMessage: Boolean,
Expand Down Expand Up @@ -288,6 +293,15 @@ private fun Nav3SampleApp(
controller = customTransactionController,
)

// Compose runs effects in composition order, so this and the effect after SentryNavEffect
// bracket the SDK's synchronous back stack work, mapper calls included.
if (isPerformanceScenario) {
DisposableEffect(sentryBackStack.toList(), sentryNavOptions) {
performanceState.beginSentryNavEffectTiming()
onDispose {}
}
}

SentryNavEffect(
backStack = sentryBackStack,
backStackEntryMapper = backStackEntryMapper,
Expand Down Expand Up @@ -336,6 +350,47 @@ private fun Nav3SampleApp(
}
}

val selectScenario: (Nav3Scenario) -> Unit = { scenario ->
// Selecting a scenario resets its back stack and counters, so stop any run first.
if (performanceState.performanceRunActive) {
performanceState.cancelPerformanceRun()
}
selectedScenario = scenario
performanceState.stopAutomaticWork()
backStack.openScenario(scenario)
if (scenario == Nav3Scenario.PERFORMANCE) {
performanceState.resetCounters()
performanceState.markNavigationMutation()
}
}
Comment thread
sentry-warden[bot] marked this conversation as resolved.
Resolved

if (performanceLaunch != null) {
LaunchedEffect(Unit) {
selectScenario(Nav3Scenario.PERFORMANCE)
awaitNav3PerformanceFrames()
val preset = performanceLaunch.preset
if (
preset != null &&
!prepareNav3PerformancePreset(
preset = preset,
state = performanceState,
backStack = backStack,
onMaxCapturedBackStackEntriesChange = { maxCapturedBackStackEntries = it },
)
) {
// Leaving Performance cancels the preset. Don't start the run on another scenario.
return@LaunchedEffect
}
performanceLaunch.integrationMode?.let { mode ->
performanceState.updateIntegrationMode(mode)
}
performanceLaunch.run?.let { run ->
awaitNav3PerformanceFrames()
runNav3Performance(run = run, state = performanceState, backStack = backStack)
}
}
}
Comment thread
0xadam-brown marked this conversation as resolved.
Resolved

LaunchedEffect(selectedScenario, sentryBackStack.lastOrNull()) {
tagCurrentNavigationSampleScenario(selectedScenario.label)
}
Expand Down Expand Up @@ -373,18 +428,7 @@ private fun Nav3SampleApp(
maxCapturedBackStackEntries = maxCapturedBackStackEntries,
onTransactionHistoryClick = onShowTransactionHistorySheet,
onRouteWorkSettingsClick = onShowRouteWorkSettings,
onScenarioSelected = { scenario ->
if (scenario != Nav3Scenario.PERFORMANCE && performanceState.performanceRunActive) {
performanceState.cancelPerformanceRun()
}
selectedScenario = scenario
performanceState.stopAutomaticWork()
backStack.openScenario(scenario)
if (scenario == Nav3Scenario.PERFORMANCE) {
performanceState.resetCounters()
performanceState.markNavigationMutation()
}
},
onScenarioSelected = selectScenario,
)
},
bottomBar = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.sentry.Sentry
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Composable
internal fun Nav3PerformanceRoute(
Expand Down Expand Up @@ -84,12 +87,13 @@ internal fun Nav3PerformanceRoute(
)
}

/** Returns false if the preset was cancelled before it finished warming up. */
internal suspend fun prepareNav3PerformancePreset(
preset: Nav3PerformancePreset,
state: Nav3PerformanceState,
backStack: SnapshotStateList<Nav3Route>,
onMaxCapturedBackStackEntriesChange: (Int) -> Unit,
) {
): Boolean {
state.startPerformanceRun("Preparing ${preset.label}")
try {
state.stackDepth = preset.stackDepth
Expand All @@ -107,11 +111,12 @@ internal suspend fun prepareNav3PerformancePreset(
backStack = backStack,
)
) {
return
return false
}
state.resetCounters()
state.suppressNextDestinationChange()
state.cancelPerformanceRun(status = "Ready: ${preset.label}")
return true
} catch (e: CancellationException) {
state.cancelPerformanceRun()
throw e
Expand Down Expand Up @@ -163,6 +168,10 @@ internal suspend fun runNav3Performance(
) {
return
}
state.stopCollectingMeasurements()
if (!flushSentryAndMeasureHeap(state)) {
return
}
state.finishPerformanceRun()
} catch (e: CancellationException) {
state.cancelPerformanceRun()
Expand Down Expand Up @@ -222,6 +231,10 @@ private suspend fun runNav3PerformanceAbComparison(
return
}
state.stopCollectingMeasurements()
// Also gives the next side an empty send queue.
if (!flushSentryAndMeasureHeap(state)) {
return
}
results += state.performanceSummary(mode.label)
}
} finally {
Expand Down Expand Up @@ -274,10 +287,22 @@ private fun performNav3PerformanceIteration(
}
}

/** Sends what the run queued, then reads the heap again. Returns false if the run was cancelled. */
private suspend fun flushSentryAndMeasureHeap(state: Nav3PerformanceState): Boolean {
state.updatePerformanceStatus("Sending queued events")
withContext(Dispatchers.IO) { Sentry.flush(PERFORMANCE_FLUSH_TIMEOUT_MILLIS) }
if (!state.performanceRunActive) {
return false
}
state.recordLiveHeapAfterFlush()
return true
}

internal suspend fun awaitNav3PerformanceFrames() {
withFrameNanos {}
withFrameNanos {}
}

private const val PERFORMANCE_WARM_UP_ITERATIONS = 5
private const val PERFORMANCE_MEASURED_ITERATIONS = 20
private const val PERFORMANCE_FLUSH_TIMEOUT_MILLIS = 15_000L
internal const val PERFORMANCE_MEASURED_ITERATIONS = 100
Loading
Loading