Skip to content

Latest commit

 

History

History
109 lines (85 loc) · 3.83 KB

File metadata and controls

109 lines (85 loc) · 3.83 KB

Benchmarking in AndroidX

[TOC]

Macrobenchmark (new!) Benchmark (existing!)
Measure high-level entry points (Activity launch / Scrolling a list) Measure individual functions
Out-of-process test of full app In-process test of CPU work
Slow iteration speed (Often several minutes) Fast iteration speed (Often less than 10 seconds)
Results come with profiling traces Optional method sampling/tracing
Min API 29 Min API 14

The public documentation for macrobenchmark explains how to use the library. This page focuses on specifics to writing library macrobenchmarks in the AndroidX repo. If you're looking for measuring CPU perf of individual functions, see the guide for MICRObenchmarks here.

Writing the benchmark

Benchmarks are just regular instrumentation tests! Just use the MacrobenchmarkRule provided by the library:

Kotlin {.new-tab}

@RunWith(AndroidJUnit4::class)
class MyStartupBenchmark {
    @get:Rule
    val benchmarkRule = MacrobenchmarkRule()

    @Test
    fun startup() = benchmarkRule.measureRepeated(
        packageName = "mypackage.myapp",
        metrics = listOf(StartupTimingMetric()),
        startupMode = StartupMode.COLD,
        iterations = 5
    ) { // this = MacrobenchmarkScope
        pressHome()
        val intent = Intent()
        intent.setPackage("mypackage.myapp")
        intent.setAction("mypackage.myapp.myaction")
        startActivityAndWait(intent)
    }
}

Java {.new-tab}

// TODO: Java APIs are not yet available.

Project structure

As in the public documentation, macrobenchmarks in the AndroidX repo are comprised of an app, and a separate macrobenchmark module. Additional setups steps/constraints for the AndroidX repository are listed below.

  1. App and macrobenchmark modules must be unique, and map 1:1.

  2. Target app path in settings.gradle must end with :integration-tests:macrobenchmark-target.

  3. Macrobenchmark library path must be at the same path, but instead ending with :integration-tests:macrobenchmark

  4. An entry should be placed in AffectedModuleDetector to recognize macrobenchmark dependency on target app module, for example

Compose Macrobenchmark Examples:

Note: Compose macrobenchmarks are generally duplicated with View system counterparts, defined in :benchmark:integration-tests:macrobenchmark-target. This is how we compare performance of the two systems.