代码优化

This commit is contained in:
HapeLee
2026-05-14 15:37:26 +08:00
parent 4d3ae1e537
commit 7fda394d74
12 changed files with 301 additions and 147 deletions
+2 -1
View File
@@ -21,7 +21,8 @@ android {
targetSdk = 37
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "DEBUGGABLE"
}
targetProjectPath = ":app"
@@ -30,36 +30,41 @@ class BaselineProfileGenerator {
packageName = packageName,
includeInStartupProfile = true
) {
// 1. 启动应用
pressHome()
startActivityAndWait()
// 2. 等待书架加载 (我们在 BookshelfScreen 中添加了 ReportDrawnWhen)
// 等待书架列表 testTag 出现
device.wait(Until.hasObject(By.res(packageName, "bookshelf_list")), 10000)
val bookshelfSelector = By.desc("bookshelf_list")
device.wait(Until.hasObject(bookshelfSelector), 10000)
// 3. 模拟滑动书架 (优化列表渲染性能)
val bookshelf = device.findObject(By.res(packageName, "bookshelf_list"))
bookshelf?.apply {
setGestureMargin(device.displayWidth / 10)
fling(Direction.DOWN)
device.waitForIdle()
fling(Direction.UP)
device.waitForIdle()
device.waitForIdle()
Thread.sleep(2000)
val screenWidth = device.displayWidth
val screenHeight = device.displayHeight
val startX = screenWidth / 2
val startY = (screenHeight * 0.75).toInt()
val endY = (screenHeight * 0.25).toInt()
repeat(3) {
device.swipe(startX, startY, startX, endY, 25)
Thread.sleep(1000)
}
// 4. 遍历主要 Tab (优化 Compose 导航和各页面初始化)
val tabs = listOf("nav_explore", "nav_rss", "nav_my", "nav_bookshelf")
for (tabTag in tabs) {
val tab = device.findObject(By.res(packageName, tabTag))
repeat(2) {
device.swipe(startX, endY, startX, startY, 10)
Thread.sleep(1000)
}
// "nav_my"
/* val tabs = listOf("nav_explore", "nav_rss", "nav_bookshelf")
for (tabDesc in tabs) {
val tab = device.wait(Until.findObject(By.desc(tabDesc)), 5000)
if (tab != null) {
tab.click()
device.waitForIdle()
// 稍微等待页面加载
Thread.sleep(500)
val bounds = tab.visibleBounds
device.click(bounds.centerX(), bounds.centerY())
Thread.sleep(3000)
}
}
}*/
}
}
}
@@ -44,21 +44,19 @@ class StartupBenchmarks {
val rule = MacrobenchmarkRule()
@Test
fun startupCompilationNone() =
benchmark(CompilationMode.None())
fun startupCompilationNone() = benchmark(CompilationMode.None())
@Test
fun startupCompilationBaselineProfiles() =
benchmark(CompilationMode.Partial(BaselineProfileMode.Require))
private fun benchmark(compilationMode: CompilationMode) {
// The application id for the running build variant is read from the instrumentation arguments.
val packageName = InstrumentationRegistry.getArguments().getString("targetAppId")
?: "io.legado.app"
rule.measureRepeated(
packageName = packageName,
metrics = listOf(StartupTimingMetric(), FrameTimingMetric()),
metrics = listOf(StartupTimingMetric()),
compilationMode = compilationMode,
startupMode = StartupMode.COLD,
iterations = 3,
@@ -67,18 +65,6 @@ class StartupBenchmarks {
},
measureBlock = {
startActivityAndWait()
// 等待书架列表加载完成
// 我们在 BookshelfScreen 中添加了 testTag("bookshelf_list")
device.wait(Until.hasObject(By.res(packageName, "bookshelf_list")), 10000)
// 模拟交互:在书架上进行滑动,以确保 Profile 捕获到列表渲染和图片加载的路径
val bookshelfList = device.findObject(By.res(packageName, "bookshelf_list"))
bookshelfList?.apply {
setGestureMargin(device.displayWidth / 10)
fling(Direction.DOWN)
device.waitForIdle()
}
}
)
}