Skip to content

Commit

Permalink
clean up time API
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed May 18, 2021
1 parent e00f7f5 commit 64ceaae
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
16 changes: 3 additions & 13 deletions core/koin-core/src/commonMain/kotlin/org/koin/core/time/Measure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.koin.core.time

import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
import kotlin.time.measureTimedValue
Expand All @@ -28,12 +29,7 @@ import kotlin.time.measureTimedValue

@OptIn(ExperimentalTime::class)
fun measureDuration(code: () -> Unit): Double {
return measureTime(code).inMilliseconds
}

fun measureDuration(message: String, code: () -> Unit) {
val time = measureDuration(code)
println("$message - $time ms")
return measureTime(code).toDouble(DurationUnit.MILLISECONDS)
}

/**
Expand All @@ -42,11 +38,5 @@ fun measureDuration(message: String, code: () -> Unit) {
@OptIn(ExperimentalTime::class)
fun <T> measureDurationForResult(code: () -> T): Pair<T, Double> {
val result = measureTimedValue(code)
return Pair(result.value, result.duration.inMilliseconds)
}

fun <T> measureDurationForResult(message: String, code: () -> T): T {
val (result, time) = measureDurationForResult(code)
println("$message - $time ms")
return result
return Pair(result.value, result.duration.toDouble(DurationUnit.MILLISECONDS))
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package org.koin.core

import kotlin.test.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import kotlin.test.fail
import kotlin.test.Test
import org.koin.Simple
import org.koin.core.error.ClosedScopeException
import org.koin.core.qualifier.named
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import kotlin.test.*

class ScopeTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import org.koin.core.time.measureDuration
import org.koin.dsl.module
import org.koin.ext.inject
import kotlin.test.Test
import kotlin.time.DurationUnit
import kotlin.time.measureTime

class B : KoinScopeComponent {
override val scope: Scope by lazy { createScope(this) }
Expand Down Expand Up @@ -68,4 +70,10 @@ class PlayTest {

stopKoin()
}
}

fun measureDuration(msg : String, code: () -> Unit): Double {
val duration = measureDuration(code)
println(msg)
return duration
}
11 changes: 9 additions & 2 deletions core/koin-core/src/commonTest/kotlin/org/koin/perfs/PerfsTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.koin.perfs

import kotlin.test.Test
import org.koin.core.A
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.definition.Definitions
Expand All @@ -10,6 +9,7 @@ import org.koin.core.time.measureDurationForResult
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import org.koin.test.assertDefinitionsCount
import kotlin.test.Test

class PerfsTest {

Expand Down Expand Up @@ -42,7 +42,8 @@ class PerfsTest {
A::class,
definition = { A() },
options = Options(),
scopeQualifier = ScopeDefinition.ROOT_SCOPE_QUALIFIER)
scopeQualifier = ScopeDefinition.ROOT_SCOPE_QUALIFIER
)
)
app.close()
}
Expand Down Expand Up @@ -89,4 +90,10 @@ class PerfsTest {

app.close()
}
}

fun <T> measureDurationForResult(msg: String, code: () -> T): T {
val result = measureDurationForResult(code)
println(msg)
return result.first
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ fun main() {
measureDuration("Got Coffee") {
coffeeShop.maker.brew()
}
}

fun measureDuration(msg : String, code: () -> Unit): Double {
val duration = measureDuration(code)
println(msg)
return duration
}

0 comments on commit 64ceaae

Please sign in to comment.