Baseline Profiles

2023. 7. 26. 11:08개발/Android

Baseline Profiles란

  • apk에 담기는 클래스와 메소드의 목록
  • 앱이 설치되는 동안 이 목록의 클래스와 메소드는 pre-compile된다. 이 결과물은 실행이 더 빠르다 → 앱 실행 시간과 jank(cpu의 초과 작업시간)감소

Baseline Profiles 만들기

  1. Jetpack Macrobenchmark 라이브러리를 사용해야 한다.
  1. BaselineProfileRule을 사용해서 테스트를 작성한다.
  1. rooted 기기 또는 구글플레이가 설치되지 않은 emulator에서 테스트를 실행한다.
  1. Test Results 패널에서 생성된 profiles 로그를 찾는다. 로그 예)
To copy the profile use:
adb pull "/sdcard/Android/media/com.example.macrobenchmark/additional_test_output/TrivialBaselineProfileBenchmark_startup-baseline-prof.txt"
  1. 이름을 baseline-prof.txt으로 바꿔서 앱모듈의 src/main 디렉토리에 위치시킨다.

BaselineProfileRule 샘플

BaselineProfileRule vs MacrobenchmarkRule

BaselineProfileRule

Baseline profiles 생성을 위한 용도

@OptIn(ExperimentalBaselineProfilesApi::class)
class TrivialBaselineProfileBenchmark {
    // [START baseline_profile_basic]
    @get:Rule
    val baselineProfileRule = BaselineProfileRule()


    @Test
    fun startup() = baselineProfileRule.collectBaselineProfile(
        packageName = TARGET_PACKAGE,
        profileBlock = {
            startActivityAndWait()
            device.waitForIdle()
        }
    )
    // [END baseline_profile_basic]
}

MacrobenchmarkRule

startup, scrolling, transition 등의 성능을 측정하는 용도

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


    @Test
    fun startup() = benchmarkRule.measureRepeated(
        packageName = TARGET_PACKAGE,
        metrics = listOf(StartupTimingMetric()),
        iterations = 5,
        setupBlock = {
            // Press home button before each run to ensure the starting activity isn't visible.
            pressHome()
        }
    ) {
        // starts default launch activity
        startActivityAndWait()
    }
}

Uploaded by N2T

'개발 > Android' 카테고리의 다른 글

알아두면 좋은 Kotlin extension  (0) 2023.07.26
Android Macrobenchmark  (0) 2023.07.26
Fragment 에서 데이터를 주고받는 방법  (0) 2023.07.26
Android Jetpack security  (0) 2023.07.26
Architecture guide - suggested by Google  (0) 2023.07.26