|
| 1 | +package com.cheonjaeung.compose.grid.benchmark |
| 2 | + |
| 3 | +import androidx.benchmark.junit4.BenchmarkRule |
| 4 | +import androidx.benchmark.junit4.measureRepeated |
| 5 | +import androidx.compose.foundation.layout.Arrangement |
| 6 | +import androidx.compose.foundation.layout.Box |
| 7 | +import androidx.compose.foundation.layout.Column |
| 8 | +import androidx.compose.foundation.layout.Row |
| 9 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 10 | +import androidx.compose.foundation.layout.height |
| 11 | +import androidx.compose.foundation.lazy.grid.GridCells |
| 12 | +import androidx.compose.foundation.lazy.grid.LazyVerticalGrid |
| 13 | +import androidx.compose.foundation.lazy.grid.items |
| 14 | +import androidx.compose.runtime.Composable |
| 15 | +import androidx.compose.runtime.getValue |
| 16 | +import androidx.compose.runtime.mutableStateOf |
| 17 | +import androidx.compose.runtime.setValue |
| 18 | +import androidx.compose.ui.Modifier |
| 19 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 20 | +import androidx.compose.ui.unit.dp |
| 21 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 22 | +import com.cheonjaeung.compose.grid.SimpleGridCells |
| 23 | +import com.cheonjaeung.compose.grid.VerticalGrid |
| 24 | +import org.junit.Rule |
| 25 | +import org.junit.Test |
| 26 | +import org.junit.runner.RunWith |
| 27 | + |
| 28 | +private const val COLUMNS = 3 |
| 29 | +private val ITEM_HEIGHT = 50.dp |
| 30 | +private val SPACING = 8.dp |
| 31 | + |
| 32 | +/** |
| 33 | + * Benchmarks for first-composition cost: [VerticalGrid] vs [LazyVerticalGrid] vs [Row]+[Column]. |
| 34 | + * |
| 35 | + * It can answer to two questions: |
| 36 | + * - How does [VerticalGrid] compare to [Row]+[Column] grid? |
| 37 | + * - How does [VerticalGrid] compare to [LazyVerticalGrid] when both must compose all items? |
| 38 | + * |
| 39 | + * [LazyVerticalGrid] is given an exact height that fits every item (all-visible), |
| 40 | + * so it cannot virtualize and the comparison is like-for-like on composition + measure cost. |
| 41 | + * |
| 42 | + * All item counts are multiples of [COLUMNS] so no placeholder Spacers are needed |
| 43 | + * in [RowColumnCase] — both contestants see exactly the same number of composables. |
| 44 | + * |
| 45 | + * Run with: |
| 46 | + * ``` |
| 47 | + * ./gradlew :benchmark:connectedAndroidTest |
| 48 | + * ``` |
| 49 | + * |
| 50 | + * Results are saved to: |
| 51 | + * ``` |
| 52 | + * benchmark/build/outputs/connected_android_test_additional_output/releaseAndroidTest/connected/<device>/ |
| 53 | + * ``` |
| 54 | + */ |
| 55 | +@RunWith(AndroidJUnit4::class) |
| 56 | +class GridBenchmark { |
| 57 | + |
| 58 | + @get:Rule(order = 1) |
| 59 | + val benchmarkRule = BenchmarkRule() |
| 60 | + |
| 61 | + @get:Rule(order = 2) |
| 62 | + val composeTestRule = createComposeRule() |
| 63 | + |
| 64 | + @Test |
| 65 | + fun verticalGrid_9() { |
| 66 | + benchmarkContent { VerticalGridCase(itemCount = 9) } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun verticalGrid_30() { |
| 71 | + benchmarkContent { VerticalGridCase(itemCount = 30) } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun verticalGrid_60() { |
| 76 | + benchmarkContent { VerticalGridCase(itemCount = 60) } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun lazyVerticalGrid_9() { |
| 81 | + benchmarkContent { LazyVerticalGridCase(itemCount = 9) } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun lazyVerticalGrid_30() { |
| 86 | + benchmarkContent { LazyVerticalGridCase(itemCount = 30) } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun lazyVerticalGrid_60() { |
| 91 | + benchmarkContent { LazyVerticalGridCase(itemCount = 60) } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun rowColumn_9() { |
| 96 | + benchmarkContent { RowColumnCase(itemCount = 9) } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun rowColumn_30() { |
| 101 | + benchmarkContent { RowColumnCase(itemCount = 30) } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun rowColumn_60() { |
| 106 | + benchmarkContent { RowColumnCase(itemCount = 60) } |
| 107 | + } |
| 108 | + |
| 109 | + private fun benchmarkContent(content: @Composable () -> Unit) { |
| 110 | + var isVisible by mutableStateOf(false) |
| 111 | + |
| 112 | + composeTestRule.setContent { |
| 113 | + if (isVisible) content() |
| 114 | + } |
| 115 | + composeTestRule.waitForIdle() |
| 116 | + |
| 117 | + benchmarkRule.measureRepeated { |
| 118 | + isVisible = true |
| 119 | + composeTestRule.waitForIdle() |
| 120 | + |
| 121 | + runWithMeasurementDisabled { |
| 122 | + isVisible = false |
| 123 | + composeTestRule.waitForIdle() |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +@Composable |
| 130 | +private fun VerticalGridCase(itemCount: Int) { |
| 131 | + VerticalGrid( |
| 132 | + columns = SimpleGridCells.Fixed(COLUMNS), |
| 133 | + modifier = Modifier.fillMaxWidth(), |
| 134 | + horizontalArrangement = Arrangement.spacedBy(SPACING), |
| 135 | + verticalArrangement = Arrangement.spacedBy(SPACING), |
| 136 | + ) { |
| 137 | + repeat(itemCount) { Box(modifier = Modifier.height(ITEM_HEIGHT)) } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +@Composable |
| 142 | +private fun LazyVerticalGridCase(itemCount: Int) { |
| 143 | + val rows = itemCount / COLUMNS |
| 144 | + val gridHeight = ITEM_HEIGHT * rows + SPACING * (rows - 1) |
| 145 | + LazyVerticalGrid( |
| 146 | + columns = GridCells.Fixed(COLUMNS), |
| 147 | + modifier = Modifier.height(gridHeight), |
| 148 | + horizontalArrangement = Arrangement.spacedBy(SPACING), |
| 149 | + verticalArrangement = Arrangement.spacedBy(SPACING), |
| 150 | + ) { |
| 151 | + items(List(itemCount) { it }) { Box(modifier = Modifier.height(ITEM_HEIGHT)) } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +@Composable |
| 156 | +private fun RowColumnCase(itemCount: Int) { |
| 157 | + val rowCount = itemCount / COLUMNS |
| 158 | + Column( |
| 159 | + modifier = Modifier.fillMaxWidth(), |
| 160 | + verticalArrangement = Arrangement.spacedBy(SPACING), |
| 161 | + ) { |
| 162 | + (0 until rowCount).forEach { _ -> |
| 163 | + Row( |
| 164 | + modifier = Modifier.fillMaxWidth(), |
| 165 | + horizontalArrangement = Arrangement.spacedBy(SPACING), |
| 166 | + ) { |
| 167 | + (0 until COLUMNS).forEach { _ -> |
| 168 | + Box(modifier = Modifier.weight(1f).height(ITEM_HEIGHT)) |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments