Macrobenchmark 라이브러리를 사용하여 측정 자동화하기

컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.

기준 프로필이 사용 설정된 상태에서 앱 성능을 테스트하고 해당 테스트 결과를 기준 프로필을 사용 중지한 벤치마크와 비교하려면 Jetpack Macrobenchmark를 사용하는 것이 좋습니다. 이렇게 하면 앱 시작 시간(최초 표시 시간 및 전체 표시 시간)이나 런타임 렌더링 성능을 측정하여 생성된 프레임으로 인해 버벅거림이 발생하는지 확인할 수 있습니다.

Macrobenchmark를 사용하면 CompilationMode API를 통해 측정 전 컴파일을 제어할 수 있습니다. 결과를 측정하려면 다음 스니펫과 같이 compilationMode 매개변수에 적절한 값을 설정해야 합니다.

@RunWith(AndroidJUnit4ClassRunner::class)
class ColdStartupBenchmark {
    @get:Rule
    val benchmarkRule = MacrobenchmarkRule()

    @Test
    fun startupNoCompilation() = startup(CompilationMode.None())

    @Test
    fun startupBaselineProfile() = startup(CompilationMode.Partial())

    @Test
    fun startupFullCompilation() = startup(CompilationMode.Full())

    private fun startup(compilationMode: CompilationMode) = benchmarkRule.measureRepeated(
        packageName = "com.example.macrobenchmark.target",
        metrics = listOf(StartupTimingMetric()),
        compilationMode = compilationMode,
        iterations = 10,
        startupMode = StartupMode.COLD,
        setupBlock = {
            pressHome()
        }
    ) {
        // Waits for the first rendered frame, which represents time to initial display.
        startActivityAndWait()

        // Waits for content to be visible, which represents time to fully drawn.
        device.wait(Until.hasObject(By.res("my-content")), 5_000)
    }
}

다음 스크린샷과 같이 Google Pixel 7에서 실행한 Now in Android 샘플 앱의 결과를 Android 스튜디오에서 직접 확인할 수 있습니다. 결과를 보면 컴파일하지 않은 경우(378.6ms)에 반해 기준 프로필(275.1ms)을 사용할 때 앱 시작이 가장 빠른 것을 알 수 있습니다. 전체 AOT 컴파일은 시스템이 디스크에서 더 큰 파일을 로드해야 하므로 더 오래 걸릴 수 있습니다(393ms).

최초 표시 시간을 보여주는 ColdStartupBenchmark의 결과는 컴파일하지 않은 경우 393ms, 전체 컴파일한 경우 393ms, 기준 프로필을 사용한 경우 275ms임

이전 예에서 StartupTimingMetric으로 캡처된 앱 시작 결과를 보여주며 여기에는 FrameTimingMetric과 같이 고려할 만한 다른 중요한 측정항목도 포함되어 있습니다. 모든 측정항목 유형에 관한 자세한 내용은 측정항목 캡처에서 확인하시기 바랍니다.