|
| 1 | +package io.github.droidkaigi.confsched2023 |
| 2 | + |
| 3 | +import androidx.benchmark.macro.BaselineProfileMode |
| 4 | +import androidx.benchmark.macro.CompilationMode |
| 5 | +import androidx.benchmark.macro.StartupMode |
| 6 | +import androidx.benchmark.macro.StartupTimingMetric |
| 7 | +import androidx.benchmark.macro.junit4.MacrobenchmarkRule |
| 8 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 9 | +import androidx.test.filters.LargeTest |
| 10 | +import org.junit.Rule |
| 11 | +import org.junit.Test |
| 12 | +import org.junit.runner.RunWith |
| 13 | + |
| 14 | +/** |
| 15 | + * This test class benchmarks the speed of app startup. |
| 16 | + * Run this benchmark to verify how effective a Baseline Profile is. |
| 17 | + * It does this by comparing [CompilationMode.None], which represents the app with no Baseline |
| 18 | + * Profiles optimizations, and [CompilationMode.Partial], which uses Baseline Profiles. |
| 19 | + * |
| 20 | + * Run this benchmark to see startup measurements and captured system traces for verifying |
| 21 | + * the effectiveness of your Baseline Profiles. You can run it directly from Android |
| 22 | + * Studio as an instrumentation test, or run all benchmarks with this Gradle task: |
| 23 | + * ``` |
| 24 | + * ./gradlew :baselineprofile:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=Macrobenchmark |
| 25 | + * ``` |
| 26 | + * |
| 27 | + * You should run the benchmarks on a physical device, not an Android emulator, because the |
| 28 | + * emulator doesn't represent real world performance and shares system resources with its host. |
| 29 | + * |
| 30 | + * For more information, see the [Macrobenchmark documentation](https://d.android.com/macrobenchmark#create-macrobenchmark) |
| 31 | + * and the [instrumentation arguments documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args). |
| 32 | + **/ |
| 33 | +@RunWith(AndroidJUnit4::class) |
| 34 | +@LargeTest |
| 35 | +class StartupBenchmarks { |
| 36 | + |
| 37 | + @get:Rule |
| 38 | + val rule = MacrobenchmarkRule() |
| 39 | + |
| 40 | + @Test |
| 41 | + fun startupCompilationNone() = |
| 42 | + benchmark(CompilationMode.None()) |
| 43 | + |
| 44 | + @Test |
| 45 | + fun startupCompilationBaselineProfiles() = |
| 46 | + benchmark(CompilationMode.Partial(BaselineProfileMode.Require)) |
| 47 | + |
| 48 | + private fun benchmark(compilationMode: CompilationMode) { |
| 49 | + rule.measureRepeated( |
| 50 | + packageName = PACKAGE_NAME, |
| 51 | + metrics = listOf(StartupTimingMetric()), |
| 52 | + compilationMode = compilationMode, |
| 53 | + startupMode = StartupMode.COLD, |
| 54 | + iterations = 10, |
| 55 | + setupBlock = { |
| 56 | + pressHome() |
| 57 | + }, |
| 58 | + measureBlock = { |
| 59 | + startActivityAndWait() |
| 60 | + |
| 61 | + // TODO Add interactions to wait for when your app is fully drawn. |
| 62 | + // The app is fully drawn when Activity.reportFullyDrawn is called. |
| 63 | + // For Jetpack Compose, you can use ReportDrawn, ReportDrawnWhen and ReportDrawnAfter |
| 64 | + // from the AndroidX Activity library. |
| 65 | + |
| 66 | + // Check the UiAutomator documentation for more information on how to |
| 67 | + // interact with the app. |
| 68 | + // https://d.android.com/training/testing/other-components/ui-automator |
| 69 | + } |
| 70 | + ) |
| 71 | + } |
| 72 | +} |
0 commit comments