|
| 1 | +@file:OptIn(WorkflowExperimentalApi::class) |
| 2 | + |
1 | 3 | package com.squareup.benchmark.composeworkflow.benchmark |
2 | 4 |
|
3 | 5 | import androidx.benchmark.junit4.BenchmarkRule |
4 | 6 | import androidx.benchmark.junit4.measureRepeated |
| 7 | +import androidx.compose.runtime.Composable |
5 | 8 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 9 | +import com.squareup.workflow1.RuntimeConfigOptions |
| 10 | +import com.squareup.workflow1.Workflow |
| 11 | +import com.squareup.workflow1.WorkflowExperimentalApi |
| 12 | +import com.squareup.workflow1.WorkflowExperimentalRuntime |
| 13 | +import com.squareup.workflow1.compose.ComposeWorkflow |
| 14 | +import com.squareup.workflow1.compose.composable |
| 15 | +import com.squareup.workflow1.compose.renderChild |
| 16 | +import com.squareup.workflow1.renderChild |
| 17 | +import com.squareup.workflow1.renderWorkflowIn |
| 18 | +import com.squareup.workflow1.stateless |
| 19 | +import kotlinx.coroutines.Job |
| 20 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 21 | +import kotlinx.coroutines.job |
| 22 | +import kotlinx.coroutines.plus |
| 23 | +import kotlinx.coroutines.test.runTest |
6 | 24 | import org.junit.Rule |
7 | 25 | import org.junit.Test |
8 | 26 | import org.junit.runner.RunWith |
| 27 | +import kotlin.test.assertEquals |
9 | 28 |
|
| 29 | +@OptIn(WorkflowExperimentalRuntime::class) |
10 | 30 | @RunWith(AndroidJUnit4::class) |
11 | 31 | class ComposeWorkflowMicroBenchmark { |
12 | 32 |
|
13 | 33 | @get:Rule val benchmarkRule = BenchmarkRule() |
14 | 34 |
|
15 | | - @Test fun foo() { |
| 35 | + @Test fun tradRoot_tradChildren_initialRender() { |
| 36 | + benchmarkSimpleTreeInitialRender( |
| 37 | + maxChildCount = 100, |
| 38 | + composeRoot = false, |
| 39 | + composeChildren = false |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + @Test fun tradRoot_composeChildren_initialRender() { |
| 44 | + benchmarkSimpleTreeInitialRender( |
| 45 | + maxChildCount = 100, |
| 46 | + composeRoot = false, |
| 47 | + composeChildren = true |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + @Test fun composeRoot_tradChildren_initialRender() { |
| 52 | + benchmarkSimpleTreeInitialRender( |
| 53 | + maxChildCount = 100, |
| 54 | + composeRoot = true, |
| 55 | + composeChildren = false |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + @Test fun composeRoot_composeChildren_initialRender() { |
| 60 | + benchmarkSimpleTreeInitialRender( |
| 61 | + maxChildCount = 100, |
| 62 | + composeRoot = true, |
| 63 | + composeChildren = true |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + private fun benchmarkSimpleTreeInitialRender( |
| 68 | + maxChildCount: Int, |
| 69 | + composeRoot: Boolean, |
| 70 | + composeChildren: Boolean |
| 71 | + ) = runTest { |
| 72 | + val props = |
| 73 | + MutableStateFlow(RootWorkflowProps(childCount = 0, composeChildren = composeChildren)) |
| 74 | + val workflowJob = Job(parent = coroutineContext.job) |
| 75 | + val renderings = renderWorkflowIn( |
| 76 | + workflow = if (composeRoot) { |
| 77 | + composeSimpleRoot |
| 78 | + } else { |
| 79 | + traditionalSimpleRoot |
| 80 | + }, |
| 81 | + props = props, |
| 82 | + scope = this + workflowJob, |
| 83 | + runtimeConfig = RuntimeConfigOptions.ALL, |
| 84 | + onOutput = {} |
| 85 | + ) |
| 86 | + |
16 | 87 | benchmarkRule.measureRepeated { |
17 | | - Thread.sleep(100) |
| 88 | + runWithTimingDisabled { |
| 89 | + props.value = RootWorkflowProps(childCount = 0, composeChildren = composeChildren) |
| 90 | + testScheduler.runCurrent() |
| 91 | + assertEquals(0, renderings.value.rendering) |
| 92 | + } |
| 93 | + |
| 94 | + props.value = RootWorkflowProps(childCount = maxChildCount, composeChildren = composeChildren) |
| 95 | + testScheduler.runCurrent() |
| 96 | + assertEquals(maxChildCount, renderings.value.rendering) |
18 | 97 | } |
| 98 | + |
| 99 | + workflowJob.cancel() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +private data class RootWorkflowProps( |
| 104 | + val childCount: Int, |
| 105 | + val composeChildren: Boolean |
| 106 | +) |
| 107 | + |
| 108 | +private val traditionalSimpleRoot = Workflow.stateless<RootWorkflowProps, Nothing, Int> { props -> |
| 109 | + var rendering = 0 |
| 110 | + repeat(props.childCount) { child -> |
| 111 | + rendering += renderChild( |
| 112 | + key = child.toString(), |
| 113 | + child = if (props.composeChildren) { |
| 114 | + composeSimpleLeaf |
| 115 | + } else { |
| 116 | + traditionalSimpleLeaf |
| 117 | + } |
| 118 | + ) |
19 | 119 | } |
| 120 | + rendering |
20 | 121 | } |
| 122 | + |
| 123 | +private val composeSimpleRoot = Workflow.composable<RootWorkflowProps, Nothing, Int> { props, _ -> |
| 124 | + var rendering = 0 |
| 125 | + repeat(props.childCount) { |
| 126 | + rendering += renderChild( |
| 127 | + workflow = if (props.composeChildren) { |
| 128 | + composeSimpleLeaf |
| 129 | + } else { |
| 130 | + traditionalSimpleLeaf |
| 131 | + }, |
| 132 | + props = Unit, |
| 133 | + onOutput = null |
| 134 | + ) |
| 135 | + } |
| 136 | + rendering |
| 137 | +} |
| 138 | + |
| 139 | +private val traditionalSimpleLeaf = Workflow.stateless<Unit, Nothing, Int> { 1 } |
| 140 | +private val composeSimpleLeaf = Workflow.composable<Unit, Nothing, Int> { _, _ -> 1 } |
0 commit comments