Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
creates new performance test (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiagoperes committed Jan 6, 2023
1 parent c1cf914 commit f2fb0f1
Show file tree
Hide file tree
Showing 2 changed files with 468 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import br.com.zup.nimbus.core.JsonLoader
import br.com.zup.nimbus.core.Nimbus
import br.com.zup.nimbus.core.NodeUtils
import br.com.zup.nimbus.core.ServerDrivenConfig
import br.com.zup.nimbus.core.dependency.Dependent
import br.com.zup.nimbus.core.tree.ServerDrivenNode
import br.com.zup.nimbus.core.tree.dynamic.node.DynamicNode
import br.com.zup.nimbus.core.tree.findNodeById
import br.com.zup.nimbus.core.ui.UILibrary
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand All @@ -36,6 +38,7 @@ import kotlin.test.assertTrue

private const val MAX_AVERAGE_UPDATE_TIME_MS = 15
private const val FOR_EACH_MAX_AVERAGE_UPDATE_TIME_MS = 15
private const val MAX_TIME_FOR_FILTERING_LIST_MS = 10
private const val SHOULD_PRINT_TIMES = false

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -104,5 +107,42 @@ class PerformanceTest {
fun `should add to cart in reasonable time - with forEach`() = scope.runTest {
runPerformanceTest("products-forEach", FOR_EACH_MAX_AVERAGE_UPDATE_TIME_MS)
}

private fun onUpdateEachNode(node: DynamicNode, listener: (ServerDrivenNode) -> Unit) {
node.addDependent(object: Dependent {
override fun update() {
listener(node)
}
})
node.children?.forEach { onUpdateEachNode(it, listener) }
}

@Test
fun `should filter for each list without performance hiccups`() {
// WHEN the "to-do" screen is rendered
val json = JsonLoader.loadJson("to-do")
val nimbus = Nimbus(ServerDrivenConfig("", "test", httpClient = EmptyHttpClient))
val tree = nimbus.nodeBuilder.buildFromJsonString(json)
tree.initialize(nimbus)
// AND we start measuring time
val start = Clock.System.now().toEpochMilliseconds()
// AND we filter the notes to show only the "to do" notes
NodeUtils.triggerEvent(tree.findNodeById("status-filter"), "onChange", "To do")
// THEN it shouldn't have taken to much time to render the filtered list
val end = Clock.System.now().toEpochMilliseconds() - start
if (SHOULD_PRINT_TIMES) println("===> ELAPSED TIME: ${end}ms") // around 5ms on both Android and iOS
assertTrue(end < MAX_TIME_FOR_FILTERING_LIST_MS)
// WHEN we remove the filter making the list go back to its original state
NodeUtils.triggerEvent(tree.findNodeById("status-filter"), "onChange", "All")
// AND we start counting every node update
val updates = mutableListOf<String>()
onUpdateEachNode(tree) { updates.add(it.component) }
// AND we filter the notes to show only the "to do"
NodeUtils.triggerEvent(tree.findNodeById("status-filter"), "onChange", "To do")
// THEN it should have updated only two components: the filter itself (selection group) and the scroll view
assertEquals(2, updates.size)
assertTrue(updates.contains("todoapp:selectionGroup"))
assertTrue(updates.contains("layout:scrollView"))
}
}

Loading

0 comments on commit f2fb0f1

Please sign in to comment.