Android基準(zhǔn)配置文件Baseline?Profile方案提升啟動(dòng)速度
引言
偶然在Youtube上看到一名國外安卓開發(fā)者分享了一個(gè)提升應(yīng)用性能的視頻,其中使用到了macro benchmark來進(jìn)行性能測量,包括啟動(dòng)速度和列表幀率,方法是生成一個(gè)baseline-prof.txt文件放于app/src/main/下。查閱google的官方文檔,其背后原理如下:
通過在應(yīng)用或庫中分發(fā)基準(zhǔn)配置文件,Android 運(yùn)行時(shí) (ART) 可以通過預(yù)先 (AOT) 編譯來優(yōu)化包含的代碼路徑,從而針對每位新用戶以及每個(gè)應(yīng)用更新提升性能。這種配置文件引導(dǎo)的優(yōu)化 (PGO) 可讓應(yīng)用優(yōu)化啟動(dòng)、減少互動(dòng)卡頓,并提高整體的運(yùn)行時(shí)性能,從而讓用戶從首次啟動(dòng)開始便獲得更好的使用體驗(yàn)。
baseline-prof.txt文件中定義了安裝時(shí)要預(yù)編譯的代碼路徑,打包時(shí)會(huì)跟隨aab一起上傳到Google Play,通過Google play安裝時(shí)將獲得預(yù)編譯的收益。
這個(gè)方案看起來很不錯(cuò),相比于其它的那些難以上手的啟動(dòng)優(yōu)化方案,這個(gè)似乎比較好落地,于是乎我開始了接入嘗試,最后艱難成功了。
測量工具
官方建議使用Jetpack Macrobenchmark來測試應(yīng)用在已啟動(dòng)基準(zhǔn)配置文件時(shí)的性能,然后將這些結(jié)果與已停用基準(zhǔn)配置文件時(shí)的基準(zhǔn)進(jìn)行比較。接入的方式也很簡單,如果你的AS版本滿足要求,F(xiàn)ile/New Module/Benchmark就可以了。

會(huì)在benchmark Module生成一個(gè)ExampleStartupBenchmark測試類,將其修改一下變成如下。
@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)目首頁的布局決定
device.wait(Until.hasObject(By.res("my-content")), 5_000)
}
}
選擇帶有Benchmark后綴的build variant,測試結(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)用通過 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ì)遇到的問題:
有配置多渠道(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才行
生成基準(zhǔn)配置文件
在benchmark模塊處新建一個(gè)測試類:
@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添加支持,之后就可以跑上面新建的測試了,執(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。
驗(yàn)證優(yōu)化效果
萬事俱備,只欠驚喜,驗(yàn)證一下對啟動(dòng)速度有多大提升。
在app模塊添加以下依賴:
dependencies {
implementation("androidx.profileinstaller:profileinstaller:1.3.0-alpha03")
}
連接真機(jī)再次跑ExampleStartupBenchmark測試,在不同機(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ù)可以看出,總體來說有一定的提升,特別是在性能低一點(diǎn)的機(jī)器會(huì)比較明顯,但相比于google官方給的文檔中的示例結(jié)果(提升20%+)還有一點(diǎn)差距,猜測應(yīng)該跟生成的baseline-prof.txt有關(guān),因?yàn)槲疫@里只生成了啟動(dòng)過程到完成第一幀繪制時(shí)的熱點(diǎn)代碼列表,google的例子是生成了到首頁并且切換tab的熱點(diǎn)代碼。
此外,基準(zhǔn)配置文件也可以用在提升首次打開操作流暢性上,原理也是一樣的,只需要在BaselineProfileGenerator處添加首次進(jìn)入之后的一些操作,比如像官方的例子一樣的切換tab、列表滑動(dòng),生成新的文件即可。
以上就是Android基準(zhǔn)配置文件Baseline Profile方案提升啟動(dòng)速度的詳細(xì)內(nèi)容,更多關(guān)于Android Baseline Profile提升啟動(dòng)速度的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android編程實(shí)現(xiàn)RotateAnimation設(shè)置中心點(diǎn)旋轉(zhuǎn)動(dòng)畫效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)RotateAnimation設(shè)置中心點(diǎn)旋轉(zhuǎn)動(dòng)畫效果,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android xml布局及RotateAnimation動(dòng)畫類相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
TextView顯示文本控件兩種方法 TextView顯示link的方法
這篇文章主要為大家詳細(xì)介紹了TextView顯示文本控件兩種方法,TextView顯示link的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android編程之listView中checkbox用法實(shí)例分析
這篇文章主要介紹了Android編程之listView中checkbox用法,結(jié)合實(shí)例形式分析了Android中checkbox的頁面布局及功能實(shí)現(xiàn)相關(guān)技巧,需要的朋友可以參考下2016-01-01
Android入門之RelativeLayout、FrameLayout用法分析
這篇文章主要介紹了Android入門之RelativeLayout、FrameLayout用法分析,需要的朋友可以參考下2014-08-08
Android實(shí)現(xiàn)瘋狂連連看游戲之狀態(tài)數(shù)據(jù)模型(三)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)瘋狂連連看游戲之狀態(tài)數(shù)據(jù)模型,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Eclipse+ADT+Android SDK搭建安卓開發(fā)環(huán)境的實(shí)現(xiàn)步驟
這篇文章主要介紹了Eclipse+ADT+Android SDK搭建安卓開發(fā)環(huán)境的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Android采取BroadcastReceiver方式自動(dòng)獲取驗(yàn)證碼
這篇文章主要介紹了Android采取BroadcastReceiver方式自動(dòng)獲取驗(yàn)證碼,感興趣的小伙伴們可以參考一下2016-08-08
Android 自定義圓形帶刻度漸變色的進(jìn)度條樣式實(shí)例代碼
這篇文章主要介紹了Android 自定義圓形帶刻度漸變色的進(jìn)度條的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android 實(shí)現(xiàn)圓角圖片的簡單實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)圓角圖片的簡單實(shí)例的相關(guān)資料,Android 圓角圖片的實(shí)現(xiàn)形式,包括用第三方、也有系統(tǒng),需要的朋友可以參考下2017-07-07

