使用以下插桩参数配置 Microbenchmark 的行为。您可以将这些参数添加到 Gradle 配置中,也可以在从命令行运行插桩时直接应用这些参数。如需为所有 Android Studio 和命令行测试运行设置这些参数,请将它们添加到 testInstrumentationRunnerArguments
android {
defaultConfig {
// ...
testInstrumentationRunnerArguments["androidx.benchmark.dryRunMode.enable"] = "true"
}
}
您还可以在从 Android Studio 运行基准测试时设置插桩参数。如需更改参数,请执行以下操作:
- 点击修改并选择要修改的配置来修改运行配置。
图 1. 修改运行配置。 - 点击 Instrumentation arguments(插桩参数)字段旁边的
图 2. 修改插桩参数。
以修改插桩参数。 - 点击
图 3. 添加插桩参数。
并添加所需的插桩参数。
如果您从命令行运行基准测试,请使用 -P android.testInstrumentationRunnerArguments.[name of the argument]
./gradlew :benchmark:connectedAndroidTest -P android.testInstrumentationRunnerArguments.androidx.benchmark.profiling.mode=StackSampling
如果您直接调用 am instrument 命令(CI 测试环境可能如此),请通过 -e
将参数传递给 am instrument
adb shell am instrument -e androidx.benchmark.profiling.mode StackSampling -w com.example.macrobenchmark/androidx.benchmark.junit4.AndroidBenchmarkRunner
如需详细了解如何在 CI 中配置基准测试,请参阅在 CI 中进行基准测试
additionalTestOutputDir
配置 JSON 基准测试报告和性能分析结果在设备上的保存位置。
- 参数类型:文件路径字符串
- 默认值:测试 APK 的外部目录
androidx.benchmark.cpuEventCounter.enable (实验性)
统计 androidx.benchmark.cupEventCounter.events
中指定的 CPU 事件。需要 root 权限。
- 参数类型:布尔值
- 默认值:false
androidx.benchmark.cpuEventCounter.events (实验性)
指定要统计的 CPU 事件类型。要使用此参数,必须将 androidx.benchmark.cpuEventCounter.enable
设置为 true
。
- 参数类型:逗号分隔的字符串列表
- 可用选项
Instructions
CPUCycles
L1DReferences
L1DMisses
BranchInstructions
BranchMisses
L1IReferences
L1IMisses
- 默认值:
Instructions
、CpuCycles
、BranchMisses
androidx.benchmark.dryRunMode.enable
允许您在单个循环中运行基准测试,以验证它们是否正常工作。
这意味着:
- 配置错误不会强制执行(例如,为了更轻松地使用模拟器上的常规正确性测试运行)
- 基准测试仅运行一个循环,没有预热
- 不捕获测量结果和跟踪以减少运行时
这优化了测试吞吐量和基准测试逻辑验证,而不是构建和测量正确性。
- 参数类型:布尔值
- 默认值:
false
androidx.benchmark.iterations
覆盖时间驱动的目标迭代次数,以帮助确保一致的工作量。这通常仅在启用性能分析时有用,以帮助确保在比较不同的实现或运行情况时,性能分析跟踪中执行的工作量保持一致。在其他情况下,这可能会降低测量的准确性或稳定性。
- 参数类型:整数
- 默认值:未指定
androidx.benchmark.junit4.SideEffectRunListener
如果在基准测试运行时执行了不相关的后台工作,您可能会得到不一致的基准测试结果。
要在基准测试期间停用后台工作,请将 listener
插桩参数类型设置为 androidx.benchmark.junit4.SideEffectRunListener
。
- 参数类型:字符串
- 可用选项
androidx.benchmark.junit4.SideEffectRunListener
- 默认值:未指定
androidx.benchmark.output.enable
启用将结果 JSON 文件写入外部存储。
- 参数类型:布尔值
- 默认值:
true
androidx.benchmark.profiling.mode
允许在运行基准测试时捕获跟踪文件。如需了解可用选项,请参阅分析微基准测试。
请注意,某些 Android 操作系统版本不支持方法跟踪,且不会影响后续测量。为防止出现这种情况,Microbenchmark 会抛出异常,因此仅在安全时才使用默认参数捕获方法跟踪。请参阅 问题 #316174880。
- 参数类型:字符串
- 可用选项
MethodTracing
StackSampling
None
- 默认值:
MethodTracing
的安全版本,仅在设备能够在不影响测量的情况下捕获方法跟踪时才捕获该跟踪。
androidx.benchmark.suppressErrors
接受逗号分隔的错误列表,将其转换为警告。
- 参数类型:字符串列表
- 可用选项
DEBUGGABLE
LOW-BATTERY
EMULATOR
CODE-COVERAGE
UNLOCKED
SIMPLEPERF
ACTIVITY-MISSING
- 默认值:空列表
为您推荐
- 注意:当 JavaScript 关闭时,会显示链接文本
- 宏基准测试插桩参数
- 分析微基准测试
- 创建基准配置文件 {:#creating-profile-rules}