Repository navigation
Migrate Baseline Profiles and Startup Profiles snippets #1097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pengzhi-cb
wants to merge
13
commits into
android:main
Choose a base branch
from
StellarElements:katherinekuan/performance-snippets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a6df157
Migrate Performance (Baseline Profiles, Benchmarking, JankStats, Laun…
kkuan2011 9bfbeda
Update performance file structure for running tests and fix snippet i…
pengzhi-cb 715d37c
Clean up testing dependencies
pengzhi-cb ad76a1c
Move files to performance module
pengzhi-cb c10dc2b
Upgrade androidx-metrics and fix SplitInitializer pathing
pengzhi-cb 036dba9
Remove unused mock helper class
pengzhi-cb 8c450f3
Move classes from misc module to performance
pengzhi-cb 4fc0293
Add new line
pengzhi-cb 163d29f
Remove snippets that are not required atm and reorganize test depende…
pengzhi-cb b3640ea
Replace internal class for JUnit with public
pengzhi-cb 33457fd
Remove unused androidx-metrics-performance dependency
pengzhi-cb 48ab305
Merge branch 'main' into katherinekuan/performance-snippets
erikrodriguez-se 539922c
Merge branch 'main' into katherinekuan/performance-snippets
pengzhi-cb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...oidTest/java/com/example/android/performance/baselineprofiles/BaselineProfilesOverview.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
80 changes: 80 additions & 0 deletions
80
...droidTest/java/com/example/android/performance/baselineprofiles/MeasureBaselineProfile.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
54 changes: 54 additions & 0 deletions
54
...ndroidTest/java/com/example/android/performance/startupprofiles/DexLayoutOptimizations.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.