我配置了一个基准测试模块并为项目创建了一个基线配置文件,并根据此处的说明将其保存在应用程序中https://developer.android.com/studio/profile/baselineprofiles。
我还添加了基准:
@RunWith(AndroidJUnit4::class)
class BaselineProfileBenchmark {
@get:Rule
val benchmarkRule = MacrobenchmarkRule()
@Test
fun startupNoCompilation() {
startup(CompilationMode.None())
}
@Test
fun startupBaselineProfile() {
startup(
CompilationMode.Partial(
baselineProfileMode = BaselineProfileMode.Require
)
)
}
private fun startup(compilationMode: CompilationMode) {
benchmarkRule.measureRepeated(
packageName = "com.example.app",
metrics = listOf(StartupTimingMetric()),
iterations = 10,
startupMode = StartupMode.COLD,
compilationMode = compilationMode
) {
pressHome()
startActivityAndWait()
}
}
}
Run Code Online (Sandbox Code Playgroud)
startupBaselineProfile()测试失败,并出现异常“此设备版本不支持基线配置文件”。我尝试在具有最新 Android 版本的不同 Pixel 和 Samsung 设备上运行它,但到处都会抛出相同的异常。
这些基线配置文件真的有效吗?
我一直在尝试使用 Android 的 Baseline Profile 和 MacroBenchmark 库来提高应用程序性能。
我的问题是,建议MacroBenchmark 的构建类型尽可能接近构建release,但符合-dontobfuscateproguard 规则。
如果生产应用程序将通过obfuscated,minification那么 Baseline Profiler 如何帮助提高应用程序性能,因为baseline-prof.txt应用程序classes.dex文件中的类可能完全不同。
Profiler 和 Proguard/R8 之间是否存在某种内部映射?
我的应用程序整体性能相当不错,唯一 FPS 下降的部分是在伴奏者滑动 HorizontalPager 时。
每个页面都有一个简单的 LazyVerticalGrid,有 3 个固定列,但与 XML 相比,性能要差得多。
LazyVerticalGrid 的外观如下:
出于测试目的,我在 XML 中开发了相同的设置,以使用 Perfetto 和 Android Studio 编译器进行比较。与 XML 相比,Jetpack Compose 版本具有更多的 Janky 框架。
这是 Perfetto 中 compose 的结果:
这里是 XML:
此外,每次我滑动 HorizontalPager 时,Profiler 都会显示一堆卡顿的帧:
我拥有 Jetpack Compose 的基本知识,并且还阅读了 Android 开发人员 Compose 性能指南,我非常确定我没有做任何导致大量重组的愚蠢事情。
这是我在测试项目中使用的代码:
可滚动 TabRow + HorizontalPager:
val tabList = listOf("Following", "Trending", "New", "Top", "Most Shared", "Most Saved", "All")
val pagerState: PagerState = rememberPagerState(initialPage = 1)
val coroutineScope = rememberCoroutineScope()
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background) …Run Code Online (Sandbox Code Playgroud) android android-jetpack android-jetpack-compose jetpack-compose-accompanist baseline-profile
我正在尝试为我的应用程序实现基线配置文件(官方文档)。按照本视频(通过基线配置文件提高性能)中指示的步骤,如果我使用DebugBuildVariant,我可以创建benchmark-prof.txt文件以缩短启动时间。但是当我尝试创建用于发布的基线-prof 文件时,出现以下异常:
./gradlew :macrobenchmark:pixel2Api31BenchmarkAndroidTest -P android.testInstrumentationRunnerArguments.class=com.example.BaselineProfileGenerator
Run Code Online (Sandbox Code Playgroud)
com.example.BaselineProfileGenerator > generate[pixel2Api31] FAILED
java.lang.IllegalStateException: Unable to confirm activity launch completion [] Please report a bug with the output of `adb shell dumpsys gfxinfo com.leinardi.forlago framestats`
at androidx.benchmark.macro.MacrobenchmarkScope.startActivityImpl(MacrobenchmarkScope.kt:179)
Tests on pixel2Api31 failed: There was 1 failure(s).
Run Code Online (Sandbox Code Playgroud)
这就是我们的BaselineProfileGenerator的样子:
./gradlew :macrobenchmark:pixel2Api31BenchmarkAndroidTest -P android.testInstrumentationRunnerArguments.class=com.example.BaselineProfileGenerator
Run Code Online (Sandbox Code Playgroud)
如果我配置调试基准,它会很好地工作:
benchmark {
initWith buildTypes.debug
signingConfig signingConfigs.debug
matchingFallbacks = ['debug']
debuggable false
proguardFiles('benchmark-rules.pro')
}
Run Code Online (Sandbox Code Playgroud)
但是将其配置为Release,当您尝试生成它时,基准测试会引发之前的异常。
benchmark {
initWith buildTypes.release …Run Code Online (Sandbox Code Playgroud) 我正在将基线配置文件应用到我的应用程序中以提高性能,并且我按照 android 页面上的此链接https://developer.android.com/topic/performance/baselineprofiles
而且效果很好。但是当我添加productFlavors时它运行失败。
消费者被配置为查找组件的运行时,以及值为“benchmark”的属性“com.android.build.api.attributes.BuildTypeAttr”,属性“com.android.build.api.attributes.AgpVersionAttr”值“7.1.1”。但是,我们无法在项目的以下变体之间进行选择:app: - devBenchmarkRuntimeElements - liveBenchmarkRuntimeElements
有谁知道如何处理这种情况?
下面是完整的错误详细信息和 build.gradle 文件。
Could not determine the dependencies of task
':benchmark:connectedBenchmarkAndroidTest'.
> Could not determine the dependencies of null.
> Could not resolve all task dependencies for configuration
':benchmark:benchmarkTestedApks'.
> Could not resolve project :app.
Required by:
project :benchmark
> The consumer was configured to find a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark', attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1'. However we cannot choose between …Run Code Online (Sandbox Code Playgroud) 为了使用Macrobenchmark,我们需要添加<profileable>到 AndroidManifest.
<profileable
android:shell="true"
tools:targetApi="q" />
Run Code Online (Sandbox Code Playgroud)
包含<profileable>在生产版本中安全吗?
我创建了一个基准测试模块,并在其 build.gradle 文件中添加了一个托管设备,如下所示:
testOptions {
managedDevices {
devices {
pixel2api30 (com.android.build.api.dsl.ManagedVirtualDevice) {
device = "Pixel 2"
apiLevel = 30
systemImageSource = "aosp"
require64Bit = false
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
每当我运行命令时gradle :benchmark:pixel2api30Setup它就会工作
但是当我运行命令时gradle :benchmark:pixel2api30BenchmarkAndroidTest
它抛出以下错误:
Task 'pixel2api30BenchmarkAndroidTest' not found in project ':benchmark'.
Run Code Online (Sandbox Code Playgroud) android ×7
performance ×3
android-r8 ×1
androidx ×1
gradle ×1
jetpack-compose-accompanist ×1
proguard ×1