-
Notifications
You must be signed in to change notification settings - Fork 45
/
InjectionTest.kt
170 lines (155 loc) · 6.3 KB
/
InjectionTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.sloydev.dependencyinjectionperformance
import android.os.Build
import com.sloydev.dependencyinjectionperformance.custom.DIContainer
import com.sloydev.dependencyinjectionperformance.custom.customJavaModule
import com.sloydev.dependencyinjectionperformance.custom.customKotlinModule
import com.sloydev.dependencyinjectionperformance.dagger2.DaggerJavaDaggerComponent
import com.sloydev.dependencyinjectionperformance.dagger2.DaggerKotlinDaggerComponent
import com.sloydev.dependencyinjectionperformance.dagger2.JavaDaggerComponent
import com.sloydev.dependencyinjectionperformance.dagger2.KotlinDaggerComponent
import com.sloydev.dependencyinjectionperformance.katana.katanaJavaModule
import com.sloydev.dependencyinjectionperformance.katana.katanaKotlinModule
import com.sloydev.dependencyinjectionperformance.kodein.kodeinKotlinModule
import com.sloydev.dependencyinjectionperformance.kodein.kodeinJavaModule
import com.sloydev.dependencyinjectionperformance.koin.koinJavaModule
import com.sloydev.dependencyinjectionperformance.koin.koinKotlinModule
import org.kodein.di.Kodein
import org.kodein.di.direct
import org.kodein.di.erased.instance
import org.koin.core.KoinComponent
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.core.get
import org.rewedigital.katana.Component
import org.rewedigital.katana.Katana
import org.rewedigital.katana.android.environment.AndroidEnvironmentContext
import org.rewedigital.katana.android.environment.AndroidEnvironmentContext.Profile.SPEED
import javax.inject.Inject
class InjectionTest : KoinComponent {
private val kotlinDaggerTest = KotlinDaggerTest()
private val javaDaggerTest = JavaDaggerTest()
private val rounds = 100
fun runTests(): List<LibraryResult> {
val results = listOf(
koinTest(),
kodeinTest(),
katanaTest(),
customTest(),
daggerTest()
)
reportMarkdown(results)
return results
}
private fun reportMarkdown(results: List<LibraryResult>) {
log("Done!")
log(" ")
log("${Build.BRAND} ${Build.DEVICE} with Android ${Build.VERSION.RELEASE}")
log(" ")
log("Library | Setup Kotlin | Setup Java | Inject Kotlin | Inject Java")
log("--- | ---:| ---:| ---:| ---:")
results.forEach {
log("**${it.injectorName}** | ${it[Variant.KOTLIN].startupTime.median().format()} | ${it[Variant.JAVA].startupTime.median().format()} | ${it[Variant.KOTLIN].injectionTime.median().format()} | ${it[Variant.JAVA].injectionTime.median().format()}")
}
}
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)
}
private fun koinTest(): LibraryResult {
log("Running Koin...")
return LibraryResult("Koin", mapOf(
Variant.KOTLIN to runTest(
setup = {
startKoin {
modules(koinKotlinModule)
}
},
test = { get<Fib8>() },
teardown = { stopKoin() }
),
Variant.JAVA to runTest(
setup = {
startKoin {
modules(koinJavaModule)
}
},
test = { get<FibonacciJava.Fib8>() },
teardown = { stopKoin() }
)
))
}
private fun kodeinTest(): LibraryResult {
log("Running Kodein...")
lateinit var kodein: Kodein
return LibraryResult("Kodein", mapOf(
Variant.KOTLIN to runTest(
setup = { kodein = Kodein { import(kodeinKotlinModule) } },
test = { kodein.direct.instance<Fib8>() }
),
Variant.JAVA to runTest(
setup = { kodein = Kodein { import(kodeinJavaModule) } },
test = { kodein.direct.instance<FibonacciJava.Fib8>() }
)
))
}
private fun katanaTest(): LibraryResult {
log("Running Katana...")
Katana.environmentContext = AndroidEnvironmentContext(profile = SPEED)
lateinit var component: Component
return LibraryResult("Katana", mapOf(
Variant.KOTLIN to runTest(
setup = { component = Component(modules = listOf(katanaKotlinModule)) },
test = { component.injectNow<Fib8>() }
),
Variant.JAVA to runTest(
setup = { component = Component(modules = listOf(katanaJavaModule)) },
test = { component.injectNow<FibonacciJava.Fib8>() }
)
))
}
private fun customTest(): LibraryResult {
log("Running Custom...")
return LibraryResult("Custom", mapOf(
Variant.KOTLIN to runTest(
setup = { DIContainer.loadModule(customKotlinModule) },
test = { DIContainer.get<Fib8>() },
teardown = { DIContainer.unloadModules() }
),
Variant.JAVA to runTest(
setup = { DIContainer.loadModule(customJavaModule) },
test = { DIContainer.get<FibonacciJava.Fib8>() },
teardown = { DIContainer.unloadModules() }
)
))
}
private fun daggerTest(): LibraryResult {
log("Running Dagger...")
lateinit var kotlinComponent: KotlinDaggerComponent
lateinit var javaComponent: JavaDaggerComponent
return LibraryResult("Dagger", mapOf(
Variant.KOTLIN to runTest(
setup = { kotlinComponent = DaggerKotlinDaggerComponent.create() },
test = { kotlinComponent.inject(kotlinDaggerTest) }
),
Variant.JAVA to runTest(
setup = { javaComponent = DaggerJavaDaggerComponent.create() },
test = { javaComponent.inject(javaDaggerTest) }
)
))
}
class KotlinDaggerTest {
@Inject
lateinit var daggerFib8: Fib8
}
class JavaDaggerTest {
@Inject
lateinit var daggerFib8: FibonacciJava.Fib8
}
}