1+ package  org.nsh07.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+ import  androidx.test.platform.app.InstrumentationRegistry 
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+         //  The application id for the running build variant is read from the instrumentation arguments.
52+         rule.measureRepeated(
53+             packageName =  InstrumentationRegistry .getArguments().getString(" targetAppId" 
54+                 ? :  throw  Exception (" targetAppId not passed as instrumentation runner arg" 
55+             metrics =  listOf (StartupTimingMetric ()),
56+             compilationMode =  compilationMode,
57+             startupMode =  StartupMode .COLD ,
58+             iterations =  10 ,
59+             setupBlock =  {
60+                 pressHome()
61+             },
62+             measureBlock =  {
63+                 startActivityAndWait()
64+ 
65+                 //  TODO Add interactions to wait for when your app is fully drawn.
66+                 //  The app is fully drawn when Activity.reportFullyDrawn is called.
67+                 //  For Jetpack Compose, you can use ReportDrawn, ReportDrawnWhen and ReportDrawnAfter
68+                 //  from the AndroidX Activity library.
69+ 
70+                 //  Check the UiAutomator documentation for more information on how to
71+                 //  interact with the app.
72+                 //  https://d.android.com/training/testing/other-components/ui-automator
73+             }
74+         )
75+     }
76+ }
0 commit comments