Skip to content

Instantly share code, notes, and snippets.

@mohamedmenasy
Last active November 19, 2019 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohamedmenasy/b9ea87432dea17a6aef045de0f3492d4 to your computer and use it in GitHub Desktop.
Save mohamedmenasy/b9ea87432dea17a6aef045de0f3492d4 to your computer and use it in GitHub Desktop.
Koin DI Benchmark Code Snippet
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