From 66be0be3c4b49c689a817d2591558c6517571431 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 15 Jun 2026 17:18:55 +0800 Subject: [PATCH] test(benchmark): add StartupBenchmark for cold launch timing --- .../java/plus/rua/project/baseline/AGENTS.md | 11 ++-- .../rua/project/baseline/StartupBenchmark.kt | 54 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 macrobenchmark/src/main/java/plus/rua/project/baseline/StartupBenchmark.kt diff --git a/macrobenchmark/src/main/java/plus/rua/project/baseline/AGENTS.md b/macrobenchmark/src/main/java/plus/rua/project/baseline/AGENTS.md index aaa39cd..b135a52 100644 --- a/macrobenchmark/src/main/java/plus/rua/project/baseline/AGENTS.md +++ b/macrobenchmark/src/main/java/plus/rua/project/baseline/AGENTS.md @@ -1,16 +1,19 @@ - + # baseline ## Purpose -Baseline Profile 自动生成器。通过 UI Automator 模拟完整用户交互路径(冷启动、FAB 展开、年/月视图切换、日期选择、折叠/展开、关于页、开源许可页),生成 AOT 编译优化所需的 startup profile。 +Baseline Profile 自动生成器与启动性能基准测试。包含: +- `BaselineProfileGenerator.kt`:通过 UI Automator 模拟完整用户交互路径(冷启动、FAB 展开、年/月视图切换、日期选择、折叠/展开、关于页、开源许可页),生成 AOT 编译优化所需的 startup profile。 +- `StartupBenchmark.kt`:冷启动性能基准测试,测量 `timeToInitialDisplay` 与 `timeToFullDisplay`。 ## Key Files | File | Description | |------|-------------| | `BaselineProfileGenerator.kt` | Profile 生成测试类,覆盖全部用户交互路径 | +| `StartupBenchmark.kt` | 冷启动基准测试类,覆盖 Full/Partial/None 三种编译模式 | ## Subdirectories 无 @@ -23,13 +26,15 @@ Baseline Profile 自动生成器。通过 UI Automator 模拟完整用户交互 - 每个操作后调用 `device.waitForIdle()` 等待动画完成 ### Testing Requirements -- 运行:`./gradlew :macrobenchmark:updateBaselineProfile` +- 生成 Profile:`./gradlew :macrobenchmark:updateBaselineProfile` +- 运行冷启动基准(仅该类):`./gradlew :macrobenchmark:connectedBenchmarkAndroidTest --tests "plus.rua.project.baseline.StartupBenchmark"` - 需要设备/模拟器连接,应用已安装在 benchmark 构建类型下 ## Dependencies ### External - `androidx.benchmark.macro.junit4.BaselineProfileRule` +- `androidx.benchmark.macro.junit4.MacrobenchmarkRule` - `androidx.test.uiautomator` diff --git a/macrobenchmark/src/main/java/plus/rua/project/baseline/StartupBenchmark.kt b/macrobenchmark/src/main/java/plus/rua/project/baseline/StartupBenchmark.kt new file mode 100644 index 0000000..5b5490b --- /dev/null +++ b/macrobenchmark/src/main/java/plus/rua/project/baseline/StartupBenchmark.kt @@ -0,0 +1,54 @@ +package plus.rua.project.baseline + +import androidx.benchmark.macro.CompilationMode +import androidx.benchmark.macro.StartupMode +import androidx.benchmark.macro.StartupTimingMetric +import androidx.benchmark.macro.junit4.MacrobenchmarkRule +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * 冷启动性能基准测试。 + * + * 测量指标: + * - timeToInitialDisplay:从 intent 到首帧显示 + * - timeToFullDisplay:从 intent 到 reportFullyDrawn()/ReportDrawn() 被触发 + * + * 运行方式: + * ``` + * ./gradlew :macrobenchmark:connectedBenchmarkAndroidTest --tests "plus.rua.project.baseline.StartupBenchmark" + * ``` + */ +@RunWith(AndroidJUnit4::class) +class StartupBenchmark { + + @get:Rule + val benchmarkRule = MacrobenchmarkRule() + + @Test + fun startupColdFull() = startup(CompilationMode.Full()) + + @Test + fun startupColdPartial() = startup(CompilationMode.Partial()) + + @Test + fun startupColdNone() = startup(CompilationMode.None()) + + private fun startup(compilationMode: CompilationMode) { + benchmarkRule.measureRepeated( + packageName = "plus.rua.project", + metrics = listOf(StartupTimingMetric()), + compilationMode = compilationMode, + startupMode = StartupMode.COLD, + iterations = 5, + setupBlock = { + pressHome() + }, + measureBlock = { + startActivityAndWait() + } + ) + } +}