Last active
November 19, 2019 18:46
-
-
Save mohamedmenasy/b9ea87432dea17a6aef045de0f3492d4 to your computer and use it in GitHub Desktop.
Koin DI Benchmark Code Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun measureTime(block: () -> Unit): Double = measureNanoTime(block) / 1000000.0 | |
data class TestResult( | |
val startupTime: List<Milliseconds>, | |
val injectionTime: List<Milliseconds> | |
) | |
private fun runTest( | |
setup: () -> Unit, | |
test: () -> Unit, | |
teardown: () -> Unit = {} | |
): TestResult { | |
val startup = (1..rounds).map { measureTime { setup() }.also { teardown() } } | |
setup() | |
val testDurations = (1..rounds).map { measureTime { test() } } | |
teardown() | |
return TestResult(startup, testDurations) | |
} | |
fun log(msg: String) { | |
Log.i("DI-TEST", msg) | |
} | |
fun List<Milliseconds>.median() = sorted().let { (it[it.size / 2] + it[(it.size - 1) / 2]) / 2 } | |
private fun koinTest() { | |
lateinit var koin: Koin | |
val testResult = runTest( | |
setup = { | |
koin = startKoin { | |
// module list | |
modules(listOf()) | |
}.koin | |
}, | |
test = { koin.get<Navigator>() }, | |
teardown = { stopKoin() } | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment