|
| 1 | +package com.aquib.androidperflab |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import android.util.Log |
| 5 | +import androidx.activity.ComponentActivity |
| 6 | +import androidx.compose.runtime.Recomposer |
| 7 | +import androidx.compose.ui.test.junit4.createAndroidComposeRule |
| 8 | +import androidx.compose.ui.test.onNodeWithTag |
| 9 | +import androidx.compose.ui.test.performClick |
| 10 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 11 | +import androidx.test.platform.app.InstrumentationRegistry |
| 12 | +import com.aquib.androidperflab.ui.DetailScreen |
| 13 | +import com.aquib.androidperflab.ui.FeedItem |
| 14 | +import com.aquib.androidperflab.ui.theme.AndroidPerfLabTheme |
| 15 | +import kotlinx.coroutines.test.TestCoroutineScheduler |
| 16 | +import kotlinx.coroutines.test.runTest |
| 17 | +import org.junit.Before |
| 18 | +import org.junit.Rule |
| 19 | +import org.junit.Test |
| 20 | +import org.junit.runner.RunWith |
| 21 | + |
| 22 | +@RunWith(AndroidJUnit4::class) |
| 23 | +class RecompositionBenchmark { |
| 24 | + // Shared scheduler drives virtual time for runTest blocks in this class. |
| 25 | + private val testScheduler = TestCoroutineScheduler() |
| 26 | + |
| 27 | + @get:Rule |
| 28 | + val composeTestRule = createAndroidComposeRule<ComponentActivity>() |
| 29 | + |
| 30 | + private val testItem = FeedItem( |
| 31 | + id = 42, |
| 32 | + title = "Performance Testing in Android", |
| 33 | + subtitle = "Benchmarking Compose recompositions", |
| 34 | + description = "Android performance testing involves many dimensions including startup " + |
| 35 | + "time, frame rendering, and recomposition overhead. This article explores " + |
| 36 | + "techniques for measuring each of these in depth.", |
| 37 | + author = "Mohd Aquib", |
| 38 | + imageUrl = "", |
| 39 | + timestampMillis = 1_700_000_000_000L, |
| 40 | + ) |
| 41 | + |
| 42 | + @Before |
| 43 | + fun setUp() { |
| 44 | + composeTestRule.setContent { |
| 45 | + AndroidPerfLabTheme { |
| 46 | + DetailScreen(item = testItem, onBack = {}) |
| 47 | + } |
| 48 | + } |
| 49 | + // Pause Compose's virtual clock so the LaunchedEffect tick loop in DetailScreen |
| 50 | + // does not fire between measurement checkpoints, keeping deltas deterministic. |
| 51 | + composeTestRule.mainClock.autoAdvance = false |
| 52 | + // Drain the initial composition pass before any test captures a baseline count. |
| 53 | + composeTestRule.mainClock.advanceTimeByFrame() |
| 54 | + composeTestRule.waitForIdle() |
| 55 | + } |
| 56 | + |
| 57 | + // ── Like button ────────────────────────────────────────────────────────────── |
| 58 | + |
| 59 | + @Test |
| 60 | + fun likeButton_recompositionCount() { |
| 61 | + val before = totalChangeCount() |
| 62 | + |
| 63 | + composeTestRule.onNodeWithTag("detail_like_button").performClick() |
| 64 | + composeTestRule.mainClock.advanceTimeByFrame() |
| 65 | + composeTestRule.waitForIdle() |
| 66 | + |
| 67 | + val delta = totalChangeCount() - before |
| 68 | + record("like_button_click", delta) |
| 69 | + } |
| 70 | + |
| 71 | + // ── Bookmark button ────────────────────────────────────────────────────────── |
| 72 | + |
| 73 | + @Test |
| 74 | + fun bookmarkButton_recompositionCount() { |
| 75 | + val before = totalChangeCount() |
| 76 | + |
| 77 | + composeTestRule.onNodeWithTag("detail_bookmark_button").performClick() |
| 78 | + composeTestRule.mainClock.advanceTimeByFrame() |
| 79 | + composeTestRule.waitForIdle() |
| 80 | + |
| 81 | + val delta = totalChangeCount() - before |
| 82 | + record("bookmark_button_click", delta) |
| 83 | + } |
| 84 | + |
| 85 | + // ── Tick-driven recompositions ─────────────────────────────────────────────── |
| 86 | + |
| 87 | + @Test |
| 88 | + fun tickEffect_recompositionCountPerInterval() = runTest(testScheduler) { |
| 89 | + val tickCount = 5 |
| 90 | + var totalDelta = 0L |
| 91 | + |
| 92 | + repeat(tickCount) { index -> |
| 93 | + val before = totalChangeCount() |
| 94 | + |
| 95 | + // Advance Compose's main clock to unblock the delay(500L) in LaunchedEffect. |
| 96 | + composeTestRule.mainClock.advanceTimeBy(500L) |
| 97 | + // Advance TestCoroutineScheduler by the same interval so virtual time |
| 98 | + // stays in sync for any coroutines running on testScheduler. |
| 99 | + testScheduler.advanceTimeBy(500L) |
| 100 | + composeTestRule.waitForIdle() |
| 101 | + |
| 102 | + val delta = totalChangeCount() - before |
| 103 | + totalDelta += delta |
| 104 | + Log.d(TAG, "Tick ${index + 1}: $delta recompositions") |
| 105 | + } |
| 106 | + |
| 107 | + record("tick_effect_per_interval", totalDelta / tickCount) |
| 108 | + } |
| 109 | + |
| 110 | + // ── Helpers ────────────────────────────────────────────────────────────────── |
| 111 | + |
| 112 | + private fun totalChangeCount(): Long = |
| 113 | + Recomposer.runningRecomposers.value.sumOf { it.changeCount } |
| 114 | + |
| 115 | + private fun record(interaction: String, recompositionCount: Long) { |
| 116 | + Log.d(TAG, "[$interaction] recompositions per interaction: $recompositionCount") |
| 117 | + val bundle = Bundle().apply { putLong(interaction, recompositionCount) } |
| 118 | + InstrumentationRegistry.getInstrumentation().sendStatus(2, bundle) |
| 119 | + } |
| 120 | + |
| 121 | + companion object { |
| 122 | + private const val TAG = "RecompositionBenchmark" |
| 123 | + } |
| 124 | +} |
0 commit comments