您好,登錄后才能下訂單哦!
這篇“怎么使用Android基準(zhǔn)配置文件Baseline Profile方案提升啟動(dòng)速度”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“怎么使用Android基準(zhǔn)配置文件Baseline Profile方案提升啟動(dòng)速度”文章吧。
官方建議使用Jetpack Macrobenchmark來(lái)測(cè)試應(yīng)用在已啟動(dòng)基準(zhǔn)配置文件時(shí)的性能,然后將這些結(jié)果與已停用基準(zhǔn)配置文件時(shí)的基準(zhǔn)進(jìn)行比較。接入的方式也很簡(jiǎn)單,如果你的AS版本滿(mǎn)足要求,F(xiàn)ile/New Module/Benchmark就可以了。
會(huì)在benchmark Module生成一個(gè)ExampleStartupBenchmark測(cè)試類(lèi),將其修改一下變成如下。
@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依賴(lài)找不到
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è)試類(lèi):
@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
處,名字類(lèi)似于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模塊添加以下依賴(lài):
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),生成新的文件即可。
以上就是關(guān)于“怎么使用Android基準(zhǔn)配置文件Baseline Profile方案提升啟動(dòng)速度”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。