generated from Kotlin/multiplatform-library-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Setup `:benchmark` module * Add Koin for benchmark purposes * Add my first benchmark * Configure benchmarks * Add startup benchmark * Add benchmark for small Android dep graph * Configure benchmarks * Increase stat confidence * Fix the Android Common graph * Improve benchmark configs * Improve configuration * Fix config * Improve config * Improve config * Add benchmark workflow * Refactor * Fix the benchmark * Increase the complexity of the DI graph * Complicate the DI graph
- Loading branch information
1 parent
5cc6dd4
commit 63d5b26
Showing
11 changed files
with
352 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: Benchmark | ||
on: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
benchmark: | ||
name: Benchmark | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout GIT | ||
uses: actions/checkout@v4 | ||
|
||
- name: Gradle cache | ||
uses: gradle/actions/setup-gradle@v4 | ||
|
||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
- name: Run benchmarks | ||
run: ./gradlew :benchmark:benchmark | ||
|
||
- name: Upload benchmark results | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: benchmark-results | ||
path: benchmark/build/reports/benchmark/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
plugins { | ||
alias(libs.plugins.kotlinJvm) | ||
alias(libs.plugins.kotlin.allOpen) | ||
alias(libs.plugins.kotlin.benchmark) | ||
} | ||
|
||
allOpen { | ||
annotation("org.openjdk.jmh.annotations.State") | ||
} | ||
|
||
benchmark { | ||
targets { | ||
register("main") | ||
} | ||
} | ||
|
||
benchmark { | ||
targets { | ||
register("jvm") | ||
} | ||
configurations { | ||
named("main") { | ||
warmups = 5 | ||
iterations = 10 | ||
iterationTime = 5 | ||
iterationTimeUnit = "s" | ||
|
||
reportFormat = "csv" | ||
outputTimeUnit = "ms" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(libs.kotlin.benchmark) | ||
implementation(project(":di")) | ||
implementation(libs.koin.core) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package ivy.di.benchmark | ||
|
||
import ivy.di.Di | ||
import ivy.di.benchmark.fixtures.android.* | ||
import kotlinx.benchmark.* | ||
import org.koin.core.context.startKoin | ||
import org.koin.core.context.stopKoin | ||
import org.koin.java.KoinJavaComponent.getKoin | ||
import org.openjdk.jmh.annotations.Level | ||
import java.util.concurrent.TimeUnit | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
class DiComparisonBenchmark { | ||
|
||
private val diGetIterations = 100 | ||
|
||
@TearDown(Level.Invocation) | ||
fun cleanup() { | ||
stopKoin() // Clean up Koin | ||
Di.reset() // Clean up Ivy DI | ||
} | ||
|
||
@Benchmark | ||
fun startIvyDi() { | ||
Di.appScope {} | ||
} | ||
|
||
@Benchmark | ||
fun startKoin() { | ||
startKoin { | ||
modules(emptyList()) | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun androidCommonIvyDi() { | ||
Di.init(AndroidCommonModuleIvyDi) | ||
repeat(diGetIterations) { | ||
Di.get<ArticlesViewModel>() | ||
Di.get<AuthorViewModel>() | ||
Di.get<App>() | ||
Di.get<AppHolder>() | ||
Di.get<AppAppHolder>() | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun androidCommonKoin() { | ||
startKoin { | ||
modules(AndroidCommonModuleKoin) | ||
} | ||
repeat(diGetIterations) { | ||
getKoin().get<ArticlesViewModel>() | ||
getKoin().get<AuthorViewModel>() | ||
getKoin().get<App>() | ||
getKoin().get<AppHolder>() | ||
getKoin().get<AppAppHolder>() | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
benchmark/src/main/kotlin/ivy/di/benchmark/fixtures/android/AndroidCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
@file:Suppress("unused") | ||
|
||
package ivy.di.benchmark.fixtures.android | ||
|
||
interface DispatchersProvider | ||
class AndroidDispatchersProvider : DispatchersProvider | ||
|
||
class Context | ||
interface Logger | ||
class AndroidLogger : Logger | ||
|
||
class HttpClient | ||
class LocalStorage | ||
|
||
class SessionManager(val localStorage: LocalStorage, val logger: Logger) | ||
|
||
class Backstack(val initialRoute: String) | ||
class Navigation( | ||
val backstack: Backstack, | ||
val logger: Logger, | ||
val context: Context, | ||
) | ||
|
||
interface ArticlesDataSource | ||
class RemoteArticlesDataSource( | ||
val httpClient: Lazy<HttpClient>, | ||
val sessionManger: SessionManager | ||
) : ArticlesDataSource | ||
|
||
interface ArticlesRepository | ||
class ArticlesRepositoryImpl( | ||
val dataSource: ArticlesDataSource, | ||
val logger: Logger, | ||
) : ArticlesRepository | ||
|
||
class ArticlesUseCase( | ||
val dispatchers: DispatchersProvider, | ||
val articlesRepo: ArticlesRepository, | ||
val logger: Logger, | ||
) | ||
|
||
class AuthorDataSource(val httpClient: HttpClient) | ||
class AuthorRepository(val dataSource: AuthorDataSource) | ||
|
||
class ArticlesViewModel( | ||
val navigation: Navigation, | ||
val dispatchers: DispatchersProvider, | ||
val articlesUseCase: ArticlesUseCase, | ||
val authorRepository: AuthorRepository, | ||
val logger: Logger, | ||
) | ||
class AuthorViewModel( | ||
val navigation: Navigation, | ||
val dispatchers: DispatchersProvider, | ||
val articlesUseCase: ArticlesUseCase, | ||
val authorRepository: AuthorRepository, | ||
val sessionManger: SessionManager, | ||
val context: Context, | ||
) | ||
|
||
class ContentScreen( | ||
val authorViewModel: AuthorViewModel, | ||
val articlesViewModel: ArticlesViewModel, | ||
val context: Context, | ||
) | ||
|
||
class AuthorScreen( | ||
val authorViewModel: AuthorViewModel, | ||
val context: Context, | ||
) | ||
|
||
class ArticlesScreen( | ||
val articlesViewModel: ArticlesViewModel, | ||
val context: Context, | ||
) | ||
|
||
class App( | ||
val context: Context, | ||
val navigation: Navigation, | ||
val contentScreen: ContentScreen, | ||
val authorScreen: AuthorScreen, | ||
val articlesScreen: ArticlesScreen, | ||
val logger: Logger, | ||
) | ||
|
||
class AppHolder( | ||
val app: App, | ||
val context: Context, | ||
val logger: Logger, | ||
) | ||
|
||
// Complicate the DI graph | ||
class AppAppHolder( | ||
val app: App, | ||
val appHolder: AppHolder, | ||
) |
54 changes: 54 additions & 0 deletions
54
benchmark/src/main/kotlin/ivy/di/benchmark/fixtures/android/AndroidCommonIvyDi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ivy.di.benchmark.fixtures.android | ||
|
||
import ivy.di.Di | ||
import ivy.di.Di.bind | ||
import ivy.di.Di.register | ||
import ivy.di.Di.singleton | ||
import ivy.di.autowire.autoWire | ||
import ivy.di.autowire.autoWireSingleton | ||
|
||
object AndroidCommonModuleIvyDi : Di.Module { | ||
override fun init() = Di.appScope { | ||
autoWireSingleton(::Context) | ||
|
||
autoWire(::AndroidDispatchersProvider) | ||
bind<DispatchersProvider, AndroidDispatchersProvider>() | ||
|
||
autoWire(::AndroidLogger) | ||
bind<Logger, AndroidLogger>() | ||
|
||
singleton { HttpClient() } | ||
autoWire(::LocalStorage) | ||
|
||
singleton { Backstack("/") } | ||
autoWireSingleton(::Navigation) | ||
|
||
autoWireSingleton(::SessionManager) | ||
|
||
register<ArticlesDataSource> { | ||
RemoteArticlesDataSource( | ||
httpClient = Di.getLazy(), | ||
sessionManger = Di.get() | ||
) | ||
} | ||
|
||
autoWire(::ArticlesRepositoryImpl) | ||
bind<ArticlesRepository, ArticlesRepositoryImpl>() | ||
|
||
autoWire(::ArticlesUseCase) | ||
|
||
autoWire(::AuthorDataSource) | ||
autoWire(::AuthorRepository) | ||
|
||
autoWire(::ArticlesViewModel) | ||
autoWire(::AuthorViewModel) | ||
|
||
autoWire(::ContentScreen) | ||
autoWire(::ArticlesScreen) | ||
autoWire(::AuthorScreen) | ||
|
||
autoWire(::App) | ||
autoWireSingleton(::AppHolder) | ||
autoWire(::AppAppHolder) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
benchmark/src/main/kotlin/ivy/di/benchmark/fixtures/android/AndroidCommonKoin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ivy.di.benchmark.fixtures.android | ||
|
||
import org.koin.core.module.dsl.bind | ||
import org.koin.core.module.dsl.factoryOf | ||
import org.koin.core.module.dsl.singleOf | ||
import org.koin.dsl.module | ||
|
||
val AndroidCommonModuleKoin = module { | ||
singleOf(::Context) | ||
|
||
factoryOf(::AndroidDispatchersProvider) { bind<DispatchersProvider>() } | ||
|
||
factoryOf(::AndroidLogger) { bind<Logger>() } | ||
|
||
single { HttpClient() } | ||
factoryOf(::LocalStorage) | ||
|
||
single { Backstack("/") } | ||
singleOf(::Navigation) | ||
|
||
singleOf(::SessionManager) | ||
|
||
factory<ArticlesDataSource> { | ||
RemoteArticlesDataSource( | ||
httpClient = lazy { get() }, | ||
sessionManger = get() | ||
) | ||
} | ||
|
||
singleOf(::ArticlesRepositoryImpl) { bind<ArticlesRepository>() } | ||
|
||
singleOf(::ArticlesUseCase) | ||
|
||
factoryOf(::AuthorDataSource) | ||
factoryOf(::AuthorRepository) | ||
|
||
factoryOf(::ArticlesViewModel) | ||
factoryOf(::AuthorViewModel) | ||
|
||
singleOf(::ContentScreen) | ||
singleOf(::ArticlesScreen) | ||
singleOf(::AuthorScreen) | ||
|
||
factoryOf(::App) | ||
singleOf(::AppHolder) | ||
factoryOf(::AppAppHolder) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
plugins { | ||
alias(libs.plugins.androidLibrary) apply false | ||
alias(libs.plugins.kotlinMultiplatform) apply false | ||
alias(libs.plugins.kotlinJvm) apply false | ||
alias(libs.plugins.publish) apply false | ||
alias(libs.plugins.kotlin.allOpen) apply false | ||
alias(libs.plugins.kotlin.benchmark) apply false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.