代码优化
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import com.android.build.api.dsl.ManagedVirtualDevice
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.android.test)
|
||||
alias(libs.plugins.baselineprofile)
|
||||
@@ -20,7 +18,7 @@ android {
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 28
|
||||
targetSdk = 36
|
||||
targetSdk = 37
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
@@ -31,24 +29,13 @@ android {
|
||||
productFlavors {
|
||||
create("app") { dimension = "mode" }
|
||||
}
|
||||
|
||||
// This code creates the gradle managed device used to generate baseline profiles.
|
||||
// To use GMD please invoke generation through the command line:
|
||||
// ./gradlew :app:generateBaselineProfile
|
||||
testOptions.managedDevices.allDevices {
|
||||
create<ManagedVirtualDevice>("pixel6Api34") {
|
||||
device = "Pixel 6"
|
||||
apiLevel = 34
|
||||
systemImageSource = "google"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This is the configuration block for the Baseline Profile plugin.
|
||||
// You can specify to run the generators on a managed devices or connected devices.
|
||||
baselineProfile {
|
||||
managedDevices += "pixel6Api34"
|
||||
useConnectedDevices = false
|
||||
// managedDevices += "pixel6Api34"
|
||||
useConnectedDevices = true
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@@ -59,6 +46,11 @@ dependencies {
|
||||
}
|
||||
|
||||
androidComponents {
|
||||
beforeVariants { v ->
|
||||
if (v.buildType == "noR8" || v.buildType == "debug") {
|
||||
v.enable = false
|
||||
}
|
||||
}
|
||||
onVariants { v ->
|
||||
val artifactsLoader = v.artifacts.getBuiltArtifactsLoader()
|
||||
v.instrumentationRunnerArguments.put(
|
||||
|
||||
+33
-36
@@ -4,32 +4,15 @@ import androidx.benchmark.macro.junit4.BaselineProfileRule
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.filters.LargeTest
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.uiautomator.By
|
||||
import androidx.test.uiautomator.Direction
|
||||
import androidx.test.uiautomator.Until
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
/**
|
||||
* This test class generates a basic startup baseline profile for the target package.
|
||||
*
|
||||
* We recommend you start with this but add important user flows to the profile to improve their performance.
|
||||
* Refer to the [baseline profile documentation](https://d.android.com/topic/performance/baselineprofiles)
|
||||
* for more information.
|
||||
*
|
||||
* You can run the generator with the "Generate Baseline Profile" run configuration in Android Studio or
|
||||
* the equivalent `generateBaselineProfile` gradle task:
|
||||
* ```
|
||||
* ./gradlew :app:generateReleaseBaselineProfile
|
||||
* ```
|
||||
* The run configuration runs the Gradle task and applies filtering to run only the generators.
|
||||
*
|
||||
* Check [documentation](https://d.android.com/topic/performance/benchmarking/macrobenchmark-instrumentation-args)
|
||||
* for more information about available instrumentation arguments.
|
||||
*
|
||||
* After you run the generator, you can verify the improvements running the [StartupBenchmarks] benchmark.
|
||||
*
|
||||
* When using this class to generate a baseline profile, only API 33+ or rooted API 28+ are supported.
|
||||
*
|
||||
* The minimum required version of androidx.benchmark to generate a baseline profile is 1.2.0.
|
||||
**/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
@LargeTest
|
||||
@@ -40,29 +23,43 @@ class BaselineProfileGenerator {
|
||||
|
||||
@Test
|
||||
fun generate() {
|
||||
// The application id for the running build variant is read from the instrumentation arguments.
|
||||
rule.collect(
|
||||
packageName = InstrumentationRegistry.getArguments().getString("targetAppId")
|
||||
?: throw Exception("targetAppId not passed as instrumentation runner arg"),
|
||||
val packageName = InstrumentationRegistry.getArguments().getString("targetAppId")
|
||||
?: "io.legado.app"
|
||||
|
||||
// See: https://d.android.com/topic/performance/baselineprofiles/dex-layout-optimizations
|
||||
rule.collect(
|
||||
packageName = packageName,
|
||||
includeInStartupProfile = true
|
||||
) {
|
||||
// This block defines the app's critical user journey. Here we are interested in
|
||||
// optimizing for app startup. But you can also navigate and scroll through your most important UI.
|
||||
|
||||
// Start default activity for your app
|
||||
// 1. 启动应用
|
||||
pressHome()
|
||||
startActivityAndWait()
|
||||
|
||||
// TODO Write more interactions to optimize advanced journeys of your app.
|
||||
// For example:
|
||||
// 1. Wait until the content is asynchronously loaded
|
||||
// 2. Scroll the feed content
|
||||
// 3. Navigate to detail screen
|
||||
// 2. 等待书架加载 (我们在 BookshelfScreen 中添加了 ReportDrawnWhen)
|
||||
// 等待书架列表 testTag 出现
|
||||
device.wait(Until.hasObject(By.res(packageName, "bookshelf_list")), 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()
|
||||
}
|
||||
|
||||
// 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))
|
||||
if (tab != null) {
|
||||
tab.click()
|
||||
device.waitForIdle()
|
||||
// 稍微等待页面加载
|
||||
Thread.sleep(500)
|
||||
}
|
||||
}
|
||||
|
||||
// Check UiAutomator documentation for more information how to interact with the app.
|
||||
// https://d.android.com/training/testing/other-components/ui-automator
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,14 @@ import androidx.benchmark.macro.BaselineProfileMode
|
||||
import androidx.benchmark.macro.CompilationMode
|
||||
import androidx.benchmark.macro.StartupMode
|
||||
import androidx.benchmark.macro.StartupTimingMetric
|
||||
import androidx.benchmark.macro.FrameTimingMetric
|
||||
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.filters.LargeTest
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.uiautomator.By
|
||||
import androidx.test.uiautomator.Direction
|
||||
import androidx.test.uiautomator.Until
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
@@ -49,27 +53,32 @@ class StartupBenchmarks {
|
||||
|
||||
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 = InstrumentationRegistry.getArguments().getString("targetAppId")
|
||||
?: throw Exception("targetAppId not passed as instrumentation runner arg"),
|
||||
metrics = listOf(StartupTimingMetric()),
|
||||
packageName = packageName,
|
||||
metrics = listOf(StartupTimingMetric(), FrameTimingMetric()),
|
||||
compilationMode = compilationMode,
|
||||
startupMode = StartupMode.COLD,
|
||||
iterations = 10,
|
||||
iterations = 3,
|
||||
setupBlock = {
|
||||
pressHome()
|
||||
},
|
||||
measureBlock = {
|
||||
startActivityAndWait()
|
||||
|
||||
// TODO Add interactions to wait for when your app is fully drawn.
|
||||
// The app is fully drawn when Activity.reportFullyDrawn is called.
|
||||
// For Jetpack Compose, you can use ReportDrawn, ReportDrawnWhen and ReportDrawnAfter
|
||||
// from the AndroidX Activity library.
|
||||
// 等待书架列表加载完成
|
||||
// 我们在 BookshelfScreen 中添加了 testTag("bookshelf_list")
|
||||
device.wait(Until.hasObject(By.res(packageName, "bookshelf_list")), 10000)
|
||||
|
||||
// Check the UiAutomator documentation for more information on how to
|
||||
// interact with the app.
|
||||
// https://d.android.com/training/testing/other-components/ui-automator
|
||||
// 模拟交互:在书架上进行滑动,以确保 Profile 捕获到列表渲染和图片加载的路径
|
||||
val bookshelfList = device.findObject(By.res(packageName, "bookshelf_list"))
|
||||
bookshelfList?.apply {
|
||||
setGestureMargin(device.displayWidth / 10)
|
||||
fling(Direction.DOWN)
|
||||
device.waitForIdle()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user