Skip to content

Commit 50b2dd9

Browse files
committed
Setup benchmarks infrastructure
1 parent 6a5adf0 commit 50b2dd9

File tree

12 files changed

+182
-115
lines changed

12 files changed

+182
-115
lines changed

build-logic/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ plugins {
88

99
dependencies {
1010
implementation(libs.kotlin.gradle.plugin)
11+
implementation(libs.kotlin.allopen.gradle.plugin)
1112
implementation(libs.kotlin.dokka.gradle.plugin)
13+
implementation(libs.kotlinx.benchmark.gradle.plugin)
1214
implementation(libs.android.gradle.plugin)
1315
implementation(libs.maven.publish.gradle.plugin)
1416
implementation(libs.apache.commons.compress)

build-logic/src/main/kotlin/ckbuild.multiplatform-base.gradle.kts

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@
22
* Copyright (c) 2023-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
import ckbuild.tests.*
6-
import com.android.build.gradle.internal.tasks.*
75
import org.jetbrains.kotlin.gradle.*
86
import org.jetbrains.kotlin.gradle.dsl.*
9-
import org.jetbrains.kotlin.gradle.plugin.*
10-
import org.jetbrains.kotlin.gradle.plugin.mpp.*
11-
import org.jetbrains.kotlin.gradle.targets.js.ir.*
12-
import org.jetbrains.kotlin.gradle.targets.js.testing.*
137
import org.jetbrains.kotlin.gradle.targets.jvm.*
14-
import org.jetbrains.kotlin.gradle.targets.jvm.tasks.*
15-
import org.jetbrains.kotlin.gradle.targets.native.tasks.*
168
import org.jetbrains.kotlin.gradle.tasks.*
179

1810
plugins {
@@ -33,98 +25,17 @@ kotlin {
3325
freeCompilerArgs.add("-Xrender-internal-diagnostic-names")
3426
}
3527

36-
// for some reason just applying `kotlin-test` doesn't work for JVM - revisit after Kotlin 2.0
37-
sourceSets.configureEach {
38-
when (name) {
39-
"commonTest" -> "test"
40-
"jvmTest" -> "test-junit"
41-
else -> null
42-
}?.let { testDependency ->
43-
dependencies {
44-
implementation(kotlin(testDependency))
45-
}
46-
}
47-
}
48-
4928
targets.withType<KotlinJvmTarget>().configureEach {
5029
compilerOptions {
5130
jvmDefault = JvmDefaultMode.NO_COMPATIBILITY
5231
}
5332
}
54-
55-
// revisit JS block after WASM support
56-
targets.withType<KotlinJsIrTarget>().configureEach {
57-
whenBrowserConfigured {
58-
testTask {
59-
useKarma {
60-
useConfigDirectory(rootDir.resolve("karma.config.d"))
61-
useChromeHeadless()
62-
}
63-
}
64-
}
65-
// not used/supported by wasm
66-
if (platformType == KotlinPlatformType.js) whenNodejsConfigured {
67-
testTask {
68-
useMocha {
69-
timeout = "6000s"
70-
}
71-
}
72-
}
73-
}
74-
75-
//setup tests running in RELEASE mode
76-
targets.withType<KotlinNativeTarget>().configureEach {
77-
binaries.test(listOf(NativeBuildType.RELEASE))
78-
}
79-
targets.withType<KotlinNativeTargetWithTests<*>>().configureEach {
80-
testRuns.create("releaseTest") {
81-
setExecutionSourceFrom(binaries.getTest(NativeBuildType.RELEASE))
82-
}
83-
}
84-
}
85-
86-
// for CI mainly
87-
88-
registerTestAggregationTask(
89-
name = "wasmTest",
90-
taskDependencies = { tasks.withType<KotlinJsTest>().matching { it.compilation.platformType == KotlinPlatformType.wasm } },
91-
targetFilter = { it.platformType == KotlinPlatformType.wasm }
92-
)
93-
94-
registerTestAggregationTask(
95-
name = "jvmAllTest",
96-
taskDependencies = { tasks.withType<KotlinJvmTest>() },
97-
targetFilter = { it.platformType == KotlinPlatformType.jvm }
98-
)
99-
100-
registerTestAggregationTask(
101-
name = "nativeTest",
102-
taskDependencies = { tasks.withType<KotlinNativeTest>().matching { it.enabled } },
103-
targetFilter = { it.platformType == KotlinPlatformType.native }
104-
)
105-
106-
listOf("ios", "watchos", "tvos", "macos").forEach { targetGroup ->
107-
registerTestAggregationTask(
108-
name = "${targetGroup}Test",
109-
taskDependencies = {
110-
tasks.withType<KotlinNativeTest>().matching {
111-
it.enabled && it.name.startsWith(targetGroup, ignoreCase = true)
112-
}
113-
},
114-
targetFilter = {
115-
it.platformType == KotlinPlatformType.native && it.name.startsWith(targetGroup, ignoreCase = true)
116-
}
117-
)
11833
}
11934

12035
tasks.register("linkAll") {
12136
dependsOn(tasks.withType<KotlinNativeLink>())
12237
}
12338

124-
if (providers.gradleProperty("ckbuild.skipTestTasks").map(String::toBoolean).getOrElse(false)) {
125-
tasks.matching { it is AbstractTestTask || it is AndroidTestTask }.configureEach { onlyIf { false } }
126-
}
127-
12839
if (providers.gradleProperty("ckbuild.skipLinkTasks").map(String::toBoolean).getOrElse(false)) {
12940
tasks.withType<KotlinNativeLink>().configureEach { onlyIf { false } }
13041
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2023-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import ckbuild.*
6+
import kotlinx.benchmark.gradle.*
7+
import org.jetbrains.kotlin.gradle.plugin.*
8+
9+
plugins {
10+
id("ckbuild.multiplatform-base")
11+
id("org.jetbrains.kotlinx.benchmark")
12+
kotlin("plugin.allopen")
13+
}
14+
15+
allOpen {
16+
annotation("org.openjdk.jmh.annotations.State")
17+
}
18+
19+
kotlin {
20+
// register benchmark targets - same as kotlin targets
21+
targets.all {
22+
if (platformType != KotlinPlatformType.common) benchmark.targets.register(name)
23+
}
24+
25+
sourceSets {
26+
commonMain.dependencies {
27+
implementation(project.versionCatalogLib("kotlinx-benchmark-runtime"))
28+
}
29+
}
30+
}
31+
32+
benchmark {
33+
targets.configureEach {
34+
if (this is JvmBenchmarkTarget) jmhVersion = "1.37"
35+
}
36+
}

build-logic/src/main/kotlin/ckbuild.multiplatform-library.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.jetbrains.kotlin.gradle.dsl.abi.*
88

99
plugins {
1010
id("ckbuild.multiplatform-base")
11+
id("ckbuild.multiplatform-tests")
1112
id("ckbuild.publication")
1213
id("ckbuild.documentation")
1314
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2023-2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import ckbuild.tests.*
6+
import com.android.build.gradle.internal.tasks.*
7+
import org.jetbrains.kotlin.gradle.*
8+
import org.jetbrains.kotlin.gradle.plugin.*
9+
import org.jetbrains.kotlin.gradle.plugin.mpp.*
10+
import org.jetbrains.kotlin.gradle.targets.js.ir.*
11+
import org.jetbrains.kotlin.gradle.targets.js.testing.*
12+
import org.jetbrains.kotlin.gradle.targets.jvm.tasks.*
13+
import org.jetbrains.kotlin.gradle.targets.native.tasks.*
14+
15+
plugins {
16+
id("ckbuild.multiplatform-base")
17+
}
18+
19+
@OptIn(ExperimentalKotlinGradlePluginApi::class)
20+
kotlin {
21+
// just applying `kotlin-test` doesn't work for JVM if there are multiple test tasks (like when we test on different JDKs)
22+
sourceSets.configureEach {
23+
when (name) {
24+
"commonTest" -> "test"
25+
"jvmTest" -> "test-junit"
26+
else -> null
27+
}?.let { testDependency ->
28+
dependencies {
29+
implementation(kotlin(testDependency))
30+
}
31+
}
32+
}
33+
34+
targets.withType<KotlinJsIrTarget>().configureEach {
35+
whenBrowserConfigured {
36+
testTask {
37+
useKarma {
38+
useConfigDirectory(rootDir.resolve("karma.config.d"))
39+
useChromeHeadless()
40+
}
41+
}
42+
}
43+
// not used/supported by wasm
44+
if (platformType == KotlinPlatformType.js) whenNodejsConfigured {
45+
testTask {
46+
useMocha {
47+
timeout = "6000s"
48+
}
49+
}
50+
}
51+
}
52+
53+
// setup tests running in RELEASE mode
54+
targets.withType<KotlinNativeTarget>().configureEach {
55+
binaries.test(listOf(NativeBuildType.RELEASE))
56+
}
57+
targets.withType<KotlinNativeTargetWithTests<*>>().configureEach {
58+
testRuns.create("releaseTest") {
59+
setExecutionSourceFrom(binaries.getTest(NativeBuildType.RELEASE))
60+
}
61+
}
62+
}
63+
64+
// for CI mainly
65+
66+
registerTestAggregationTask(
67+
name = "wasmTest",
68+
taskDependencies = { tasks.withType<KotlinJsTest>().matching { it.compilation.platformType == KotlinPlatformType.wasm } },
69+
targetFilter = { it.platformType == KotlinPlatformType.wasm }
70+
)
71+
72+
registerTestAggregationTask(
73+
name = "jvmAllTest",
74+
taskDependencies = { tasks.withType<KotlinJvmTest>() },
75+
targetFilter = { it.platformType == KotlinPlatformType.jvm }
76+
)
77+
78+
registerTestAggregationTask(
79+
name = "nativeTest",
80+
taskDependencies = { tasks.withType<KotlinNativeTest>().matching { it.enabled } },
81+
targetFilter = { it.platformType == KotlinPlatformType.native }
82+
)
83+
84+
listOf("ios", "watchos", "tvos", "macos").forEach { targetGroup ->
85+
registerTestAggregationTask(
86+
name = "${targetGroup}Test",
87+
taskDependencies = {
88+
tasks.withType<KotlinNativeTest>().matching {
89+
it.enabled && it.name.startsWith(targetGroup, ignoreCase = true)
90+
}
91+
},
92+
targetFilter = {
93+
it.platformType == KotlinPlatformType.native && it.name.startsWith(targetGroup, ignoreCase = true)
94+
}
95+
)
96+
}
97+
98+
if (providers.gradleProperty("ckbuild.skipTestTasks").map(String::toBoolean).getOrElse(false)) {
99+
tasks.matching { it is AbstractTestTask || it is AndroidTestTask }.configureEach { onlyIf { false } }
100+
}

build-logic/src/main/kotlin/ckbuild/targets.kt

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package ckbuild
88

99
import com.android.build.api.dsl.*
10-
import org.gradle.api.artifacts.*
1110
import org.gradle.jvm.toolchain.*
1211
import org.gradle.kotlin.dsl.*
1312
import org.jetbrains.kotlin.gradle.*
@@ -22,6 +21,12 @@ fun KotlinMultiplatformExtension.allTargets(
2221
if (supportsWasmWasi) wasmWasiTarget()
2322
}
2423

24+
fun KotlinMultiplatformExtension.allBenchmarkTargets() {
25+
jvmTarget(jdkAdditionalTestVersions = emptySet())
26+
desktopTargets()
27+
webTargets(supportsBrowser = false)
28+
}
29+
2530
fun KotlinMultiplatformExtension.appleTargets(
2631
// not supported by Swift anymore -> not supported by CryptoKit
2732
supportsWatchosArm32: Boolean = true,
@@ -64,18 +69,22 @@ fun KotlinMultiplatformExtension.nativeTargets() {
6469
androidNativeArm32()
6570
}
6671

67-
fun KotlinMultiplatformExtension.jsTarget() {
72+
fun KotlinMultiplatformExtension.jsTarget(
73+
supportsBrowser: Boolean = true,
74+
) {
6875
js {
6976
nodejs()
70-
browser()
77+
if (supportsBrowser) browser()
7178
}
7279
}
7380

7481
@OptIn(ExperimentalWasmDsl::class)
75-
fun KotlinMultiplatformExtension.wasmJsTarget() {
82+
fun KotlinMultiplatformExtension.wasmJsTarget(
83+
supportsBrowser: Boolean = true,
84+
) {
7685
wasmJs {
7786
nodejs()
78-
browser()
87+
if (supportsBrowser) browser()
7988
}
8089
}
8190

@@ -86,9 +95,11 @@ fun KotlinMultiplatformExtension.wasmWasiTarget() {
8695
}
8796
}
8897

89-
fun KotlinMultiplatformExtension.webTargets() {
90-
jsTarget()
91-
wasmJsTarget()
98+
fun KotlinMultiplatformExtension.webTargets(
99+
supportsBrowser: Boolean = true,
100+
) {
101+
jsTarget(supportsBrowser = supportsBrowser)
102+
wasmJsTarget(supportsBrowser = supportsBrowser)
92103
}
93104

94105
fun KotlinMultiplatformExtension.jvmTarget(
@@ -130,11 +141,9 @@ fun KotlinMultiplatformExtension.androidLibraryTarget() {
130141
}
131142
}
132143

133-
val versionCatalogs = project.extensions.getByName<VersionCatalogsExtension>("versionCatalogs")
134-
135144
sourceSets.named("androidDeviceTest") {
136145
dependencies {
137-
implementation(versionCatalogs.named("libs").findLibrary("androidx-test").get())
146+
implementation(project.versionCatalogLib("androidx-test"))
138147
}
139148
}
140149

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package ckbuild
6+
7+
import org.gradle.api.*
8+
import org.gradle.api.artifacts.*
9+
import org.gradle.api.provider.*
10+
import org.gradle.kotlin.dsl.*
11+
12+
val Project.libsCatalog: VersionCatalog get() = extensions.getByName<VersionCatalogsExtension>("versionCatalogs").named("libs")
13+
14+
fun Project.versionCatalogLib(alias: String): Provider<MinimalExternalModuleDependency> = libsCatalog.findLibrary(alias).get()

build-settings/src/main/kotlin/cksettings.repositories.settings.gradle.kts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@ pluginManagement {
1313
includeGroupAndSubgroups("androidx")
1414
}
1515
}
16-
gradlePluginPortal {
17-
content {
18-
includeGroupAndSubgroups("com.gradle")
19-
includeGroupAndSubgroups("org.gradle")
20-
}
21-
}
2216
mavenCentral()
17+
gradlePluginPortal()
2318
}
2419
}
2520

@@ -32,12 +27,7 @@ dependencyResolutionManagement {
3227
includeGroupAndSubgroups("androidx")
3328
}
3429
}
35-
gradlePluginPortal {
36-
content {
37-
includeGroupAndSubgroups("com.gradle")
38-
includeGroupAndSubgroups("org.gradle")
39-
}
40-
}
4130
mavenCentral()
31+
gradlePluginPortal()
4232
}
4333
}

0 commit comments

Comments
 (0)