-
Notifications
You must be signed in to change notification settings - Fork 5.5k
WIP performance skills testing #1700
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
riggaroo
wants to merge
4
commits into
main
Choose a base branch
from
performance-testing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- | ||
| ~ 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. | ||
| --> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
| <!-- Required for macrobenchmark to write trace files on some API levels --> | ||
| <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||
| <application> | ||
| <profileable android:shell="true" /> | ||
| </application> | ||
| </manifest> |
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,8 @@ import androidx.compose.foundation.layout.width | |
| import androidx.compose.foundation.layout.wrapContentSize | ||
| import androidx.compose.foundation.lazy.LazyColumn | ||
| import androidx.compose.foundation.lazy.LazyListState | ||
| import androidx.compose.foundation.lazy.LazyRow | ||
| import androidx.compose.foundation.lazy.items | ||
| import androidx.compose.foundation.lazy.rememberLazyListState | ||
| import androidx.compose.foundation.rememberScrollState | ||
| import androidx.compose.foundation.text.KeyboardActions | ||
|
|
@@ -168,17 +170,33 @@ fun HomeScreen( | |
| // UiState of the HomeScreen | ||
| val uiState by homeViewModel.uiState.collectAsStateWithLifecycle() | ||
|
|
||
| val onToggleFavorite = remember(homeViewModel) { | ||
| { postId: String -> homeViewModel.toggleFavourite(postId) } | ||
| } | ||
| val onSelectPost = remember(navigateToPost) { | ||
| { postId: String -> navigateToPost(postId) } | ||
| } | ||
| val onRefreshPosts = remember(homeViewModel) { | ||
| { homeViewModel.refreshPosts() } | ||
| } | ||
| val onErrorDismiss = remember(homeViewModel) { | ||
| { errorId: Long -> homeViewModel.errorShown(errorId) } | ||
| } | ||
| val onSearchInputChanged = remember(homeViewModel) { | ||
| { searchInput: String -> homeViewModel.onSearchInputChanged(searchInput) } | ||
| } | ||
|
|
||
| HomeFeedScreen( | ||
| uiState = uiState, | ||
| showTopAppBar = !isExpandedScreen, | ||
| onToggleFavorite = { homeViewModel.toggleFavourite(it) }, | ||
| onSelectPost = { navigateToPost(it) }, | ||
| onRefreshPosts = { homeViewModel.refreshPosts() }, | ||
| onErrorDismiss = { homeViewModel.errorShown(it) }, | ||
| onToggleFavorite = onToggleFavorite, | ||
| onSelectPost = onSelectPost, | ||
| onRefreshPosts = onRefreshPosts, | ||
| onErrorDismiss = onErrorDismiss, | ||
| openDrawer = openDrawer, | ||
| homeListLazyListState = rememberLazyListState(), | ||
| snackbarHostState = snackbarHostState, | ||
| onSearchInputChanged = { homeViewModel.onSearchInputChanged(it) }, | ||
| onSearchInputChanged = onSearchInputChanged, | ||
| searchInput = uiState.searchInput, | ||
| ) | ||
| } | ||
|
|
@@ -422,13 +440,14 @@ private fun PostList( | |
| } | ||
| item { PostListTopSection(postsFeed.highlightedPost, onPostTapped) } | ||
| if (postsFeed.recommendedPosts.isNotEmpty()) { | ||
| item { | ||
| PostListSimpleSection( | ||
| postsFeed.recommendedPosts, | ||
| onPostTapped, | ||
| favorites, | ||
| onToggleFavorite, | ||
| items(postsFeed.recommendedPosts) { post -> | ||
| PostCardSimple( | ||
| post = post, | ||
| navigateToPost = onPostTapped, | ||
| isFavorite = favorites.contains(post.id), | ||
| onToggleFavorite = { onToggleFavorite(post.id) } | ||
| ) | ||
| PostListDivider() | ||
| } | ||
| } | ||
| if (postsFeed.popularPosts.isNotEmpty() && !showExpandedSearch) { | ||
|
|
@@ -439,7 +458,10 @@ private fun PostList( | |
| } | ||
| } | ||
| if (postsFeed.recentPosts.isNotEmpty()) { | ||
| item { PostListHistorySection(postsFeed.recentPosts, onPostTapped) } | ||
| items(postsFeed.recentPosts) { post -> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| PostCardHistory(post, onPostTapped) | ||
| PostListDivider() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -478,33 +500,7 @@ private fun PostListTopSection(post: Post, navigateToPost: (String) -> Unit) { | |
| PostListDivider() | ||
| } | ||
|
|
||
| /** | ||
| * Full-width list items for [PostList] | ||
| * | ||
| * @param posts (state) to display | ||
| * @param navigateToPost (event) request navigation to Post screen | ||
| * @param favorites (state) a set of favorite posts | ||
| * @param onToggleFavorite (event) request that this post toggle its favorite state | ||
| */ | ||
| @Composable | ||
| private fun PostListSimpleSection( | ||
| posts: List<Post>, | ||
| navigateToPost: (String) -> Unit, | ||
| favorites: Set<String>, | ||
| onToggleFavorite: (String) -> Unit, | ||
| ) { | ||
| Column { | ||
| posts.forEach { post -> | ||
| PostCardSimple( | ||
| post = post, | ||
| navigateToPost = navigateToPost, | ||
| isFavorite = favorites.contains(post.id), | ||
| onToggleFavorite = { onToggleFavorite(post.id) }, | ||
| ) | ||
| PostListDivider() | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Horizontal scrolling cards for [PostList] | ||
|
|
@@ -520,14 +516,13 @@ private fun PostListPopularSection(posts: List<Post>, navigateToPost: (String) - | |
| text = stringResource(id = R.string.home_popular_section_title), | ||
| style = MaterialTheme.typography.titleLarge, | ||
| ) | ||
| Row( | ||
| LazyRow( | ||
| modifier = Modifier | ||
| .horizontalScroll(rememberScrollState()) | ||
| .height(IntrinsicSize.Max) | ||
| .padding(horizontal = 16.dp), | ||
| horizontalArrangement = Arrangement.spacedBy(8.dp), | ||
| ) { | ||
| for (post in posts) { | ||
| items(posts) { post -> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| PostCardPopular( | ||
| post, | ||
| navigateToPost, | ||
|
|
@@ -539,21 +534,7 @@ private fun PostListPopularSection(posts: List<Post>, navigateToPost: (String) - | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Full-width list items that display "based on your history" for [PostList] | ||
| * | ||
| * @param posts (state) to display | ||
| * @param navigateToPost (event) request navigation to Post screen | ||
| */ | ||
| @Composable | ||
| private fun PostListHistorySection(posts: List<Post>, navigateToPost: (String) -> Unit) { | ||
| Column { | ||
| posts.forEach { post -> | ||
| PostCardHistory(post, navigateToPost) | ||
| PostListDivider() | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Full-width divider with padding for [PostList] | ||
|
|
||
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,48 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| plugins { | ||
| alias(libs.plugins.android.test) | ||
| } | ||
|
|
||
| android { | ||
| compileSdk = libs.versions.compileSdk.get().toInt() | ||
| namespace = "com.example.jetnews.benchmark" | ||
|
|
||
| defaultConfig { | ||
| minSdk = 23 | ||
| targetSdk = libs.versions.targetSdk.get().toInt() | ||
| testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
| } | ||
|
|
||
| buildTypes { | ||
| // This benchmark build type is matching the type in the target application | ||
| create("benchmark") { | ||
| signingConfig = signingConfigs.getByName("debug") | ||
| matchingFallbacks.add("release") | ||
| } | ||
| } | ||
|
|
||
| targetProjectPath = ":app" | ||
| experimentalProperties["android.experimental.self-instrumenting"] = true | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(libs.androidx.test.rules) | ||
| implementation(libs.androidx.test.ext.junit) | ||
| implementation(libs.androidx.test.uiautomator) | ||
| implementation(libs.androidx.benchmark.macro.junit4) | ||
| } |
70 changes: 70 additions & 0 deletions
70
JetNews/benchmark/src/main/java/com/example/jetnews/benchmark/ExampleStartupBenchmark.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,70 @@ | ||
| /* | ||
| * 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.jetnews.benchmark | ||
|
|
||
| import android.content.Intent | ||
| 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.platform.app.InstrumentationRegistry | ||
| import androidx.test.uiautomator.By | ||
| import androidx.test.uiautomator.Until | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class ExampleStartupBenchmark { | ||
| @get:Rule | ||
| val benchmarkRule = MacrobenchmarkRule() | ||
|
|
||
| private val targetPackageName = "com.example.jetnews" | ||
|
|
||
| @Test | ||
| fun startupCold() = benchmarkRule.measureRepeated( | ||
| packageName = targetPackageName, | ||
| metrics = listOf(StartupTimingMetric()), | ||
| compilationMode = CompilationMode.None(), | ||
| startupMode = StartupMode.COLD, | ||
| iterations = 5 | ||
| ) { | ||
| pressHome() | ||
| val context = InstrumentationRegistry.getInstrumentation().context | ||
| val intent = context.packageManager.getLaunchIntentForPackage(packageName)!! | ||
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) | ||
| context.startActivity(intent) | ||
| device.wait(Until.hasObject(By.pkg(packageName).depth(0)), 15000) | ||
| } | ||
|
|
||
| @Test | ||
| fun startupHot() = benchmarkRule.measureRepeated( | ||
| packageName = targetPackageName, | ||
| metrics = listOf(StartupTimingMetric()), | ||
| compilationMode = CompilationMode.None(), | ||
| startupMode = StartupMode.HOT, | ||
| iterations = 5 | ||
| ) { | ||
| pressHome() | ||
| val context = InstrumentationRegistry.getInstrumentation().context | ||
| val intent = context.packageManager.getLaunchIntentForPackage(packageName)!! | ||
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) | ||
| context.startActivity(intent) | ||
| device.wait(Until.hasObject(By.pkg(packageName).depth(0)), 15000) | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
LazyColumn, it is highly recommended to specify a uniquekeyfor items. This helps Jetpack Compose maintain the scroll position and state of items when the list changes (e.g., when a post is favorited or the list is refreshed), preventing unnecessary recompositions and improving scroll performance.