设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间13 现在时间13 缓存数据 当初听了胡歌的光棍结果单身到现在,现在听完这首歌我突然害怕了起来[生病]

当初听了胡歌的光棍结果单身到现在,现在听完这首歌我突然害怕了起来[生病] -- 好几年

查看: 1986|回复: 1

Android基准配置文件Baseline Profile方案提升启动速度

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:228
  • 打卡月天数:0
  • 打卡总奖励:3500
  • 最近打卡:2025-04-20 20:26:40
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
462
主题
420
精华
0
金钱
4850
积分
946
注册时间
2023-1-22
最后登录
2025-5-31

发表于 2023-3-2 10:40:04 来自手机 | 显示全部楼层 |阅读模式
引言

偶然在Youtube上看到一名国外安卓开发者分享了一个提升应用性能的视频,其中使用到了macro benchmark来进行性能测量,包括启动速度和列表帧率,方法是生成一个baseline-prof.txt文件放于app/src/main/下。查阅google的官方文档,其背后原理如下:
通过在应用或库中分发基准配置文件,Android 运行时 (ART) 可以通过预先 (AOT) 编译来优化包含的代码路径,从而针对每位新用户以及每个应用更新提升性能。这种配置文件引导的优化 (PGO) 可让应用优化启动、减少互动卡顿,并提高整体的运行时性能,从而让用户从首次启动开始便获得更好的使用体验。
基准配置文件介绍
baseline-prof.txt文件中定义了安装时要预编译的代码路径,打包时会跟随aab一起上传到Google Play,通过Google play安装时将获得预编译的收益。
这个方案看起来很不错,相比于其它的那些难以上手的启动优化方案,这个似乎比较好落地,于是乎我开始了接入尝试,最后艰难成功了。

测量工具

官方建议使用Jetpack Macrobenchmark来测试应用在已启动基准配置文件时的性能,然后将这些结果与已停用基准配置文件时的基准进行比较。接入的方式也很简单,如果你的AS版本满足要求,File/New Module/Benchmark就可以了。
114039ckzsafjbcdczsz77.jpeg

会在benchmark Module生成一个ExampleStartupBenchmark测试类,将其修改一下变成如下。
  1. @RunWith(AndroidJUnit4ClassRunner::class)
  2. class ColdStartupBenchmark {
  3.     @get:Rule
  4.     val benchmarkRule = MacrobenchmarkRule()
  5.     /**
  6.     * 不使用基准配置文件
  7.     */
  8.     @Test
  9.     fun startupNoCompilation() = startup(CompilationMode.None() )
  10.     /**
  11.     * 使用基准配置文件模式
  12.     */
  13.     @Test
  14.     fun startupBaselineProfile() = startup(CompilationMode.Partial())
  15.     @Test
  16.     fun startupFullCompilation() = startup(CompilationMode.Full())
  17.     private fun startup(compilationMode: CompilationMode) = benchmarkRule.measureRepeated(
  18.         packageName = "com.example.macrobenchmark.target",
  19.         metrics = listOf(StartupTimingMetric()),
  20.         compilationMode = compilationMode,
  21.         iterations = 10,
  22.         startupMode = StartupMode.COLD,
  23.         setupBlock = {
  24.             pressHome()
  25.         }
  26.     ) {
  27.         // Waits for the first rendered frame, which represents time to initial display.
  28.         startActivityAndWait()
  29.         // Waits for content to be visible, which represents time to fully drawn.
  30.         //此处可删除,my-content根据自己项目首页的布局决定
  31.         device.wait(Until.hasObject(By.res("my-content")), 5_000)
  32.     }
  33. }
复制代码
选择带有Benchmark后缀的build variant,测试结果如下所示:
  1. ExampleStartupBenchmark_startUpCompilationModePartialtimeToInitialDisplayMs   min 290.7,   median 310.5,   max 391.2Traces: Iteration 0 1 2 3 4
  2. ExampleStartupBenchmark_startUpCompilationModeNonetimeToInitialDisplayMs   min 359.4,   median 381.9,   max 420.6Traces: Iteration 0 1 2 3 4
复制代码
  1. timeToInitialDisplayMs
复制代码
- 从系统收到启动 intent 到渲染目标 activity 的第一帧的时间
  1. timeToFullDisplayMs
复制代码
- 从系统收到启动 intent 到应用通过 reportFullyDrawn 方法报告已完成绘制的时间。这个需要你手动调用activity.reportFullDrawn()才会有结果展示,表示此时已完全绘制。
  1. Trace: Iteration
复制代码
可以看到每次启动的trace记录,点击数字会跳到Profiler分析界面
运行的时候可能会遇到的问题:
有配置多渠道(Flavor),然后提示Run configuration ExampleStartupBenchmark is not supported in the current project.Cannot obtain the package.解决办法是benchmark里的flavor保持跟app模块一致就可以了
aar依赖找不到
  1. Could not determine the dependencies of null.  
  2.     Could not resolve all task dependencies for configuration':benchmark:flavorDemoBenchmarkTestedApks'.  
  3.         Could not find :your_aar_name_in_testModule_libs:.  
  4.            Required by:  
  5.                project :benchmark > project :app > project :testModule
复制代码
解决方案:在benchmark模块的build.gradle中添加
  1. repositories {
  2.     flatDir {
  3.         dirs '../testModule/libs', '../app/libs'
  4.     }
  5. }
复制代码
Unable to read any metrics during benchmark因为benchmark模块中的benchmark buildtype中debuggable要设为true才行
官方文档

生成基准配置文件

在benchmark模块处新建一个测试类:
  1. @ExperimentalBaselineProfilesApi
  2. @RunWith(AndroidJUnit4::class)
  3. class BaselineProfileGenerator {
  4.     @get:Rule val baselineProfileRule = BaselineProfileRule()
  5.     @Test
  6.     fun startup() =
  7.         baselineProfileRule.collectBaselineProfile(packageName = "com.example.app") {
  8.             pressHome()
  9.             // This block defines the app's critical user journey. Here we are interested in
  10.             // optimizing for app startup. But you can also navigate and scroll
  11.             // through your most important UI.
  12.             startActivityAndWait()
  13.         }
  14. }
复制代码
新建一个Android9以上版本模拟器(真机不行),注意系统选择不包含Google Api的,执行adb root命令,修改ndk filter添加支持,之后就可以跑上面新建的测试了,执行完成之后基准配置文件会生成于
  1. 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。
官方文档

验证优化效果

万事俱备,只欠惊喜,验证一下对启动速度有多大提升。
在app模块添加以下依赖:
  1. dependencies {
  2.      implementation("androidx.profileinstaller:profileinstaller:1.3.0-alpha03")
  3. }
复制代码
连接真机再次跑ExampleStartupBenchmark测试,在不同机型分别得到的结果为:
Pixel 1: android 10
  1. ExampleStartupBenchmark_compilationPartial  timeToInitialDisplayMs   min 1,359.2,   median 1,422.4,   max 2,583.0  
  2. ExampleStartupBenchmark_compilationNone  timeToInitialDisplayMs   min 1,454.1,   median 1,556.7,   max 2,610.3 
复制代码
三星S20: android 13
  1. ExampleStartupBenchmark_compilationPartialtimeToInitialDisplayMs   min 597.2,   median 683.9,   max 763.4
  2. ExampleStartupBenchmark_compilationNonetimeToInitialDisplayMs   min 699.5,   median 726.1,   max 753.5
复制代码
三星S8+: android7
  1. ExampleStartupBenchmark_compilationPartial  timeToInitialDisplayMs   min 1,089.1,   median 1,121.6,   max 1,249.4 
  2. ExampleStartupBenchmark_compilationNone  timeToInitialDisplayMs   min 1,147.5,   median 1,166.2,   max 1,338.2
复制代码
观察数据可以看出,总体来说有一定的提升,特别是在性能低一点的机器会比较明显,但相比于google官方给的文档中的示例结果(提升20%+)还有一点差距,猜测应该跟生成的baseline-prof.txt有关,因为我这里只生成了启动过程到完成第一帧绘制时的热点代码列表,google的例子是生成了到首页并且切换tab的热点代码。
此外,基准配置文件也可以用在提升首次打开操作流畅性上,原理也是一样的,只需要在BaselineProfileGenerator处添加首次进入之后的一些操作,比如像官方的例子一样的切换tab、列表滑动,生成新的文件即可。
以上就是Android基准配置文件Baseline Profile方案提升启动速度的详细内容,更多关于Android Baseline Profile提升启动速度的资料请关注晓枫资讯其它相关文章!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

  • 打卡等级:无名新人
  • 打卡总天数:1
  • 打卡月天数:0
  • 打卡总奖励:13
  • 最近打卡:2024-07-11 07:45:16
等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
27
积分
8
注册时间
2023-5-17
最后登录
2024-7-11

发表于 2023-7-9 09:25:43 | 显示全部楼层
感谢分享~~~~学习学习~~~~~
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表