Android基准配置文件Baseline Profile方案提升启动速度
作者:linversion 发布时间:2022-08-12 18:54:42
引言
偶然在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就可以了。
会在benchmark Module生成一个ExampleStartupBenchmark测试类,将其修改一下变成如下。
@RunWith(AndroidJUnit4ClassRunner::class)
class ColdStartupBenchmark {
@get:Rule
val benchmarkRule = MacrobenchmarkRule()
/**
* 不使用基准配置文件
*/
@Test
fun startupNoCompilation() = startup(CompilationMode.None() )
/**
* 使用基准配置文件模式
*/
@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根据自己项目首页的布局决定
device.wait(Until.hasObject(By.res("my-content")), 5_000)
}
}
选择带有Benchmark后缀的build variant,测试结果如下所示:
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
- 从系统收到启动 intent 到渲染目标 activity 的第一帧的时间
timeToFullDisplayMs
- 从系统收到启动 intent 到应用通过 reportFullyDrawn 方法报告已完成绘制的时间。这个需要你手动调用activity.reportFullDrawn()才会有结果展示,表示此时已完全绘制。
Trace: Iteration
可以看到每次启动的trace记录,点击数字会跳到Profiler分析界面
运行的时候可能会遇到的问题:
有配置多渠道(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因为benchmark模块中的benchmark buildtype中debuggable要设为true才行
官方文档
生成基准配置文件
在benchmark模块处新建一个测试类:
@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()
}
}
新建一个Android9以上版本模拟器(真机不行),注意系统选择不包含Google Api的,执行adb root命令,修改ndk filter添加支持,之后就可以跑上面新建的测试了,执行完成之后基准配置文件会生成于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模块添加以下依赖:
dependencies {
implementation("androidx.profileinstaller:profileinstaller:1.3.0-alpha03")
}
连接真机再次跑ExampleStartupBenchmark测试,在不同机型分别得到的结果为:
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
观察数据可以看出,总体来说有一定的提升,特别是在性能低一点的机器会比较明显,但相比于google官方给的文档中的示例结果(提升20%+)还有一点差距,猜测应该跟生成的baseline-prof.txt有关,因为我这里只生成了启动过程到完成第一帧绘制时的热点代码列表,google的例子是生成了到首页并且切换tab的热点代码。
此外,基准配置文件也可以用在提升首次打开操作流畅性上,原理也是一样的,只需要在BaselineProfileGenerator处添加首次进入之后的一些操作,比如像官方的例子一样的切换tab、列表滑动,生成新的文件即可。
来源:https://juejin.cn/post/7195535228230975547
猜你喜欢
- JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用。一、icepdf。有收费版和开源版,几种方
- 前言这几天琢磨着开发个个人作品的时候,发现原来Unity3D官方没有提供圆锥体的创建功能,就自己做了个编辑器扩展。鉴于之前搜索Mesh编程的
- 1.瞬时流量过高,服务被压垮?2.恶意用户高频光顾,导致服务器宕机?3.消息消费过快,导致数据库压力过大,性能下降甚至崩溃?......在高
- 一、bean实例化——构造方法(常用)bean本质上就是对象,创建bean使用构造方法完成BookD
- 双向顺序队列ArrayDeque和双向链式队列LinkedList,JDK已经包含,在此略。ArrayDeque包括顺序栈和顺序队列,Lin
- 1 pom.xml文件注:热部署功能spring-boot-1.3开始有的<!--添加依赖--><dependency&g
- 本文实例为大家分享了Java实现简单员工管理系统的具体代码,供大家参考,具体内容如下代码如下:import java.util.*;publ
- 目录前言一、技术介绍1.Minio是什么?二、使用步骤1.引入maven库2.封装Minio3.配置文件4.单元测试总结前言使用Spring
- 使用unity制作瞄准镜,供大家参考,具体内容如下一、创建场景在Hierarchy窗口中使用Plane和Cube创建场景,并调整摄像机位置二
- 一、连接客户端原理流程图二、功能要求1)连接成功后,可以将服务器发来的消息不停地显示在 listbox 中;2) 客户端要发给服务器的数据,
- 问题遇到问题:在前后端分离跨域访问的项目中shiro进行权限拦截失效 (即使有正确权限的访问也会被拦截) 时造成302重定向错误等问题报错:
- 本文以实例形式详细讲述了Java的反射机制,是Java程序设计中重要的技巧。分享给大家供大家参考。具体分析如下:首先,Reflection是
- Java多线程深入理解本文主要从三个方面了解和掌握多线程:1. 多线程的实现方式,通过继承Thread类和通过实现Runnable接口的方式
- MyBatis框架提供了二级缓存接口,我们只需要实现它再开启配置就可以使用了。特别注意,我们要解决缓存穿透、缓存穿透和缓存雪崩的问题,同时也
- 本文实例讲述了C#使用GDI+创建缩略图的方法,分享给大家供大家参考。具体方法分析如下:C#的Gdi+还是相当好用的。创建缩略图步骤如下:1
- Spring AOP对嵌套方法不起作用今天在调研系统操作记录日志时,好多教程都是借助于Spring AOP机制来实现。于是也采用这种方法来实
- 本文实例为大家分享了C#请求http向网页发送数据、网页接收,供大家参考,具体内容如下首先,我们需要的是什么东西?用POST方式请求http
- 生命太短暂,不要去做一些根本没有人想要的东西。本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术
- 数组实现Java 自定义Queue队列及应用Java 自定义队列Queue:队列的抽象数据类型就是一个容器,其中的对象排成一个序列,我们只能
- 一、项目简述功能包括: 登录注册,办理借阅。借阅记录,预约借阅,借出未还, 借阅逾期,学生管理,图书管理,书库分类查询搜索。二、项目运行环境