与 Jetpack Benchmark 库不同,Macrobenchmark 适用于测试较大的用例,而非小块代码。
使用 Macrobenchmark 的示例包括应用启动、运行时性能用例,例如滚动 RecyclerView。
1、Android studio需要使用预览版,下载地址:Android Studio Preview | Android Developers

2、运行设备要在Android Q(Android10.0)及以上

相关库的引入配置参考官网:https://developer.android.com/studio/profile/macrobenchmark
步骤二:定义宏基准测试环境配置完成后,新建一个macrobenchmark的module,用来测试我们的目标工程APP。如下图:在模块macrobenchmark中定义一个新的测试类,并填入要测试应用的软件包名称,

如需执行 activity 启动,您可以向 measureRepeated() 函数传递一种预定义的启动模式(COLD、WARM 或 HOT 中的一种)。此参数会更改 activity 的启动方式,以及测试开始时的进程状态。
类似于测试用例编写,本次测试冷启动的代码如下:
@LargeTest
@RunWith(Parameterized::class)
class SmallListStartupBenchmark(private val startupMode: StartupMode) {
@get:Rule
val benchmarkRule = MacrobenchmarkRule()
@Test
fun startup() = benchmarkRule.measureStartup(
profileCompiled = true,
startupMode = startupMode
) {
action = "com.example.macrobenchmark.target.RECYCLER_VIEW_ACTIVITY"
putExtra("ITEM_COUNT", 5)
}
companion object {
@Parameterized.Parameters(name = "mode={0}")
@JvmStatic
fun parameters(): List {
return listOf(StartupMode.COLD, StartupMode.WARM)
.map { arrayOf(it) }
}
}
}
const val TARGET_PACKAGE = "com.example.macrobenchmark.target"
fun MacrobenchmarkRule.measureStartup(
profileCompiled: Boolean,
startupMode: StartupMode,
iterations: Int = 3,
setupIntent: Intent.() -> Unit = {}
) = measureRepeated(
packageName = TARGET_PACKAGE,
metrics = listOf(StartupTimingMetric()),
compilationMode = if (profileCompiled) {
CompilationMode.SpeedProfile(warmupIterations = 3)
} else {
CompilationMode.None
},
iterations = iterations,
startupMode = startupMode
) {
pressHome()
val intent = Intent()
intent.setPackage(TARGET_PACKAGE)
setupIntent(intent)
startActivityAndWait(intent)
}
可以看到几次启动测试的最快,最慢值

点击Traces:Iteration 0 1 2后可跳转到对应trace:


Test results saved as file:/F:/benchmark/performance-samples/MacrobenchmarkSample/macrobenchmark/build/outputs/androidTest-results/connected/M2011K2C%20-%2011/test-result.pb. Inspect these results in Android Studio by selecting Run > Import Tests From File from the menu bar and importing test-result.pb.
会有相关的日志,trace文件等信息。

JSON文件中记录设备,及测试用例相关信息:
{
"context": {
"build": {
"brand": "Xiaomi",
"device": "venus",
"fingerprint": "Xiaomi/venus/venus:11/RKQ1.200928.002/V12.5.13.0.RKBCNXM:user/release-keys",
"model": "M2011K2C",
"version": {
"sdk": 30
}
},
"cpuCoreCount": 8,
"cpuLocked": false,
"cpuMaxFreqHz": 2841600000,
"memTotalBytes": 7652691968,
"sustainedPerformanceModeEnabled": false
},
"benchmarks": [
{
"name": "startup[mode=COLD]",
"params": {
"mode": "COLD"
},
"className": "com.example.macrobenchmark.SmallListStartupBenchmark",
"totalRunTimeNs": 30471050717,
"metrics": {
"startupMs": {
"minimum": 187.015833,
"maximum": 217.015365,
"median": 208.226459,
"runs": [
208.226459,
217.015365,
187.015833
]
}
},
"sampledMetrics": {},
"warmupIterations": 3,
"repeatIterations": 3,
"thermalThrottleSleepSeconds": 0
},
{
"name": "startup[mode=WARM]",
"params": {
"mode": "WARM"
},
"className": "com.example.macrobenchmark.SmallListStartupBenchmark",
"totalRunTimeNs": 31034628321,
"metrics": {
"startupMs": {
"minimum": 145.8775,
"maximum": 183.948385,
"median": 159.618177,
"runs": [
159.618177,
183.948385,
145.8775
]
}
},
"sampledMetrics": {},
"warmupIterations": 3,
"repeatIterations": 3,
"thermalThrottleSleepSeconds": 0
}
]
}
宏基准测试适用场景类似于自动化测试,我们可以编写执行脚本,获取执行路径的运行时间。
总体来说使用成本较高,也不利于大规模测试,了解相关功能即可,暂不使用。
