AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 1 | # Benchmarking in AndroidX |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | The public documentation at |
| 6 | [d.android.com/benchmark](http://d.android.com/benchmark) explains how to use |
| 7 | the library - this page focuses on specifics to writing libraries in the |
| 8 | AndroidX repo, and our continuous testing / triage process. |
| 9 | |
AndroidX Core Team | 0e7745f | 2021-04-08 17:00:10 +0000 | [diff] [blame^] | 10 | This page is for MICRO benchmarks measuring CPU performance of small sections of |
| 11 | code. If you're looking for measuring startup or jank, see the guide for |
| 12 | MACRObenchmarks [here](macrobenchmarking). |
| 13 | |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 14 | ### Writing the benchmark |
| 15 | |
| 16 | Benchmarks are just regular instrumentation tests! Just use the |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 17 | [`BenchmarkRule`](https://android.googlesource.com/platform/frameworks/support/+/androidx-main/benchmark/junit4/src/main/java/androidx/benchmark/junit4/BenchmarkRule.kt) |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 18 | provided by the library: |
| 19 | |
| 20 | <section class="tabs"> |
| 21 | |
| 22 | #### Kotlin {.new-tab} |
| 23 | |
| 24 | ```kotlin |
| 25 | @RunWith(AndroidJUnit4::class) |
| 26 | class ViewBenchmark { |
| 27 | @get:Rule |
| 28 | val benchmarkRule = BenchmarkRule() |
| 29 | |
| 30 | @Test |
| 31 | fun simpleViewInflate() { |
| 32 | val context = InstrumentationRegistry |
| 33 | .getInstrumentation().targetContext |
| 34 | val inflater = LayoutInflater.from(context) |
| 35 | val root = FrameLayout(context) |
| 36 | |
| 37 | benchmarkRule.measure { |
| 38 | inflater.inflate(R.layout.test_simple_view, root, false) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | ``` |
| 43 | |
| 44 | #### Java {.new-tab} |
| 45 | |
| 46 | ```java |
| 47 | @RunWith(AndroidJUnit4.class) |
| 48 | public class ViewBenchmark { |
| 49 | @Rule |
| 50 | public BenchmarkRule mBenchmarkRule = new BenchmarkRule(); |
| 51 | |
| 52 | @Test |
| 53 | public void simpleViewInflate() { |
| 54 | Context context = InstrumentationRegistry |
| 55 | .getInstrumentation().getTargetContext(); |
| 56 | final BenchmarkState state = mBenchmarkRule.getState(); |
| 57 | LayoutInflater inflater = LayoutInflater.from(context); |
| 58 | FrameLayout root = new FrameLayout(context); |
| 59 | |
| 60 | while (state.keepRunning()) { |
| 61 | inflater.inflate(R.layout.test_simple_view, root, false); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | </section> |
| 68 | |
| 69 | ## Project structure |
| 70 | |
| 71 | As in the public documentation, benchmarks in the AndroidX repo are test-only |
| 72 | library modules. Differences for AndroidX repo: |
| 73 | |
AndroidX Core Team | 0e7745f | 2021-04-08 17:00:10 +0000 | [diff] [blame^] | 74 | 1. Module must live in `integration-tests` group directory |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 75 | 1. Module name must end with `-benchmark` in `settings.gradle`. |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 76 | |
| 77 | ### I'm lazy and want to start quickly |
| 78 | |
| 79 | Start by copying one of the following projects: |
| 80 | |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 81 | * [navigation-benchmark](https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/navigation/benchmark/) |
| 82 | * [recyclerview-benchmark](https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/recyclerview/recyclerview-benchmark/) |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 83 | |
| 84 | ### Compose |
| 85 | |
| 86 | Compose builds the benchmark from source, so usage matches the rest of the |
| 87 | AndroidX project. See existing Compose benchmark projects: |
| 88 | |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 89 | * [Compose UI benchmarks](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/integration-tests/benchmark/) |
| 90 | * [Compose Runtime benchmarks](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/runtime/runtime/compose-runtime-benchmark/) |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 91 | |
| 92 | ## Profiling |
| 93 | |
| 94 | ### Command Line |
| 95 | |
| 96 | The benchmark library supports capturing profiling information - sampled and |
| 97 | method - from the command line. Here's an example which runs the |
| 98 | `androidx.ui.benchmark.test.CheckboxesInRowsBenchmark#draw` method with |
| 99 | `MethodSampling` profiling: |
| 100 | |
| 101 | ``` |
| 102 | ./gradlew compose:integ:bench:cC \ |
| 103 | -P android.testInstrumentationRunnerArguments.androidx.benchmark.profiling.mode=MethodSampling \ |
| 104 | -P android.testInstrumentationRunnerArguments.class=androidx.ui.benchmark.test.CheckboxesInRowsBenchmark#draw |
| 105 | ``` |
| 106 | |
| 107 | The command output will tell you where to look for the file on your host |
| 108 | machine: |
| 109 | |
| 110 | ``` |
| 111 | 04:33:49 I/Benchmark: Benchmark report files generated at |
AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 112 | /androidx-main/out/ui/ui/integration-tests/benchmark/build/outputs/connected_android_test_additional_output |
AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 113 | ``` |
| 114 | |
| 115 | To inspect the captured trace, open the appropriate `*.trace` file in that |
| 116 | directory with Android Studio, using `File > Open`. |
| 117 | |
| 118 | For more information on the `MethodSampling` and `MethodTracing` profiling |
| 119 | modes, see the |
| 120 | [Studio Profiler configuration docs](https://developer.android.com/studio/profile/cpu-profiler#configurations), |
| 121 | specifically Java Sampled Profiling, and Java Method Tracing. |
| 122 | |
| 123 |  |
| 124 | |
| 125 | ### Advanced: Simpleperf Method Sampling |
| 126 | |
| 127 | [Simpleperf](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/) |
| 128 | offers more accurate profiling for apps than standard method sampling, due to |
| 129 | lower overhead (as well as C++ profiling support). Simpleperf support will be |
| 130 | simplified and improved over time. |
| 131 | |
| 132 | [Simpleperf app profiling docs](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/android_application_profiling.md). |
| 133 | |
| 134 | #### Device |
| 135 | |
| 136 | Get an API 28+ device (Or a rooted API 27 device). The rest of this section is |
| 137 | about *why* those constraints exist, skip if not interested. |
| 138 | |
| 139 | Simpleperf has restrictions about where it can be used - Jetpack Benchmark will |
| 140 | only support API 28+ for now, due to |
| 141 | [platform/simpleperf constraints](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/android_application_profiling.md#prepare-an-android-application) |
| 142 | (see last subsection titled "If you want to profile Java code"). Summary is: |
| 143 | |
| 144 | - <=23 (M): Unsupported for Java code. |
| 145 | |
| 146 | - 24-25 (N): Requires compiled Java code. We haven't investigated support. |
| 147 | |
| 148 | - 26 (O): Requires compiled Java code, and wrapper script. We haven't |
| 149 | investigated support. |
| 150 | |
| 151 | - 27 (P): Can profile all Java code, but requires `userdebug`/rooted device |
| 152 | |
| 153 | - \>=28 (Q): Can profile all Java code, requires profileable (or |
| 154 | `userdebug`/rooted device) |
| 155 | |
| 156 | We aren't planning to support profiling debuggable APK builds, since they're |
| 157 | misleading for profiling. |
| 158 | |
| 159 | #### Initial setup |
| 160 | |
| 161 | Currently, we rely on Python scripts built by the simpleperf team. We can |
| 162 | eventually build this into the benchmark library / gradle plugin. Download the |
| 163 | scripts from AOSP: |
| 164 | |
| 165 | ``` |
| 166 | # copying to somewhere outside of the androidx repo |
| 167 | git clone https://android.googlesource.com/platform/system/extras ~/simpleperf |
| 168 | ``` |
| 169 | |
| 170 | Next configure your path to ensure the ADB that the scripts will use matches the |
| 171 | androidx tools: |
| 172 | |
| 173 | ``` |
| 174 | export PATH=$PATH:<path/to/androidx>/prebuilts/fullsdk-<linux or darwin>/platform-tools |
| 175 | ``` |
| 176 | |
| 177 | Now, setup your device for simpleperf: |
| 178 | |
| 179 | ``` |
| 180 | ~/simpleperf/simpleperf/scripts/api_profiler.py prepare --max-sample-rate 10000000 |
| 181 | ``` |
| 182 | |
| 183 | #### Build and Run, Option 1: Studio (slightly recommended) |
| 184 | |
| 185 | Running from Studio is simpler, since you don't have to manually install and run |
| 186 | the APKs, avoiding Gradle. |
| 187 | |
| 188 | Add the following to the benchmark module's build.gradle: |
| 189 | |
| 190 | ``` |
| 191 | android { |
| 192 | defaultConfig { |
| 193 | // DO NOT COMMIT!! |
| 194 | testInstrumentationRunnerArgument 'androidx.benchmark.profiling.mode', 'MethodSamplingSimpleperf' |
| 195 | // Optional: Control freq / duration. |
| 196 | testInstrumentationRunnerArgument 'androidx.benchmark.profiler.sampleFrequency', '1000000' |
| 197 | testInstrumentationRunnerArgument 'androidx.benchmark.profiler.sampleDurationSeconds', '5' |
| 198 | } |
| 199 | } |
| 200 | ``` |
| 201 | |
| 202 | And run the test or tests you'd like to measure from within Studio. |
| 203 | |
| 204 | #### Build and Run, Option 2: Command Line |
| 205 | |
| 206 | **Note - this will be significantly simplified in the future** |
| 207 | |
| 208 | Since we're not using AGP to pull the files yet, we can't invoke the benchmark |
| 209 | through Gradle, because Gradle uninstalls after each test run. Instead, let's |
| 210 | just build and run manually: |
| 211 | |
| 212 | ``` |
| 213 | ./gradlew compose:integration-tests:benchmark:assembleReleaseAndroidTest |
| 214 | |
| 215 | adb install -r ../../../out/ui/compose/integration-tests/benchmark/build/outputs/apk/androidTest/release/benchmark-release-androidTest.apk |
| 216 | |
| 217 | # run the test (can copy this line from Studio console, when running a benchmark) |
| 218 | adb shell am instrument -w -m --no-window-animation -e androidx.benchmark.profiling.mode MethodSamplingSimpleperf -e debug false -e class 'androidx.ui.benchmark.test.CheckboxesInRowsBenchmark#toggleCheckbox_draw' androidx.ui.benchmark.test/androidx.benchmark.junit4.AndroidBenchmarkRunner |
| 219 | ``` |
| 220 | |
| 221 | #### Pull and open the trace |
| 222 | |
| 223 | ``` |
| 224 | # move the files to host |
| 225 | # (Note: removes files from device) |
| 226 | ~/simpleperf/simpleperf/scripts/api_profiler.py collect -p androidx.ui.benchmark.test -o ~/simpleperf/results |
| 227 | |
| 228 | # create/open the HTML report |
| 229 | ~/simpleperf/simpleperf/scripts/report_html.py -i ~/simpleperf/results/CheckboxesInRowsBenchmark_toggleCheckbox_draw\[1\].data |
| 230 | ``` |
| 231 | |
| 232 | ### Advanced: Studio Profiling |
| 233 | |
| 234 | Profiling for allocations and simpleperf profiling requires Studio to capture. |
| 235 | |
| 236 | Studio profiling tools require `debuggable=true`. First, temporarily override it |
| 237 | in your benchmark's `androidTest/AndroidManifest.xml`. |
| 238 | |
| 239 | Next choose which profiling you want to do: Allocation, or Sampled (SimplePerf) |
| 240 | |
| 241 | `ConnectedAllocation` will help you measure the allocations in a single run of a |
| 242 | benchmark loop, after warmup. |
| 243 | |
| 244 | `ConnectedSampled` will help you capture sampled profiling, but with the more |
| 245 | detailed / accurate Simpleperf sampling. |
| 246 | |
| 247 | Set the profiling type in your benchmark module's `build.gradle`: |
| 248 | |
| 249 | ``` |
| 250 | android { |
| 251 | defaultConfig { |
| 252 | // Local only, don't commit this! |
| 253 | testInstrumentationRunnerArgument 'androidx.benchmark.profiling.mode', 'ConnectedAllocation' |
| 254 | } |
| 255 | } |
| 256 | ``` |
| 257 | |
| 258 | Run `File > Sync Project with Gradle Files`, or sync if Studio asks you. Now any |
| 259 | benchmark runs in that project will permit debuggable, and pause before and |
| 260 | after the test, to allow you to connect a profiler and start recording, and then |
| 261 | stop recording. |
| 262 | |
| 263 | #### Running and Profiling |
| 264 | |
| 265 | After the benchmark test starts, you have about 20 seconds to connect the |
| 266 | profiler: |
| 267 | |
| 268 | 1. Click the profiler tab at the bottom |
| 269 | 1. Click the plus button in the top left, `<device name>`, `<process name>` |
| 270 | 1. Next step depends on which you intend to capture |
| 271 | |
| 272 | #### Allocations |
| 273 | |
| 274 | Click the memory section, and right click the window, and select `Record |
| 275 | allocations`. Approximately 20 seconds later, right click again and select `Stop |
| 276 | recording`. |