android6-6 Exercise 3: Macrobenchmark and a baseline profile ========================================================= // A Macrobenchmark module (docs: use the Baseline Profile / Macrobenchmark module templates) @RunWith(AndroidJUnit4::class) class StartupBenchmark { @get:Rule val benchmarkRule = MacrobenchmarkRule() @Test fun startupNoCompilation() = startup(CompilationMode.None()) @Test fun startupBaselineProfile() = startup(CompilationMode.Partial()) // docs' comparison private fun startup(mode: CompilationMode) = benchmarkRule.measureRepeated( packageName = "com.example.notes", metrics = listOf(StartupTimingMetric()), iterations = 10, startupMode = StartupMode.COLD, compilationMode = mode ) { pressHome() startActivityAndWait() } } // The generator (docs: BaselineProfileRule, in the Baseline Profile module) @RunWith(AndroidJUnit4::class) class BaselineProfileGenerator { @get:Rule val rule = BaselineProfileRule() @Test fun generate() = rule.collect(packageName = "com.example.notes") { pressHome() startActivityAndWait() // Walk the journeys users take most: scroll the list, open a note } } // Tell the benchmark (and the system) when the screen is really ready (docs: ReportDrawnWhen) @Composable fun NotesScreen(state: NotesUiState) { ReportDrawnWhen { !state.isLoading } // ... } Steps: File > New > Module, choose the Baseline Profile Generator or Macrobenchmark template (docs); set the package name; run on a real device if possible (docs: Gradle Managed Device needs an aosp image for generation). Run startupNoCompilation and startupBaselineProfile, record the median StartupTimingMetric numbers, generate the profile, rerun. Report device, build type, numbers, and whether the profile helped. The docs' example numbers (229.0 ms with a profile, 324.8 ms without compilation) are an illustration only. Code unrun.