偶然在Youtube上看到一名國(guó)外安卓開(kāi)發(fā)者分享了一個(gè)提升應(yīng)用性能的視頻,其中使用到了macro benchmark來(lái)進(jìn)行性能測(cè)量,包括啟動(dòng)速度和列表幀率,方法是生成一個(gè)baseline-prof.txt文件放于app/src/main/下。查閱google的官方文檔,其背后原理如下:
通過(guò)在應(yīng)用或庫(kù)中分發(fā)基準(zhǔn)配置文件,Android 運(yùn)行時(shí) (ART) 可以通過(guò)預(yù)先 (AOT) 編譯來(lái)優(yōu)化包含的代碼路徑,從而針對(duì)每位新用戶以及每個(gè)應(yīng)用更新提升性能。這種配置文件引導(dǎo)的優(yōu)化 (PGO) 可讓?xiě)?yīng)用優(yōu)化啟動(dòng)、減少互動(dòng)卡頓,并提高整體的運(yùn)行時(shí)性能,從而讓用戶從首次啟動(dòng)開(kāi)始便獲得更好的使用體驗(yàn)。
基準(zhǔn)配置文件介紹
【資料圖】
baseline-prof.txt文件中定義了安裝時(shí)要預(yù)編譯的代碼路徑,打包時(shí)會(huì)跟隨aab一起上傳到Google Play,通過(guò)Google play安裝時(shí)將獲得預(yù)編譯的收益。
這個(gè)方案看起來(lái)很不錯(cuò),相比于其它的那些難以上手的啟動(dòng)優(yōu)化方案,這個(gè)似乎比較好落地,于是乎我開(kāi)始了接入嘗試,最后艱難成功了。
官方建議使用Jetpack Macrobenchmark來(lái)測(cè)試應(yīng)用在已啟動(dòng)基準(zhǔn)配置文件時(shí)的性能,然后將這些結(jié)果與已停用基準(zhǔn)配置文件時(shí)的基準(zhǔn)進(jìn)行比較。接入的方式也很簡(jiǎn)單,如果你的AS版本滿足要求,F(xiàn)ile/New Module/Benchmark就可以了。
會(huì)在benchmark Module生成一個(gè)ExampleStartupBenchmark測(cè)試類,將其修改一下變成如下。
@RunWith(AndroidJUnit4ClassRunner::class)
class ColdStartupBenchmark {
@get:Rule
val benchmarkRule = MacrobenchmarkRule()
/**
* 不使用基準(zhǔn)配置文件
*/
@Test
fun startupNoCompilation() = startup(CompilationMode.None() )
/**
* 使用基準(zhǔn)配置文件模式
*/
@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.
//此處可刪除,my-content根據(jù)自己項(xiàng)目首頁(yè)的布局決定
device.wait(Until.hasObject(By.res("my-content")), 5_000)
}
}
選擇帶有Benchmark后綴的build variant,測(cè)試結(jié)果如下所示:
ExampleStartupBenchmark_startUpCompilationModePartial
timeToInitialDisplayMs min 290.7, median 310.5, max 391.2
Traces: Iteration 0 1 2 3 4ExampleStartupBenchmark_startUpCompilationModeNone
timeToInitialDisplayMs min 359.4, median 381.9, max 420.6
Traces: Iteration 0 1 2 3 4
timeToInitialDisplayMs- 從系統(tǒng)收到啟動(dòng) intent 到渲染目標(biāo) activity 的第一幀的時(shí)間
timeToFullDisplayMs- 從系統(tǒng)收到啟動(dòng) intent 到應(yīng)用通過(guò)reportFullyDrawn方法報(bào)告已完成繪制的時(shí)間。這個(gè)需要你手動(dòng)調(diào)用activity.reportFullDrawn()才會(huì)有結(jié)果展示,表示此時(shí)已完全繪制。
Trace: Iteration可以看到每次啟動(dòng)的trace記錄,點(diǎn)擊數(shù)字會(huì)跳到Profiler分析界面
運(yùn)行的時(shí)候可能會(huì)遇到的問(wèn)題:
有配置多渠道(Flavor),然后提示Run configuration ExampleStartupBenchmark is not supported in the current project.Cannot obtain the package.解決辦法是benchmark里的flavor保持跟app模塊一致就可以了
aar依賴找不到
Could not determine the dependencies of null.
Could not resolve all task dependencies for configuration":benchmark:flavorDemoBenchmarkTestedApks".
Could not find :your_aar_name_in_testModule_libs:.
Required by:
project :benchmark > project :app > project :testModule
解決方案:在benchmark模塊的build.gradle中添加
repositories {
flatDir {
dirs "../testModule/libs", "../app/libs"
}
}
Unable to read any metrics during benchmark因?yàn)閎enchmark模塊中的benchmark buildtype中debuggable要設(shè)為true才行
官方文檔
在benchmark模塊處新建一個(gè)測(cè)試類:
@ExperimentalBaselineProfilesApi
@RunWith(AndroidJUnit4::class)
class BaselineProfileGenerator {
@get:Rule val baselineProfileRule = BaselineProfileRule()
@Test
fun startup() =
baselineProfileRule.collectBaselineProfile(packageName = "com.example.app") {
pressHome()
// This block defines the app"s critical user journey. Here we are interested in
// optimizing for app startup. But you can also navigate and scroll
// through your most important UI.
startActivityAndWait()
}
}
新建一個(gè)Android9以上版本模擬器(真機(jī)不行),注意系統(tǒng)選擇不包含Google Api的,執(zhí)行adb root命令,修改ndk filter添加支持,之后就可以跑上面新建的測(cè)試了,執(zhí)行完成之后基準(zhǔn)配置文件會(huì)生成于benchmark/build/outputs/connected_android_test_additional_output/flavorDemoBenchmark/Pixel 2處,名字類似于BaselineProfileGenerator_generateBaselineProfile-baseline-prof-2023-01-30-07-29-28.txt,將之拷貝到app/src/main/目錄下,重命名為baseline-prof.txt。
官方文檔
萬(wàn)事俱備,只欠驚喜,驗(yàn)證一下對(duì)啟動(dòng)速度有多大提升。
在app模塊添加以下依賴:
dependencies {
implementation("androidx.profileinstaller:profileinstaller:1.3.0-alpha03")
}
連接真機(jī)再次跑ExampleStartupBenchmark測(cè)試,在不同機(jī)型分別得到的結(jié)果為:
Pixel 1: android 10
ExampleStartupBenchmark_compilationPartial
timeToInitialDisplayMs min 1,359.2, median 1,422.4, max 2,583.0ExampleStartupBenchmark_compilationNone
timeToInitialDisplayMs min 1,454.1, median 1,556.7, max 2,610.3
三星S20: android 13
ExampleStartupBenchmark_compilationPartial
timeToInitialDisplayMs min 597.2, median 683.9, max 763.4ExampleStartupBenchmark_compilationNone
timeToInitialDisplayMs min 699.5, median 726.1, max 753.5
三星S8+: android7
ExampleStartupBenchmark_compilationPartial
timeToInitialDisplayMs min 1,089.1, median 1,121.6, max 1,249.4ExampleStartupBenchmark_compilationNone
timeToInitialDisplayMs min 1,147.5, median 1,166.2, max 1,338.2
觀察數(shù)據(jù)可以看出,總體來(lái)說(shuō)有一定的提升,特別是在性能低一點(diǎn)的機(jī)器會(huì)比較明顯,但相比于google官方給的文檔中的示例結(jié)果(提升20%+)還有一點(diǎn)差距,猜測(cè)應(yīng)該跟生成的baseline-prof.txt有關(guān),因?yàn)槲疫@里只生成了啟動(dòng)過(guò)程到完成第一幀繪制時(shí)的熱點(diǎn)代碼列表,google的例子是生成了到首頁(yè)并且切換tab的熱點(diǎn)代碼。
此外,基準(zhǔn)配置文件也可以用在提升首次打開(kāi)操作流暢性上,原理也是一樣的,只需要在BaselineProfileGenerator處添加首次進(jìn)入之后的一些操作,比如像官方的例子一樣的切換tab、列表滑動(dòng),生成新的文件即可。
以上就是Android基準(zhǔn)配置文件Baseline Profile方案提升啟動(dòng)速度的詳細(xì)內(nèi)容,更多關(guān)于Android Baseline Profile提升啟動(dòng)速度的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!