Skip to content

Commit

Permalink
Add Kodein to the benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Dec 15, 2024
1 parent 60ac327 commit c35b864
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 5 deletions.
1 change: 1 addition & 0 deletions benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ dependencies {
implementation(libs.kotlin.benchmark)
implementation(project(":di"))
implementation(libs.koin.core)
implementation(libs.kodein)
}
57 changes: 57 additions & 0 deletions benchmark/src/main/kotlin/ivy/di/benchmark/DiBenchmark.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ import ivy.di.benchmark.fixtures.modules.ivy.AndroidGraphIvyDi
import ivy.di.benchmark.fixtures.modules.ivy.BeGraphIvyDi
import ivy.di.benchmark.fixtures.modules.ivy.CommonGraphIvyDi
import ivy.di.benchmark.fixtures.modules.ivy.ComplexGraphIvyDi
import ivy.di.benchmark.fixtures.modules.kodein.AndroidGraphKodein
import ivy.di.benchmark.fixtures.modules.kodein.BeGraphKodein
import ivy.di.benchmark.fixtures.modules.kodein.CommonGraphKodein
import ivy.di.benchmark.fixtures.modules.kodein.ComplexGraphKodein
import ivy.di.benchmark.fixtures.modules.koin.AndroidGraphKoin
import ivy.di.benchmark.fixtures.modules.koin.BeGraphKoin
import ivy.di.benchmark.fixtures.modules.koin.CommonGraphKoin
import ivy.di.benchmark.fixtures.modules.koin.ComplexGraphKoin
import kotlinx.benchmark.*
import org.kodein.di.DI
import org.kodein.di.direct
import org.kodein.di.instance
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.java.KoinJavaComponent.getKoin
Expand All @@ -28,10 +35,15 @@ class DiComparisonBenchmark {
private val mediumGraphGets = 50
private val complexGraphGets = 200

private lateinit var kodein: DI

@TearDown(Level.Invocation)
fun cleanup() {
stopKoin() // Clean up Koin
Di.reset() // Clean up Ivy DI
if (::kodein.isInitialized) {
kodein = DI {} // Reset Kodein
}
}

@Benchmark
Expand All @@ -46,6 +58,11 @@ class DiComparisonBenchmark {
}
}

@Benchmark
fun startKodein() {
kodein = DI {}
}

@Benchmark
fun smallGraphIvyDI() {
Di.init(CommonGraphIvyDi, AndroidGraphIvyDi)
Expand All @@ -64,6 +81,17 @@ class DiComparisonBenchmark {
}
}

@Benchmark
fun smallGraphKodein() {
kodein = DI {
import(CommonGraphKodein)
import(AndroidGraphKodein)
}
repeat(smallGraphGets) {
kodein.direct.instance<App>()
}
}

@Benchmark
fun mediumGraphIvyDI() {
Di.init(CommonGraphIvyDi, AndroidGraphIvyDi, BeGraphIvyDi)
Expand All @@ -84,6 +112,19 @@ class DiComparisonBenchmark {
}
}

@Benchmark
fun mediumGraphKodein() {
kodein = DI {
import(CommonGraphKodein)
import(AndroidGraphKodein)
import(BeGraphKodein)
}
repeat(mediumGraphGets) {
kodein.direct.instance<App>()
kodein.direct.instance<ServerApp>()
}
}

@Benchmark
fun complexGraphIvyDI() {
Di.init(
Expand Down Expand Up @@ -111,6 +152,19 @@ class DiComparisonBenchmark {
getKoin().get<MultiHolderFinal>()
}
}

@Benchmark
fun complexGraphKodein() {
kodein = DI {
import(CommonGraphKodein)
import(AndroidGraphKodein)
import(BeGraphKodein)
import(ComplexGraphKodein)
}
repeat(complexGraphGets) {
kodein.direct.instance<MultiHolderFinal>()
}
}
}

fun main() {
Expand All @@ -119,14 +173,17 @@ fun main() {
cleanup()
smallGraphIvyDI()
smallGraphKoin()
smallGraphKodein()

cleanup()
mediumGraphIvyDI()
mediumGraphKoin()
mediumGraphKodein()

cleanup()
complexGraphIvyDI()
complexGraphKoin()
complexGraphKodein()
}
println("Correctness ensured.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ object AndroidGraphIvyDi : Di.Module {
override fun init() = Di.appScope {
autoWireSingleton(::Context)

autoWire(::RealDispatchersProvider)
bind<DispatchersProvider, RealDispatchersProvider>()

autoWire(::LocalStorage)

singleton { Backstack("/") }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ivy.di.benchmark.fixtures.modules.kodein

import ivy.di.benchmark.fixtures.*
import org.kodein.di.*

val AndroidGraphKodein = DI.Module(name = "AndroidGraphKodein") {
// Singletons
bind<Context>() with singleton { Context() }
bind<Backstack>() with singleton { Backstack("/") }
bind<Navigation>() with singleton { Navigation(instance(), instance(), instance()) }
bind<SessionManager>() with singleton { SessionManager(instance(), instance()) }
bind<ArticlesUseCase>() with singleton { ArticlesUseCase(instance(), instance(), instance()) }
bind<ContentScreen>() with singleton { ContentScreen(instance(), instance(), instance()) }
bind<ArticlesScreen>() with singleton { ArticlesScreen(instance(), instance()) }
bind<AuthorScreen>() with singleton { AuthorScreen(instance(), instance()) }

// Factories
bind<LocalStorage>() with provider { LocalStorage() }
bind<ArticlesDataSource>() with factory {
RemoteArticlesDataSource(
httpClient = lazy { instance<HttpClient>() },
sessionManger = instance()
)
}
bind<ArticlesRepository>() with singleton { ArticlesRepositoryImpl(instance(), instance()) }
bind<AuthorDataSource>() with provider { AuthorDataSource(instance()) }
bind<AuthorRepository>() with provider { AuthorRepository(instance()) }
bind<ArticlesViewModel>() with provider {
ArticlesViewModel(instance(), instance(), instance(), instance(), instance())
}
bind<AuthorViewModel>() with provider {
AuthorViewModel(instance(), instance(), instance(), instance(), instance(), instance())
}

// App factory
bind<App>() with provider {
App(instance(), instance(), instance(), instance(), instance(), instance())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ivy.di.benchmark.fixtures.modules.kodein

import ivy.di.benchmark.fixtures.*
import org.kodein.di.*

val BeGraphKodein = DI.Module(name = "BeGraphKodein") {
// UrlProvider
bind<UrlProvider>() with provider { AwsUrlProvider() }

// Environment
bind<Environment>() with singleton { RealEnvironment() }

// ServerConfig
bind<ServerConfig>() with provider { ServerConfig(instance()) }

// KtorApp
bind<KtorApp>() with singleton { KtorApp() }

// RateLimiter
bind<RateLimiter>() with singleton { RateLimiter(instance()) }

// Database
bind<Database>() with singleton { Database(instance(), instance()) }

// Auth-related bindings
bind<AuthRepository>() with provider { AuthRepository(instance()) }
bind<GoogleLoginUseCase>() with provider { GoogleLoginUseCase(instance(), instance()) }
bind<AuthService>() with provider { AuthService(instance(), instance()) }
bind<AuthApi>() with provider { AuthApi(instance(), instance(), instance()) }

// Cars-related bindings
bind<CarsRepository>() with provider { CarsRepository(instance(), instance()) }
bind<CarsService>() with provider { CarsService(instance(), instance()) }
bind<CarsApi>() with provider { CarsApi(instance()) }

// Apis
bind<Apis>() with provider { Apis(instance(), instance(), instance()) }

// ServerApp
bind<ServerApp>() with provider { ServerApp(instance(), instance(), instance(), instance()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ivy.di.benchmark.fixtures.modules.kodein

import ivy.di.benchmark.fixtures.*
import org.kodein.di.*

val CommonGraphKodein = DI.Module(name = "CommonGraphKodein") {
bind<DispatchersProvider>() with provider { RealDispatchersProvider() }
bind<Logger>() with provider { LoggerImpl() }
bind<Json>() with singleton { Json() }
bind<Serialization>() with provider { KotlinXSerialization(instance()) }
bind<HttpClient>() with singleton { HttpClient(instance(), instance(), instance()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ivy.di.benchmark.fixtures.modules.kodein

import ivy.di.benchmark.fixtures.*
import org.kodein.di.*

val ComplexGraphKodein = DI.Module(name = "ComplexGraphKodein") {
// Holder bindings
bind<Holder1>() with factory { Holder1(instance(), instance()) }
bind<Holder2>() with singleton { Holder2(instance(), instance(), instance()) }
bind<Holder3>() with factory { Holder3(instance(), instance()) }
bind<Holder4>() with singleton { Holder4(instance(), instance(), instance()) }
bind<Holder5>() with factory { Holder5(instance(), instance(), instance(), instance()) }
bind<Holder6>() with singleton { Holder6(instance(), instance(), instance(), instance(), instance()) }
bind<Holder7>() with factory {
Holder7(instance(), instance(), instance(), instance(), instance(), instance())
}
bind<Holder8>() with singleton {
Holder8(instance(), instance(), instance(), instance(), instance(), instance(), instance())
}
bind<Holder9>() with factory {
Holder9(instance(), instance(), instance(), instance(), instance(), instance(), instance(), instance())
}
bind<Holder10>() with singleton {
Holder10(
instance(), instance(), instance(), instance(), instance(),
instance(), instance(), instance(), instance(),
)
}

// MultiHolder bindings
bind<MultiHolder>() with factory {
MultiHolderImpl(
instance(), instance(), instance(),
instance(), instance(), instance(),
instance(), instance(), instance(),
instance(), instance(), instance(),
instance(), instance()
)
}
bind<MultiHolderFinal>() with factory {
MultiHolderFinal(instance(), instance(), instance(), instance(), instance())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import org.koin.dsl.module
val AndroidGraphKoin = module {
singleOf(::Context)

factoryOf(::RealDispatchersProvider) { bind<DispatchersProvider>() }

factoryOf(::LocalStorage)

single { Backstack("/") }
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ android-minSdk = "24"
android-compileSdk = "34"
kotest = "5.9.1"
kotlin-benchmark = "0.4.13"
kodein = "7.23.1"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
kotlin-benchmark = { module = "org.jetbrains.kotlinx:kotlinx-benchmark-runtime", version.ref = "kotlin-benchmark" }
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
kodein = { module = "org.kodein.di:kodein-di", version.ref = "kodein" }

[bundles]
test = [
Expand Down

0 comments on commit c35b864

Please sign in to comment.