package mihon.baselineprofile import androidx.benchmark.macro.BaselineProfileMode import androidx.benchmark.macro.CompilationMode import androidx.benchmark.macro.StartupMode import androidx.benchmark.macro.StartupTimingMetric import androidx.benchmark.macro.junit4.MacrobenchmarkRule import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith /** * Run this benchmark from Studio to see startup measurements, and captured system traces * for investigating your app's performance from a cold state. */ @RunWith(AndroidJUnit4ClassRunner::class) class ColdStartupBenchmark : AbstractStartupBenchmark(StartupMode.COLD) /** * Run this benchmark from Studio to see startup measurements, and captured system traces * for investigating your app's performance from a warm state. */ @RunWith(AndroidJUnit4ClassRunner::class) class WarmStartupBenchmark : AbstractStartupBenchmark(StartupMode.WARM) /** * Run this benchmark from Studio to see startup measurements, and captured system traces * for investigating your app's performance from a hot state. */ @RunWith(AndroidJUnit4ClassRunner::class) class HotStartupBenchmark : AbstractStartupBenchmark(StartupMode.HOT) /** * This test class benchmarks the speed of app startup. * Run this benchmark to verify how effective a Baseline Profile is. * It does this by comparing [CompilationMode.None], which represents the app with no Baseline * Profiles optimizations, and [CompilationMode.Partial], which uses Baseline Profiles. * * Run this benchmark to see startup measurements and captured system traces for verifying * the effectiveness of your Baseline Profiles. You can run it directly from Android * Studio as an instrumentation test, or run all benchmarks for a variant, for example benchmarkRelease, * with this Gradle task: * ``` * ./gradlew :baselineprofile:connectedBenchmarkReleaseAndroidTest * ``` * * You should run the benchmarks on a physical device, not an Android emulator, because the * emulator doesn't represent real world performance and shares system resources with its host. * * For more information, see the [Macrobenchmark documentation](https://d.android.com/macrobenchmark#create-macrobenchmark) * and the [instrumentation arguments documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args). **/ abstract class AbstractStartupBenchmark(private val startupMode: StartupMode) { @get:Rule val rule = MacrobenchmarkRule() @Test fun startupCompilationNone() = benchmark(CompilationMode.None()) @Test fun startupCompilationBaselineProfilesDisabled() = benchmark(CompilationMode.Partial(BaselineProfileMode.Disable)) @Test fun startupCompilationBaselineProfiles() = benchmark(CompilationMode.Partial(BaselineProfileMode.Require)) @Test fun startupCompilationFull() = benchmark(CompilationMode.Full()) private fun benchmark(compilationMode: CompilationMode) { // The application id for the running build variant is read from the instrumentation arguments. rule.measureRepeated( packageName = TARGET_PACKAGE_NAME, metrics = listOf(StartupTimingMetric()), compilationMode = compilationMode, startupMode = startupMode, iterations = 10, setupBlock = { pressHome() }, measureBlock = { startActivityAndWait() // TODO Add interactions to wait for when your app is fully drawn. // The app is fully drawn when Activity.reportFullyDrawn is called. // For Jetpack Compose, you can use ReportDrawn, ReportDrawnWhen and ReportDrawnAfter // from the AndroidX Activity library. // Check the UiAutomator documentation for more information on how to // interact with the app. // https://d.android.com/training/testing/other-components/ui-automator } ) } }