Merge "Add a pager of lazy grids benchmark" into androidx-main
diff --git a/compose/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml b/compose/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml
index bda7255..cccbd34 100644
--- a/compose/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml
+++ b/compose/integration-tests/macrobenchmark-target/src/main/AndroidManifest.xml
@@ -241,7 +241,8 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
- <activity
+
+ <activity
android:name=".VectorsListActivity"
android:label="Compose vectors list"
android:exported="true">
@@ -265,6 +266,14 @@
</intent-filter>
<intent-filter>
<action android:name="androidx.compose.integration.macrobenchmark.target.CROSSFADE_ACTIVITY" />
+ </intent-filter>
+ </activity>
+
+ <activity android:name=".PagerOfLazyGridActivity"
+ android:exported="true"
+ android:theme="@style/Theme.AppCompat">
+ <intent-filter>
+ <action android:name="androidx.compose.integration.macrobenchmark.target.PAGER_LAZYGRID_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
diff --git a/compose/integration-tests/macrobenchmark-target/src/main/java/androidx/compose/integration/macrobenchmark/target/PagerOfLazyGridActivity.kt b/compose/integration-tests/macrobenchmark-target/src/main/java/androidx/compose/integration/macrobenchmark/target/PagerOfLazyGridActivity.kt
new file mode 100644
index 0000000..a18a1be
--- /dev/null
+++ b/compose/integration-tests/macrobenchmark-target/src/main/java/androidx/compose/integration/macrobenchmark/target/PagerOfLazyGridActivity.kt
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2023 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
+ *
+ * http://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 androidx.compose.integration.macrobenchmark.target
+
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.compose.foundation.ExperimentalFoundationApi
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.lazy.grid.GridCells
+import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
+import androidx.compose.foundation.pager.HorizontalPager
+import androidx.compose.foundation.pager.PagerState
+import androidx.compose.foundation.pager.rememberPagerState
+import androidx.compose.material.Button
+import androidx.compose.material.MaterialTheme
+import androidx.compose.material.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.semantics.contentDescription
+import androidx.compose.ui.semantics.semantics
+import kotlinx.coroutines.launch
+
+class PagerOfLazyGridActivity : ComponentActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ val pageCount = intent.getIntExtra(PageCount, 100)
+ val gridItemCount = intent.getIntExtra(GridItemCount, 100)
+
+ setContent {
+ MaterialTheme {
+ HorizontalPagerOfLazyGrid(pageCount, gridItemCount)
+ }
+ }
+
+ launchIdlenessTracking()
+ }
+
+ companion object {
+ const val PageCount = "PAGE_COUNT"
+ const val GridItemCount = "GRID_ITEM_COUNT"
+ }
+}
+
+@OptIn(ExperimentalFoundationApi::class)
+@Composable
+private fun HorizontalPagerOfLazyGrid(pages: Int = 100, gridItems: Int = 100) {
+ val pagerState: PagerState = rememberPagerState(initialPage = 1) { pages }
+ val coroutineScope = rememberCoroutineScope()
+
+ Column(
+ modifier = Modifier
+ .fillMaxSize()
+ .background(MaterialTheme.colors.background)
+ ) {
+ Button(onClick = {
+ coroutineScope.launch {
+ pagerState.animateScrollToPage(pagerState.currentPage + 1)
+ }
+ }) {
+ Text("Next")
+ }
+
+ HorizontalPager(
+ state = pagerState,
+ modifier = Modifier.semantics { contentDescription = "Pager" }
+ ) { page: Int ->
+ Grid(gridItems, page)
+ }
+ }
+}
+
+@Composable
+private fun Grid(itemCount: Int, pageNum: Int) {
+ val text = remember(pageNum) { "Hello + $pageNum" }
+ LazyVerticalGrid(
+ modifier = Modifier.fillMaxSize(),
+ columns = GridCells.Fixed(3),
+ ) {
+ items(itemCount, contentType = { "cell" }) { _ ->
+ Button(onClick = {}) {
+ Text(text = text)
+ }
+ }
+ }
+}
diff --git a/compose/integration-tests/macrobenchmark/src/main/java/androidx/compose/integration/macrobenchmark/PagerOfLazyGridBenchmark.kt b/compose/integration-tests/macrobenchmark/src/main/java/androidx/compose/integration/macrobenchmark/PagerOfLazyGridBenchmark.kt
new file mode 100644
index 0000000..91a4b14
--- /dev/null
+++ b/compose/integration-tests/macrobenchmark/src/main/java/androidx/compose/integration/macrobenchmark/PagerOfLazyGridBenchmark.kt
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2023 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
+ *
+ * http://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 androidx.compose.integration.macrobenchmark
+
+import android.content.Intent
+import androidx.benchmark.macro.CompilationMode
+import androidx.benchmark.macro.FrameTimingMetric
+import androidx.benchmark.macro.StartupMode
+import androidx.benchmark.macro.junit4.MacrobenchmarkRule
+import androidx.test.filters.LargeTest
+import androidx.test.platform.app.InstrumentationRegistry
+import androidx.test.uiautomator.By
+import androidx.test.uiautomator.UiDevice
+import androidx.test.uiautomator.Until
+import androidx.testutils.createCompilationParams
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.Parameterized
+
+@LargeTest
+@RunWith(Parameterized::class)
+class PagerOfLazyGridBenchmark(
+ private val compilationMode: CompilationMode
+) {
+ @get:Rule
+ val benchmarkRule = MacrobenchmarkRule()
+
+ private lateinit var device: UiDevice
+
+ @Before
+ fun setUp() {
+ val instrumentation = InstrumentationRegistry.getInstrumentation()
+ device = UiDevice.getInstance(instrumentation)
+ }
+
+ @Test
+ fun scroll() {
+ benchmarkRule.measureRepeated(
+ packageName = PackageName,
+ metrics = listOf(FrameTimingMetric()),
+ compilationMode = compilationMode,
+ startupMode = StartupMode.WARM,
+ iterations = 10,
+ setupBlock = {
+ val intent = Intent()
+ intent.action = Action
+ startActivityAndWait(intent)
+ }
+ ) {
+ val nextButton = device.findObject(By.text(NextDescription))
+ repeat(3) {
+ nextButton.click()
+ device.wait(Until.findObject(By.desc(ComposeIdle)), 3000)
+ }
+ }
+ }
+
+ companion object {
+ private const val PackageName = "androidx.compose.integration.macrobenchmark.target"
+ private const val Action =
+ "androidx.compose.integration.macrobenchmark.target.PAGER_LAZYGRID_ACTIVITY"
+ private const val ComposeIdle = "COMPOSE-IDLE"
+ private const val NextDescription = "Next"
+
+ @Parameterized.Parameters(name = "compilation={0}")
+ @JvmStatic
+ fun parameters() = createCompilationParams()
+ }
+}