Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use kotlin.time API #1697

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import org.koin.core.context.GlobalContext.startKoin
import org.koin.core.context.GlobalContext.stopKoin
import org.koin.core.context.waitKoinStart
import org.koin.core.logger.*
import org.koin.core.time.Timer
import org.koin.dsl.koinApplication
import org.koin.mp.KoinPlatform
import kotlin.test.Test
import kotlin.time.measureTime
import kotlin.time.measureTimedValue

class PerfsTest {

Expand All @@ -24,30 +25,32 @@ class PerfsTest {
}

private fun runPerfs(log: Logger = EmptyLogger(), isLazy: Boolean = false) {
val timerStart = Timer.start()
val app = koinApplication {
if (isLazy) {
lazyModules(perfModule400())
} else {
modules(perfModule400().value)
val (app, startKoinAppDuration) = measureTimedValue {
koinApplication {
if (isLazy) {
lazyModules(perfModule400())
} else {
modules(perfModule400().value)
}
}
}
timerStart.stop()
println("perf400 - start in ${timerStart.getTimeInMillis()}")

println("perf400 - start in ${startKoinAppDuration.inWholeMilliseconds} ms")

val koin = app.koin

if (isLazy) {
koin.waitAllStartJobs()
}

val timerRun = Timer.start()
koin.get<Perfs.A27>()
koin.get<Perfs.B31>()
koin.get<Perfs.C12>()
koin.get<Perfs.D42>()
timerRun.stop()
println("perf400 - executed in ${timerRun.getTimeInMillis()}")
val injectDuration = measureTime {
koin.get<Perfs.A27>()
koin.get<Perfs.B31>()
koin.get<Perfs.C12>()
koin.get<Perfs.D42>()
}

println("perf400 - executed in ${injectDuration.inWholeMilliseconds} ms")

app.close()
}
Expand Down
6 changes: 3 additions & 3 deletions core/koin-core/src/commonMain/kotlin/org/koin/core/Koin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import org.koin.core.registry.PropertyRegistry
import org.koin.core.registry.ScopeRegistry
import org.koin.core.scope.Scope
import org.koin.core.scope.ScopeID
import org.koin.core.time.measureDuration
import org.koin.mp.KoinPlatformTools
import kotlin.reflect.KClass
import kotlin.time.measureTime

/**
* Koin
Expand Down Expand Up @@ -326,9 +326,9 @@ class Koin {
*/
fun createEagerInstances() {
logger.debug("Create eager instances ...")
val duration = measureDuration {
val duration = measureTime {
instanceRegistry.createAllEagerInstances()
}
logger.debug("Created eager instances in $duration ms")
logger.debug("Created eager instances in ${duration.inWholeMilliseconds} ms")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import org.koin.core.logger.Level
import org.koin.core.logger.Logger
import org.koin.core.module.KoinApplicationDslMarker
import org.koin.core.module.Module
import org.koin.core.time.measureDuration
import org.koin.mp.KoinPlatformTools
import kotlin.time.measureTime

/**
* Koin Application
Expand Down Expand Up @@ -58,9 +58,9 @@ class KoinApplication private constructor() {
*/
fun modules(modules: List<Module>): KoinApplication {
if (koin.logger.isAt(Level.INFO)) {
val duration = measureDuration { loadModules(modules) }
val duration = measureTime { loadModules(modules) }
val count = koin.instanceRegistry.size()
koin.logger.display(Level.INFO, "Started $count definitions in $duration ms")
koin.logger.display(Level.INFO, "Started $count definitions in ${duration.inWholeMilliseconds} ms")
} else {
loadModules(modules)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ import org.koin.core.module.KoinDslMarker
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.parameter.ParametersHolder
import org.koin.core.qualifier.Qualifier
import org.koin.core.time.Timer
import org.koin.ext.getFullName
import org.koin.mp.KoinPlatformTimeTools
import org.koin.mp.KoinPlatformTools
import org.koin.mp.Lockable
import org.koin.mp.ThreadLocal
import kotlin.reflect.KClass
import kotlin.time.measureTimedValue

@Suppress("UNCHECKED_CAST")
@OptIn(KoinInternalApi::class)
Expand Down Expand Up @@ -201,12 +200,11 @@ class Scope(
val qualifierString = qualifier?.let { " with qualifier '$qualifier'" } ?: ""
_koin.logger.display(Level.DEBUG, "|- '${clazz.getFullName()}'$qualifierString ...")

val start = KoinPlatformTimeTools.getTimeInNanoSeconds()
val instance = resolveInstance<T>(qualifier, clazz, parameters)
val stop = KoinPlatformTimeTools.getTimeInNanoSeconds()
val duration = (stop - start) / Timer.NANO_TO_MILLI
val (instance, duration) = measureTimedValue<T> {
resolveInstance(qualifier, clazz, parameters)
}

_koin.logger.display(Level.DEBUG, "|- '${clazz.getFullName()}' in $duration ms")
_koin.logger.display(Level.DEBUG, "|- '${clazz.getFullName()}' in ${duration.inWholeMilliseconds} ms")
instance
} else {
resolveInstance(qualifier, clazz, parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import org.koin.mp.KoinPlatformTimeTools
* @param code - code to execute
* @return Time in milliseconds
*/
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Use measureTime() from kotlin.time instead.")
inline fun measureDuration(code: () -> Unit): TimeInMillis {
return measureTimedValue(code).second
}
Expand All @@ -38,16 +40,19 @@ inline fun measureDuration(code: () -> Unit): TimeInMillis {
* @param code - code to execute
* @return Pair Value & Time in milliseconds
*/
@Deprecated("Use measureTimedValue() from kotlin.time instead.")
inline fun <T> measureDurationForResult(code: () -> T): Pair<T, TimeInMillis> {
val (value, duration) = measureTimedValue(code)
return Pair(value, duration)
}

@Deprecated("Use measureTimedValue() from kotlin.time instead.")
inline fun <T> measureTimedValue(code: () -> T): Pair<T, TimeInMillis> {
val start = KoinPlatformTimeTools.getTimeInNanoSeconds()
val value = code()
val stop = KoinPlatformTimeTools.getTimeInNanoSeconds()
return Pair(value, (stop - start) / Timer.NANO_TO_MILLI)
}

@Deprecated("Use kotlin.time.Duration instead.")
typealias TimeInMillis = Double
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlin.time.Duration.Companion.ZERO
import kotlin.time.DurationUnit
import kotlin.time.toDuration

@Deprecated("Use TimeSource.Monotonic instead.")
class Timer {

val start: Duration = KoinPlatformTimeTools.getTimeInNanoSeconds().toDuration(DurationUnit.NANOSECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
*/
package org.koin.mp

expect object KoinPlatformTimeTools {
fun getTimeInNanoSeconds(): Long
import kotlin.time.TimeSource

@Deprecated("Use kotlin.time instead.")
object KoinPlatformTimeTools {

@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Use TimeSource.Monotonic.markNow() instead.")
fun getTimeInNanoSeconds(): Long =
TimeSource.Monotonic.markNow().elapsedNow().inWholeNanoseconds
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import org.koin.core.component.inject
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.core.scope.Scope
import org.koin.core.time.measureDuration
import org.koin.dsl.module
import org.koin.ext.inject
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.time.measureTime

class B : KoinScopeComponent {
override val scope: Scope by lazy { createScope(this) }
Expand Down Expand Up @@ -78,8 +78,7 @@ class PlayTest {
}
}

fun measureDuration(msg: String, code: () -> Unit): Double {
val duration = measureDuration(code)
println("$msg in $duration ms")
return duration
fun measureDuration(msg: String, code: () -> Unit) {
val duration = measureTime(code)
println("$msg in ${duration.inWholeMilliseconds} ms")
}
56 changes: 0 additions & 56 deletions core/koin-core/src/jsMain/kotlin/org/koin/core/time/TimeSource.kt

This file was deleted.

10 changes: 0 additions & 10 deletions core/koin-core/src/jsMain/kotlin/org/koin/mp/PlatformTimeTools.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import org.koin.core.logger.Level
import org.koin.core.parameter.ParametersHolder
import org.koin.core.parameter.emptyParametersHolder
import org.koin.core.scope.Scope
import org.koin.core.time.measureDurationForResult
import org.koin.ext.getFullName
import java.lang.reflect.Constructor
import kotlin.reflect.KClass
import kotlin.time.measureTimedValue

@KoinReflectAPI
@Deprecated("Koin Reflection API is deprecated")
Expand All @@ -35,20 +35,20 @@ fun <T : Any> Scope.newInstance(kClass: KClass<T>, params: ParametersHolder): T
?: error("No constructor found for class '${kClass.getFullName()}'")

val args = if (logger.level == Level.DEBUG) {
val (_args, duration) = measureDurationForResult {
val (_args, duration) = measureTimedValue {
getArguments(constructor, this, params)
}
logger.debug("|- got arguments in $duration ms")
logger.debug("|- got arguments in ${duration.inWholeMilliseconds} ms")
_args
} else {
getArguments(constructor, this, params)
}

instance = if (logger.level == Level.DEBUG) {
val (_instance, duration) = measureDurationForResult {
val (_instance, duration) = measureTimedValue {
createInstance(args, constructor)
}
logger.debug("|- created instance in $duration ms")
logger.debug("|- created instance in ${duration.inWholeMilliseconds} ms")
_instance
} else {
createInstance(args, constructor)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import org.junit.Test
import org.koin.core.error.InstanceCreationException
import org.koin.core.logger.Level
import org.koin.core.parameter.parametersOf
import org.koin.core.time.measureDuration
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import org.koin.dsl.single
import kotlin.time.measureTime

class CreateAPITest {

Expand All @@ -24,10 +24,10 @@ class CreateAPITest {
)
}.koin

val duration = measureDuration {
val duration = measureTime {
koin.get<ComponentB>()
}
println("create api in $duration ms")
println("create api in ${duration.inWholeMilliseconds} ms")

val createKoin = koinApplication {
printLogger(Level.DEBUG)
Expand All @@ -39,10 +39,10 @@ class CreateAPITest {
)
}.koin

val createDuration = measureDuration {
val createDuration = measureTime {
createKoin.get<ComponentB>()
}
println("create api in $createDuration ms")
println("create api in ${createDuration.inWholeMilliseconds} ms")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.koin.core.instance

import org.junit.Test
import org.koin.core.time.measureDurationForResult
import kotlin.reflect.KClass
import kotlin.time.measureTimedValue

class ReflectAPITest {

Expand Down Expand Up @@ -34,10 +34,10 @@ class ReflectAPITest {
ctor.newInstance() as T
} else {
println("|- types:${types.toList()}")
val (map, duration) = measureDurationForResult {
val (map, duration) = measureTimedValue {
types.map { it.constructors.first().newInstance() }
}
println("|- make sub instances:$map in $duration")
println("|- make sub instances: $map in $duration")
val toTypedArray = map.toTypedArray()
measure("|- created with subtypes") {
ctor.newInstance(*toTypedArray) as T
Expand All @@ -48,7 +48,7 @@ class ReflectAPITest {
}

fun <T> measure(message: String, code: () -> T): T {
val (i, time) = measureDurationForResult(code)
println("$message in $time ms")
val (i, duration) = measureTimedValue(code)
println("$message in ${duration.inWholeMilliseconds} ms")
return i
}
Loading