DEV Community

myougaTheAxo
myougaTheAxo

Posted on

Macrobenchmark: Measuring Startup Time and Frame Metrics

Benchmarking tools measure real device performance. Learn StartupMode, FrameTimingMetric, and baseline testing.

Add Benchmark Dependency

benchmarkImplementation("androidx.benchmark:benchmark-macro-junit4:1.1.1")
Enter fullscreen mode Exit fullscreen mode

Measure Startup Time

import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.junit4.MacrobenchmarkRule

class StartupBenchmark {
    @get:Rule
    val benchmarkRule = MacrobenchmarkRule()

    @Test
    fun coldStartup() = benchmarkRule.measureRepeated(
        packageName = "com.example.app",
        metrics = listOf(StartupTimings()),
        iterations = 5,
        startupMode = StartupMode.COLD,
        setupBlock = {
            pressHome()
            startActivityAndWait()
        }
    )
}
Enter fullscreen mode Exit fullscreen mode

Frame Metrics

import androidx.benchmark.macro.FrameTimingMetric

@Test
fun scrollFrameRate() = benchmarkRule.measureRepeated(
    packageName = "com.example.app",
    metrics = listOf(FrameTimingMetric()),
    setupBlock = {
        startActivityAndWait()
    },
    measureBlock = {
        // Simulate scroll
        repeat(10) {
            device.shell("input swipe 100 500 100 100")
            device.waitForIdle()
        }
    }
)
Enter fullscreen mode Exit fullscreen mode

Baseline Comparison

// Run once to establish baseline
gradlew :benchmark:pixel6Benchmark

// After optimization, compare
gradlew :benchmark:pixel6Benchmark --no-snapshot
Enter fullscreen mode Exit fullscreen mode

Key Metrics

  • StartupTimings: Cold/Warm/Hot startup duration
  • FrameTimingMetric: Frame jank, 95th percentile time
  • PowerMetrics: Battery consumption (requires API 30+)

Run benchmarks on actual devices with consistent charging state. Disable background processes for reliable results. Target <2000ms cold startup.


8 Android app templates (Habit Tracker, Budget Manager, Task Manager, and more) available on Gumroad

Top comments (0)