Macrobenchmark and Microbenchmark

Performance Mid-level Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Pick the fix

You want to know whether a new feed design scrolls smoothly on a mid-range phone. Which tool?

Options
Hint 1 of 1

Is this a user journey or a single function?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Macrobenchmark with FrameTimingMetric

Correct. It measures real frame times for a scrolling journey.

Why not the others?
  • B Microbenchmark of the item composable. Doesn't measure real scrolling.
  • C A unit test with a timer. Runs on the JVM, not the device.
  • D Logcat. No frame metrics.

30-second answer

Macrobenchmark tests whole user journeys from outside the app, such as cold start or scrolling, on a real device or emulator, reporting metrics like StartupTimingMetric and FrameTimingMetric. It runs against a release-like build. Microbenchmark measures small pieces of code in a loop, like a parser or a sort, handling warm-up and clock stability. Use macro for user-facing speed, micro for hot functions.

Practise saying it · 25 s. No recording, just a timer

Compare Macrobenchmark and Microbenchmark.

Say your answer out loud, then check which of these points you covered:

  • Macro: journeys from outside
  • Startup and frame metrics
  • Micro: small code in a loop
  • Release builds, real devices

Deep dive

Macrobenchmark lives in a separate com.android.test module that targets the app.

CompilationMode lets you compare no compilation, a Baseline Profile and full AOT.

Run benchmarks on physical devices with stable conditions; emulator numbers vary.

Track results in CI to catch regressions.

A startup benchmark Compile-checked · Kotlin 2.4.21
@RunWith(AndroidJUnit4::class)
class StartupBenchmark {
    @get:Rule val rule = MacrobenchmarkRule()

    @Test
    fun coldStart() = rule.measureRepeated(
        packageName = "com.example.app",
        metrics = listOf(StartupTimingMetric()),
        iterations = 10,
        startupMode = StartupMode.COLD,
    ) {
        pressHome()
        startActivityAndWait()
    }
}

Common mistakes

  • Micro-benchmarking startup.
  • Running benchmarks on a debug build.
  • Reading one run instead of the median of many.

Senior take & follow-ups

Two sentences: macro measures journeys, micro measures functions.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you see the Baseline Profile's effect?

    Show hint

    Run startup with CompilationMode.None vs Partial.

  2. Why median, not one run?

    Show hint

    Device noise; benchmarks repeat iterations and report statistics.

Suggest a fix if something here is wrong or out of date.