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