1. Before you begin
In this codelab you'll learn to use the macrobenchmark library. You will measure app startup timing, which is a key metric for user engagement, and frame timing, which hints at where jank may occur in your app.
What you'll need
- Android Studio
- Knowledge of Kotlin
- Basic understanding of testing on Android
- A physical Android device with Android 6 (API level 23) or higher
What you'll do
- Add a benchmarking module to an existing application
- Measure app startup and frame timing
What you'll learn
- Reliably measure application performance
2. Getting set up
To get started, clone the Github repository from the command line by using the following command:
$ git clone https://github.com/android/codelab-android-performance.git
Alternatively, you can download two zip files:
Open Project into Android Studio
- On the Welcome to Android Studio window select
Open an Existing Project - Select the folder
[Download Location]/android-performance/benchmarking(tip: make sure you select thebenchmarkingdirectory containingbuild.gradle) - When Android Studio has imported the project, ensure that you can run the
appmodule to build the sample application we'll benchmark.
3. Introduction to Jetpack Macrobenchmark
Jetpack Macrobenchmark library measures performance of larger end-user interactions, such as startup, interacting with the UI, and animations. The library provides direct control over the performance environment you're testing. It allows you to control compiling, starting and stopping your application to directly measure app startup, frame timings and traced code sections.
With Jetpack Macrobenchmark you can:
- measure the app multiple times with deterministic launch patterns and scroll speeds
- smooth out performance variance by averaging results over multiple test runs
- control the compilation state of your app – a major factor in performance stability
- check real world performance with local reproduction of install-time optimizations performed by Google Play Store
Instrumentations using this library don't call your application code directly, but instead navigate your application as a user would – touching, clicking, swiping, etc. The measurement happens on a device during these interactions. If you want to measure parts of your application code directly, see Jetpack Microbenchmark instead.
Writing a benchmark is like writing an instrumented test, except you don't need to verify the state your app is in. The benchmarks use the JUnit syntax (@RunWith, @Rule, @Test, etc.), but the tests run in a separate process to allow restarting or pre-compiling of your app. This allows us to run your app without interfering with its internal states, just as a user would. We do this by using UiAutomator to interact with the target application.
The sample app
In this codelab, you'll work with the JetSnack sample application. It's a virtual snack ordering app, using Jetpack Compose. To measure the performance of the application, you don't need to know the details of how the app is architected. What you'll need to understand is how the app behaves and the structure of the UI so you can access the UI elements from the benchmarks. Run the app and get familiar with the basic screens by ordering snacks of your choice.

4. Add the Macrobenchmark library
Macrobenchmark requires adding a new Gradle module to your project. The easiest way to add it to the project is with the Android Studio module wizard.
Open the new module dialog (for example, right-click your project or module in the Project panel