Add benchmark for derived state in SnapshotStateObserver
Change-Id: I82ced7ae2bc01727bb34b7e0cb1bc64b25e38bee
diff --git a/compose/runtime/runtime/compose-runtime-benchmark/src/androidTest/java/androidx/compose/runtime/benchmark/SnapshotStateObserverBenchmark.kt b/compose/runtime/runtime/compose-runtime-benchmark/src/androidTest/java/androidx/compose/runtime/benchmark/SnapshotStateObserverBenchmark.kt
index 7b760be..4fe328d 100644
--- a/compose/runtime/runtime/compose-runtime-benchmark/src/androidTest/java/androidx/compose/runtime/benchmark/SnapshotStateObserverBenchmark.kt
+++ b/compose/runtime/runtime/compose-runtime-benchmark/src/androidTest/java/androidx/compose/runtime/benchmark/SnapshotStateObserverBenchmark.kt
@@ -22,6 +22,7 @@
import androidx.benchmark.junit4.measureRepeated
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.runtime.snapshots.SnapshotStateObserver
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -46,7 +47,7 @@
private val doNothing: (Any) -> Unit = { _ -> }
private lateinit var stateObserver: SnapshotStateObserver
- private val models: List<MutableState<Any>> = List(StateCount) { mutableStateOf(0) }
+ private val models: List<MutableState<Int>> = List(StateCount) { mutableStateOf(0) }
private val nodes: List<Any> = List(ScopeCount) { it }
private lateinit var random: Random
@@ -116,6 +117,40 @@
}
@Test
+ fun derivedStateObservation() {
+ runOnUiThread {
+ val node = Any()
+ val states = models.take(3)
+ val derivedState = derivedStateOf {
+ states[0].value + states[1].value + states[2].value
+ }
+
+ stateObserver.observeReads(node, doNothing) {
+ // read derived state a few times
+ repeat(10) {
+ derivedState.value
+ }
+ }
+
+ benchmarkRule.measureRepeated {
+ stateObserver.observeReads(node, doNothing) {
+ // read derived state a few times
+ repeat(10) {
+ derivedState.value
+ }
+ }
+
+ runWithTimingDisabled {
+ states.forEach {
+ it.value += 1
+ }
+ Snapshot.sendApplyNotifications()
+ }
+ }
+ }
+ }
+
+ @Test
fun deeplyNestedModelObservations() {
assumeTrue(Build.VERSION.SDK_INT != 29)
runOnUiThread {