[BaseLine Profile] App 성능 개선

김태현·2023년 2월 27일
1

Android

목록 보기
3/3
post-thumbnail

BaseLine Profile 이란?

Android 런타임(ART) 이 설치 중에 중요한 경로를 기계어 코드로 사전 컴파일하는 데 사용하는 APK에 포함된 클래스 및 메서드 목록입니다. BaseLine Profile은 앱의 시작을 최적화하고, 버벅거림을 줄이고, 최종 사용자가 경험하는 성능을 개선할 수 있도록 합니다.

얼마나 개선되었는지 어떻게 알아?

Macrobenchmark ??

Android M(API 23) 이상을 실행하는 기기의 앱과 관련된 시작 및 런타임 성능 테스트를 직접 작성할 수 있습니다.

Macrobenchmark 시작

  1. 모듈 추가

  1. Benchmark 생성

  1. 생성된 benchmark 모듈에 ExampleStartupBenchmark class에 내용 작성
@RunWith(AndroidJUnit4::class)
class ExampleStartupBenchmark{
@get:Rule
    val benchmarkRule = MacrobenchmarkRule()

@Test
fun startup()= benchmarkRule.measureRepeated(
				packageName = "co.mk.app", //패키지 네임
        metrics =listOf(StartupTimingMetric()), // 앱 부팅 완료 옵션
        iterations = 3, // 반복 횟수
        startupMode = StartupMode.COLD // 부팅 종류 cold, warm, hot
    ){
pressHome()
startActivityAndWait() // 앱이 로드되는 순간
device.wait(Until.hasObject(By.text("What are you interested in?")), 30_000) // 앱이 완전히 로드 된 후

//        val recycler = device.findObject(By.res("mypackage.myapp", "recycler_id"))
//        recycler.setGestureMargin(device.displayWidth / 5)
//
//        // Scroll down several times
//        for (i in 1..10) {
//            recycler.scroll(Direction.DOWN, 2f)
//            device.waitForIdle()
//        }
   }
}




💡 Test 실행 시 Json 파일로 확인도 가능하다.

BaseLine Profile 생성 및 테스트

  1. 난독화 되지 않도록 별도 proguard 작성
**<--build.gradle.kts-->**

create("benchmark"){
signingConfig = signingConfigs.getByName("debug")
matchingFallbacks +=listOf("release")
isDebuggable = false
    proguardFiles("baseline-profiles-rules.pro")
}
  1. BaselineProfileGenerator 작성

@OptIn(ExperimentalBaselineProfilesApi::class)
@RunWith(AndroidJUnit4ClassRunner::class)
class BaselineProfileGenerator{

@get:Rule
    val rule = BaselineProfileRule()

@Test
    fun generate()= rule.collectBaselineProfile(
"co.mk.app",
){
	startActivityAndWait() // 앱이 로드되는 순간
	device.wait(Until.hasObject(By.text("What are you interested in?")), 30_000) // 앱이 완전히 로드 된 후
	}
}
  1. 테스트를 위한 루팅 기기 셋팅
**<--benchmark/build.gradle.kts-->**

testOptions{
managedDevices{
devices{
create("pixel2Api31", ManagedVirtualDevice::class){
device = "Pixel 2"
                apiLevel = 31
                systemImageSource = "aosp"
						}
        }
    }
}
  1. Terminal 을 통한 테스트 실행
💡 ./gradlew :benchmark:pixel2Api31BenchmarkAndroidTest -P android.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile
  1. 생성된 baseline-profile 파일을 app/main으로 복사

파일 이름을 baseline-prof로 변경

static.com/859cd9dd-bfc7-4b49-bf76-e37a3f91491f/Untitled.png)

  1. baseline-profile 파일로 인해 얼마나 성능이 개선되었는지 확인
@RunWith(AndroidJUnit4::class)
class ExampleStartupBenchmark{
@get:Rule
    val benchmarkRule = MacrobenchmarkRule()

@Test
    fun startupCompilationNone()= startup(CompilationMode.None())
// 완전한 AOT

@Test
    fun startupCompilationPartial()= startup(CompilationMode.Partial())
// 부분적인 AOT

fun startup(compilationMode: CompilationMode)= benchmarkRule.measureRepeated(
packageName = "co.mk.app",
        metrics =listOf(StartupTimingMetric()),
        iterations = 3,
        compilationMode = compilationMode,
        startupMode = StartupMode.COLD
    ){
pressHome()
startActivityAndWait()

device.wait(Until.hasObject(By.text("What are you interested in?")), 30_000)
		}
}




1. Run 결과 창을 통한 내용 확인

업로드중..

profile
Android 개발자

0개의 댓글