From b02bb9d5c0c2c4509d42bc399bdea399622b94cc Mon Sep 17 00:00:00 2001 From: pubiqq Date: Fri, 10 Nov 2023 09:41:47 +0300 Subject: [PATCH] Use kotlin.time API --- .../jvmTest/kotlin/org/koin/test/PerfsTest.kt | 35 ++++++------ .../commonMain/kotlin/org/koin/core/Koin.kt | 6 +- .../kotlin/org/koin/core/KoinApplication.kt | 6 +- .../kotlin/org/koin/core/scope/Scope.kt | 12 ++-- .../kotlin/org/koin/core/time/Measure.kt | 5 ++ .../kotlin/org/koin/core/time/Timer.kt | 1 + .../org/koin/mp/KoinPlatformTimeTools.kt | 11 +++- .../kotlin/org/koin/core/SetterInjectTest.kt | 9 ++- .../kotlin/org/koin/core/time/TimeSource.kt | 56 ------------------- .../kotlin/org/koin/mp/PlatformTimeTools.kt | 10 ---- .../org/koin/core/instance/InstanceBuilder.kt | 10 ++-- .../org/koin/mp/KoinPlatformTimeTools.kt | 7 --- .../org/koin/core/instance/CreateAPITest.kt | 10 ++-- .../org/koin/core/instance/ReflectAPITest.kt | 10 ++-- .../org/koin/mp/KoinPlatformTimeTools.kt | 9 --- .../org/koin/test/verify/VerifyModule.kt | 17 +++--- .../main/kotlin/org/koin/example/CoffeeApp.kt | 6 +- .../kotlin/org/koin/benchmark/PerfRunner.kt | 28 +++++----- 18 files changed, 88 insertions(+), 160 deletions(-) delete mode 100644 core/koin-core/src/jsMain/kotlin/org/koin/core/time/TimeSource.kt delete mode 100644 core/koin-core/src/jsMain/kotlin/org/koin/mp/PlatformTimeTools.kt delete mode 100644 core/koin-core/src/jvmMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt delete mode 100644 core/koin-core/src/nativeMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt diff --git a/core/koin-core-coroutines/src/jvmTest/kotlin/org/koin/test/PerfsTest.kt b/core/koin-core-coroutines/src/jvmTest/kotlin/org/koin/test/PerfsTest.kt index 28da1bcdc..d2469957b 100644 --- a/core/koin-core-coroutines/src/jvmTest/kotlin/org/koin/test/PerfsTest.kt +++ b/core/koin-core-coroutines/src/jvmTest/kotlin/org/koin/test/PerfsTest.kt @@ -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 { @@ -24,16 +25,17 @@ 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 @@ -41,13 +43,14 @@ class PerfsTest { koin.waitAllStartJobs() } - val timerRun = Timer.start() - koin.get() - koin.get() - koin.get() - koin.get() - timerRun.stop() - println("perf400 - executed in ${timerRun.getTimeInMillis()}") + val injectDuration = measureTime { + koin.get() + koin.get() + koin.get() + koin.get() + } + + println("perf400 - executed in ${injectDuration.inWholeMilliseconds} ms") app.close() } diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/core/Koin.kt b/core/koin-core/src/commonMain/kotlin/org/koin/core/Koin.kt index a6bba688c..0a0eab89f 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/core/Koin.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/core/Koin.kt @@ -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 @@ -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") } } diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/core/KoinApplication.kt b/core/koin-core/src/commonMain/kotlin/org/koin/core/KoinApplication.kt index a03f3cb53..8ae9ff590 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/core/KoinApplication.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/core/KoinApplication.kt @@ -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 @@ -58,9 +58,9 @@ class KoinApplication private constructor() { */ fun modules(modules: List): 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) } diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/core/scope/Scope.kt b/core/koin-core/src/commonMain/kotlin/org/koin/core/scope/Scope.kt index 1cc62f766..32c10ca1b 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/core/scope/Scope.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/core/scope/Scope.kt @@ -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) @@ -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(qualifier, clazz, parameters) - val stop = KoinPlatformTimeTools.getTimeInNanoSeconds() - val duration = (stop - start) / Timer.NANO_TO_MILLI + val (instance, duration) = measureTimedValue { + 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) diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/core/time/Measure.kt b/core/koin-core/src/commonMain/kotlin/org/koin/core/time/Measure.kt index dc0fc6fb4..d0f38d45b 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/core/time/Measure.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/core/time/Measure.kt @@ -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 } @@ -38,11 +40,13 @@ 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 measureDurationForResult(code: () -> T): Pair { val (value, duration) = measureTimedValue(code) return Pair(value, duration) } +@Deprecated("Use measureTimedValue() from kotlin.time instead.") inline fun measureTimedValue(code: () -> T): Pair { val start = KoinPlatformTimeTools.getTimeInNanoSeconds() val value = code() @@ -50,4 +54,5 @@ inline fun measureTimedValue(code: () -> T): Pair { return Pair(value, (stop - start) / Timer.NANO_TO_MILLI) } +@Deprecated("Use kotlin.time.Duration instead.") typealias TimeInMillis = Double diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/core/time/Timer.kt b/core/koin-core/src/commonMain/kotlin/org/koin/core/time/Timer.kt index 984806057..c2820a665 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/core/time/Timer.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/core/time/Timer.kt @@ -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) diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt b/core/koin-core/src/commonMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt index 266bce093..a9c0dc69b 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt @@ -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 } diff --git a/core/koin-core/src/commonTest/kotlin/org/koin/core/SetterInjectTest.kt b/core/koin-core/src/commonTest/kotlin/org/koin/core/SetterInjectTest.kt index dfa00e253..15e1d9d32 100644 --- a/core/koin-core/src/commonTest/kotlin/org/koin/core/SetterInjectTest.kt +++ b/core/koin-core/src/commonTest/kotlin/org/koin/core/SetterInjectTest.kt @@ -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) } @@ -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") } diff --git a/core/koin-core/src/jsMain/kotlin/org/koin/core/time/TimeSource.kt b/core/koin-core/src/jsMain/kotlin/org/koin/core/time/TimeSource.kt deleted file mode 100644 index 13296f6d1..000000000 --- a/core/koin-core/src/jsMain/kotlin/org/koin/core/time/TimeSource.kt +++ /dev/null @@ -1,56 +0,0 @@ -package org.koin.core.time - -import kotlin.js.Date -import kotlin.math.roundToLong - -/** - * JS Time API inspired by current Kotlin JS code - * - * @author Arnaud Giuliani - * @author pubiqq - */ - -interface TimeSource { - fun markNow(): Long -} - -internal fun getTimeSource(): TimeSource { - val isNode = js( - "typeof process !== 'undefined' " + - "&& process.versions " + - "&& !!process.versions.node", - ) as Boolean - - return if (isNode) { - NodeJsHrTimeSource() - } else { - val isPerformanceNowSupported = js("self.performance && !!self.performance.now") as Boolean - if (isPerformanceNowSupported) { - PerformanceNowTimeSource() - } else { - DateNowTimeSource() - } - } -} - -// https://nodejs.org/api/process.html#processhrtimetime -private class NodeJsHrTimeSource : TimeSource { - override fun markNow(): Long { - val (seconds, nanos) = js("process.hrtime()") as Array - return (seconds * 1_000_000_000 + nanos).roundToLong() - } -} - -// https://developer.mozilla.org/en-US/docs/Web/API/Performance/now -private class PerformanceNowTimeSource : TimeSource { - override fun markNow(): Long { - return (js("self.performance.now()") as Double * 1_000_000).roundToLong() - } -} - -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now -private class DateNowTimeSource : TimeSource { - override fun markNow(): Long { - return (Date.now() * 1_000_000).roundToLong() - } -} diff --git a/core/koin-core/src/jsMain/kotlin/org/koin/mp/PlatformTimeTools.kt b/core/koin-core/src/jsMain/kotlin/org/koin/mp/PlatformTimeTools.kt deleted file mode 100644 index de2b8ce97..000000000 --- a/core/koin-core/src/jsMain/kotlin/org/koin/mp/PlatformTimeTools.kt +++ /dev/null @@ -1,10 +0,0 @@ -package org.koin.mp - -import org.koin.core.time.getTimeSource - -actual object KoinPlatformTimeTools { - - actual fun getTimeInNanoSeconds(): Long { - return getTimeSource().markNow() - } -} diff --git a/core/koin-core/src/jvmMain/kotlin/org/koin/core/instance/InstanceBuilder.kt b/core/koin-core/src/jvmMain/kotlin/org/koin/core/instance/InstanceBuilder.kt index 64f43cf44..38116af9f 100644 --- a/core/koin-core/src/jvmMain/kotlin/org/koin/core/instance/InstanceBuilder.kt +++ b/core/koin-core/src/jvmMain/kotlin/org/koin/core/instance/InstanceBuilder.kt @@ -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") @@ -35,20 +35,20 @@ fun Scope.newInstance(kClass: KClass, 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) diff --git a/core/koin-core/src/jvmMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt b/core/koin-core/src/jvmMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt deleted file mode 100644 index 831b0850e..000000000 --- a/core/koin-core/src/jvmMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt +++ /dev/null @@ -1,7 +0,0 @@ -package org.koin.mp - -actual object KoinPlatformTimeTools { - actual fun getTimeInNanoSeconds(): Long { - return System.nanoTime() - } -} diff --git a/core/koin-core/src/jvmTest/kotlin/org/koin/core/instance/CreateAPITest.kt b/core/koin-core/src/jvmTest/kotlin/org/koin/core/instance/CreateAPITest.kt index 3a48d70fd..62481ee41 100644 --- a/core/koin-core/src/jvmTest/kotlin/org/koin/core/instance/CreateAPITest.kt +++ b/core/koin-core/src/jvmTest/kotlin/org/koin/core/instance/CreateAPITest.kt @@ -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 { @@ -24,10 +24,10 @@ class CreateAPITest { ) }.koin - val duration = measureDuration { + val duration = measureTime { koin.get() } - println("create api in $duration ms") + println("create api in ${duration.inWholeMilliseconds} ms") val createKoin = koinApplication { printLogger(Level.DEBUG) @@ -39,10 +39,10 @@ class CreateAPITest { ) }.koin - val createDuration = measureDuration { + val createDuration = measureTime { createKoin.get() } - println("create api in $createDuration ms") + println("create api in ${createDuration.inWholeMilliseconds} ms") } @Test diff --git a/core/koin-core/src/jvmTest/kotlin/org/koin/core/instance/ReflectAPITest.kt b/core/koin-core/src/jvmTest/kotlin/org/koin/core/instance/ReflectAPITest.kt index 9829e03dd..b39296393 100644 --- a/core/koin-core/src/jvmTest/kotlin/org/koin/core/instance/ReflectAPITest.kt +++ b/core/koin-core/src/jvmTest/kotlin/org/koin/core/instance/ReflectAPITest.kt @@ -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 { @@ -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 @@ -48,7 +48,7 @@ class ReflectAPITest { } fun 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 } diff --git a/core/koin-core/src/nativeMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt b/core/koin-core/src/nativeMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt deleted file mode 100644 index 448586cfa..000000000 --- a/core/koin-core/src/nativeMain/kotlin/org/koin/mp/KoinPlatformTimeTools.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.koin.mp - -import kotlin.time.TimeSource - -actual object KoinPlatformTimeTools { - actual fun getTimeInNanoSeconds(): Long { - return TimeSource.Monotonic.markNow().elapsedNow().inWholeNanoseconds - } -} diff --git a/core/koin-test/src/jvmMain/kotlin/org/koin/test/verify/VerifyModule.kt b/core/koin-test/src/jvmMain/kotlin/org/koin/test/verify/VerifyModule.kt index ba2d050b0..520e50bc9 100644 --- a/core/koin-test/src/jvmMain/kotlin/org/koin/test/verify/VerifyModule.kt +++ b/core/koin-test/src/jvmMain/kotlin/org/koin/test/verify/VerifyModule.kt @@ -3,8 +3,8 @@ package org.koin.test.verify import org.koin.core.annotation.KoinExperimentalAPI import org.koin.core.annotation.KoinInternalApi import org.koin.core.module.Module -import org.koin.core.time.Timer import kotlin.reflect.KClass +import kotlin.time.measureTime /** * Make a static verification about all declared classes constructors, to ensure they are all bound to an existing definition @@ -63,16 +63,13 @@ object Verify { * @throws MissingKoinDefinitionException */ fun verify(module: Module, extraTypes: List> = listOf()) { - val timer = Timer.start() - - val verification = Verification(module, extraTypes) - println("Verifying module '$module' ...") + val duration = measureTime { + val verification = Verification(module, extraTypes) + println("Verifying module '$module' ...") // println("- index: ${verification.definitionIndex.size}") - verification.verify() + verification.verify() + } - timer.stop() - val time = - if (timer.getTimeInMillis() < 1000) "${timer.getTimeInMillis()} ms" else "${timer.getTimeInSeconds()} sec" - println("\n[SUCCESS] module '$this' has been verified in $time.") + println("\n[SUCCESS] module '$this' has been verified in $duration.") } } diff --git a/examples/coffee-maker/src/main/kotlin/org/koin/example/CoffeeApp.kt b/examples/coffee-maker/src/main/kotlin/org/koin/example/CoffeeApp.kt index 4d38afdd4..467c41e02 100644 --- a/examples/coffee-maker/src/main/kotlin/org/koin/example/CoffeeApp.kt +++ b/examples/coffee-maker/src/main/kotlin/org/koin/example/CoffeeApp.kt @@ -4,7 +4,7 @@ import org.koin.core.component.KoinComponent import org.koin.core.component.inject import org.koin.core.context.startKoin import org.koin.core.logger.Level -import org.koin.core.time.measureDuration +import kotlin.time.measureTime class CoffeeApp : KoinComponent { val maker: CoffeeMaker by inject() @@ -17,8 +17,8 @@ fun main() { } val coffeeShop = CoffeeApp() - val duration = measureDuration { + val duration = measureTime { coffeeShop.maker.brew() } - println("Got Coffee in $duration ms") + println("Got Coffee in ${duration.inWholeMilliseconds} ms") } \ No newline at end of file diff --git a/examples/jvm-perfs/src/main/kotlin/org/koin/benchmark/PerfRunner.kt b/examples/jvm-perfs/src/main/kotlin/org/koin/benchmark/PerfRunner.kt index 66f1b54ef..af7c7c4b9 100644 --- a/examples/jvm-perfs/src/main/kotlin/org/koin/benchmark/PerfRunner.kt +++ b/examples/jvm-perfs/src/main/kotlin/org/koin/benchmark/PerfRunner.kt @@ -1,36 +1,36 @@ package org.koin.benchmark import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import org.koin.core.Koin -import org.koin.core.annotation.KoinExperimentalAPI -import org.koin.core.time.measureDurationForResult import org.koin.dsl.koinApplication import kotlin.math.roundToInt +import kotlin.time.Duration +import kotlin.time.measureTime +import kotlin.time.measureTimedValue object PerfRunner { suspend fun runAll(scope: CoroutineScope): PerfResult { val results = (1..10).map { i -> withContext(scope.coroutineContext) { runScenario(i) } } - val avgStart = (results.sumOf { it.first } / results.size).round(100) - val avgExec = (results.sumOf { it.second } / results.size).round(1000) + val avgStartInMs = (results.sumOf { it.first.inWholeMilliseconds }.toDouble() / results.size).round(100) + val avgExecInMs = (results.sumOf { it.second.inWholeMilliseconds }.toDouble() / results.size).round(1000) - println("Avg start: $avgStart ms") - println("Avg execution: $avgExec ms") - return PerfResult(avgStart,avgExec) + println("Avg start: $avgStartInMs ms") + println("Avg execution: $avgExecInMs ms") + return PerfResult(avgStartInMs, avgExecInMs) } - @OptIn(KoinExperimentalAPI::class) - fun runScenario(index: Int): Pair { - val (app, duration) = measureDurationForResult { +// @OptIn(KoinExperimentalAPI::class) + fun runScenario(index: Int): Pair { + val (app, duration) = measureTimedValue { koinApplication { modules( perfModule400() ) } } - println("Perf[$index] start in $duration ms") + println("Perf[$index] start in ${duration.inWholeMilliseconds} ms") val koin: Koin = app.koin @@ -38,10 +38,10 @@ object PerfRunner { // koin.awaitAllStartJobs() // } - val (_, executionDuration) = measureDurationForResult { + val executionDuration = measureTime { koinScenario(koin) } - println("Perf[$index] run in $executionDuration ms") + println("Perf[$index] run in ${executionDuration.inWholeMilliseconds} ms") app.close() return Pair(duration, executionDuration) }