diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6cb8bc0ed..651ea330b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -6,6 +6,7 @@ androidGradlePlugin = "9.3.1" androidx-activity-compose = "1.13.0" androidx-appcompat = "1.7.1" androidx-appfunctions = "1.0.0-alpha10" +androidx-benchmark = "1.4.1" androidx-cameraX = "1.6.1" androidx-car = "1.7.0" androidx-compose-bom = "2026.09.00" @@ -36,6 +37,7 @@ androidx-test = "1.7.0" androidx-test-core-ktx = "1.7.0" androidx-test-espresso = "3.7.0" androidx-test-junit = "1.3.0" +androidx-uiautomator = "2.4.0" androidx-window = "1.6.0-alpha05" androidx-window-core = "1.5.1" androidx-window-java = "1.5.1" @@ -149,6 +151,7 @@ androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" } androidx-appfunctions = { module = "androidx.appfunctions:appfunctions", version.ref = "androidx-appfunctions" } androidx-appfunctions-compiler = { module = "androidx.appfunctions:appfunctions-compiler", version.ref = "androidx-appfunctions" } +androidx-benchmark-macro-junit4 = { module = "androidx.benchmark:benchmark-macro-junit4", version.ref = "androidx-benchmark" } androidx-camera-camera2 = { module = "androidx.camera:camera-camera2", version.ref = "androidx-cameraX" } androidx-camera-compose = { module = "androidx.camera:camera-compose", version.ref = "androidx-cameraX" } androidx-camera-core = { module = "androidx.camera:camera-core", version.ref = "androidx-cameraX" } @@ -289,6 +292,7 @@ androidx-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version androidx-tiles-tooling = { module = "androidx.wear.tiles:tiles-tooling", version.ref = "tiles" } androidx-tiles-tooling-preview = { module = "androidx.wear.tiles:tiles-tooling-preview", version.ref = "tiles" } androidx-tracing = { module = "androidx.tracing:tracing", version.ref = "tracing" } +androidx-uiautomator = { module = "androidx.test.uiautomator:uiautomator", version.ref = "androidx-uiautomator" } androidx-watchface-complications-data-source-ktx = { module = "androidx.wear.watchface:watchface-complications-data-source-ktx", version.ref = "watchfaceComplicationsDataSourceKtx" } androidx-watchfacepush = { module = "androidx.wear.watchfacepush:watchfacepush", version.ref = "wfp" } androidx-wear = { module = "androidx.wear:wear", version.ref = "wear" } diff --git a/performance/build.gradle.kts b/performance/build.gradle.kts index 64f15da1b..2bd7d9b21 100644 --- a/performance/build.gradle.kts +++ b/performance/build.gradle.kts @@ -32,4 +32,9 @@ android { dependencies { implementation(libs.androidx.activity.compose) -} \ No newline at end of file + androidTestImplementation(libs.androidx.benchmark.macro.junit4) + androidTestImplementation(libs.androidx.test.ext.junit) + androidTestImplementation(libs.androidx.test.runner) + androidTestImplementation(libs.androidx.uiautomator) +} + diff --git a/performance/src/androidTest/java/com/example/android/performance/baselineprofiles/BaselineProfilesOverview.kt b/performance/src/androidTest/java/com/example/android/performance/baselineprofiles/BaselineProfilesOverview.kt new file mode 100644 index 000000000..b713319e1 --- /dev/null +++ b/performance/src/androidTest/java/com/example/android/performance/baselineprofiles/BaselineProfilesOverview.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.performance.baselineprofiles + +import androidx.benchmark.macro.junit4.BaselineProfileRule +import androidx.test.uiautomator.Direction +import androidx.test.uiautomator.textAsString +import androidx.test.uiautomator.uiAutomator +import org.junit.Rule +import org.junit.Test + +private const val PACKAGE_NAME = "com.example.snippets" + +// [START android_performance_baselineprofiles_overview_generator] +class BaselineProfileGenerator { + @get:Rule + val baselineProfileRule = BaselineProfileRule() + + @Test + fun appStartupAndUserJourneys() { + baselineProfileRule.collect(packageName = PACKAGE_NAME) { + uiAutomator { + // App startup journey. + startApp(PACKAGE_NAME) + + // Find and click elements using the new DSL + onElement { textAsString() == "COMPOSE LAZYLIST" }.click() + onElement { viewIdResourceName == "myLazyColumn" }.also { + it.fling(Direction.DOWN) + it.fling(Direction.UP) + } + pressBack() + } + } + } +} +// [END android_performance_baselineprofiles_overview_generator] diff --git a/performance/src/androidTest/java/com/example/android/performance/baselineprofiles/MeasureBaselineProfile.kt b/performance/src/androidTest/java/com/example/android/performance/baselineprofiles/MeasureBaselineProfile.kt new file mode 100644 index 000000000..e16c87f43 --- /dev/null +++ b/performance/src/androidTest/java/com/example/android/performance/baselineprofiles/MeasureBaselineProfile.kt @@ -0,0 +1,80 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.performance.baselineprofiles + +import androidx.benchmark.macro.BaselineProfileMode +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 androidx.test.uiautomator.uiAutomator +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +// [START android_performance_baselineprofiles_measure_compilation_modes] +@RunWith(AndroidJUnit4::class) +class ColdStartupBenchmark { + @get:Rule + val benchmarkRule = MacrobenchmarkRule() + + // No ahead-of-time (AOT) compilation at all. Represents performance of a + // fresh install on a user's device if you don't enable Baseline Profiles— + // generally the worst case performance. + @Test + fun startupNoCompilation() = startup(CompilationMode.None()) + + // Partial pre-compilation with Baseline Profiles. Represents performance of + // a fresh install on a user's device. + @Test + fun startupPartialWithBaselineProfiles() = + startup(CompilationMode.Partial(baselineProfileMode = BaselineProfileMode.Require)) + + // Partial pre-compilation with some just-in-time (JIT) compilation. + // Represents performance after some app usage. + @Test + fun startupPartialCompilation() = startup( + CompilationMode.Partial( + baselineProfileMode = BaselineProfileMode.Disable, + warmupIterations = 3 + ) + ) + + // Full pre-compilation. Generally not representative of real user + // experience, but can yield more stable performance metrics by removing + // noise from JIT compilation within benchmark runs. + @Test + fun startupFullCompilation() = startup(CompilationMode.Full()) + + private fun startup(compilationMode: CompilationMode) = benchmarkRule.measureRepeated( + packageName = "com.example.macrobenchmark.target", + metrics = listOf(StartupTimingMetric()), + compilationMode = compilationMode, + iterations = 10, + startupMode = StartupMode.COLD, + setupBlock = { + pressHome() + } + ) { + uiAutomator { + startApp(packageName) + onElement(5_000) { viewIdResourceName == "my-content" } + } + } +} +// [END android_performance_baselineprofiles_measure_compilation_modes] diff --git a/performance/src/androidTest/java/com/example/android/performance/startupprofiles/DexLayoutOptimizations.kt b/performance/src/androidTest/java/com/example/android/performance/startupprofiles/DexLayoutOptimizations.kt new file mode 100644 index 000000000..740ad3003 --- /dev/null +++ b/performance/src/androidTest/java/com/example/android/performance/startupprofiles/DexLayoutOptimizations.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.performance.startupprofiles + +import android.content.Intent +import androidx.benchmark.macro.junit4.BaselineProfileRule +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.LargeTest +import androidx.test.uiautomator.uiAutomator +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +// [START android_performance_startupprofiles_generator] +@RunWith(AndroidJUnit4::class) +@LargeTest +class BaselineProfileGenerator { + + @get:Rule + val rule = BaselineProfileRule() + + @Test + fun generate() { + rule.collect( + packageName = "com.example.app", + includeInStartupProfile = true + ) { + uiAutomator { + // Launch directly into the NEWS_FEED using startActivityIntent + startIntent( + Intent().apply { + setPackage(packageName) + setAction("com.example.app.NEWS_FEED") + } + ) + } + } + } +} +// [END android_performance_startupprofiles_generator]