Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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" }
Expand Down Expand Up @@ -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" }
Expand Down
7 changes: 6 additions & 1 deletion performance/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ android {

dependencies {
implementation(libs.androidx.activity.compose)
}
androidTestImplementation(libs.androidx.benchmark.macro.junit4)
Comment thread
kkuan2011 marked this conversation as resolved.
Resolved
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.androidx.test.runner)
androidTestImplementation(libs.androidx.uiautomator)
}

Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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]
Loading