Skip to content

Commit 361596a

Browse files
wldehAbduqodiri Qurbonzoda
authored andcommitted
Introduce errors on incorrect target (#142)
1 parent 8d2afb6 commit 361596a

File tree

4 files changed

+170
-1
lines changed

4 files changed

+170
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package kotlinx.benchmark.integration
2+
3+
class KotlinConfiguration {
4+
5+
data class Target(val type: String, val name: String, val compiler: String, val environment: String)
6+
7+
private val targets = mutableListOf<Target>()
8+
9+
fun wasm(name: String, configuration: WasmTarget.() -> Unit) {
10+
val wasmTarget = WasmTarget(name)
11+
wasmTarget.configuration()
12+
targets.add(Target("wasm", wasmTarget.name, "", wasmTarget.environment))
13+
}
14+
15+
fun js(name: String, compiler: String = IR, configuration: JsTarget.() -> Unit) {
16+
val jsTarget = JsTarget(name, compiler)
17+
jsTarget.configuration()
18+
targets.add(Target("js", jsTarget.name, jsTarget.compiler, jsTarget.environment))
19+
}
20+
21+
fun lines(): List<String> {
22+
return targets.map {
23+
"${it.type}('${it.name}', ${it.compiler}) { ${it.environment}() }"
24+
}
25+
}
26+
27+
class WasmTarget(val name: String) {
28+
var environment: String = ""
29+
30+
fun nodejs() {
31+
environment = "nodejs"
32+
}
33+
fun browser() {
34+
environment = "browser"
35+
}
36+
}
37+
38+
class JsTarget(val name: String, val compiler: String) {
39+
var environment: String = ""
40+
41+
fun d8() {
42+
environment = "d8"
43+
}
44+
fun nodejs() {
45+
environment = "nodejs"
46+
}
47+
fun browser() {
48+
environment = "browser"
49+
}
50+
}
51+
52+
companion object {
53+
const val IR = "'IR'"
54+
const val LEGACY = "'LEGACY'"
55+
}
56+
}

integration/src/main/kotlin/kotlinx/benchmark/integration/ProjectBuilder.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@ package kotlinx.benchmark.integration
33
class ProjectBuilder {
44
private val configurations = mutableMapOf<String, BenchmarkConfiguration>()
55
private val targets = mutableMapOf<String, BenchmarkTarget>()
6+
private val kotlinConfig = KotlinConfiguration()
67

78
fun configuration(name: String, configuration: BenchmarkConfiguration.() -> Unit = {}) {
89
configurations[name] = BenchmarkConfiguration().apply(configuration)
910
}
11+
1012
fun register(name: String, configuration: BenchmarkTarget.() -> Unit = {}) {
1113
targets[name] = BenchmarkTarget().apply(configuration)
1214
}
1315

16+
fun kotlin(configuration: KotlinConfiguration.() -> Unit) {
17+
kotlinConfig.apply(configuration)
18+
}
19+
1420
fun build(original: String): String {
1521

1622
val script =
1723
"""
24+
kotlin {
25+
${kotlinConfig.lines().joinToString("\n ")}
26+
}
1827
benchmark {
1928
configurations {
2029
${configurations.flatMap { it.value.lines(it.key) }.joinToString("\n ")}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package kotlinx.benchmark.integration
2+
3+
import kotlin.test.*
4+
5+
class InvalidTargetingTest : GradleTest() {
6+
7+
@Test
8+
fun testInvalidTargetForKotlinWASM() {
9+
val runner = project("kotlin-multiplatform") {
10+
configuration("invalidWasm") {
11+
iterations = 1
12+
iterationTime = 100
13+
iterationTimeUnit = "ms"
14+
reportFormat = "json"
15+
}
16+
register("wasmTest")
17+
kotlin {
18+
wasm("wasmTest") {
19+
nodejs()
20+
}
21+
}
22+
}
23+
24+
runner.runAndFail("invalidWasmBenchmark") {
25+
assertOutputContains("Kotlin/WASM does not support targeting NodeJS for benchmarks.")
26+
}
27+
}
28+
29+
@Test
30+
fun testInvalidTargetForKotlinJS() {
31+
val runner = project("kotlin-multiplatform") {
32+
configuration("invalidJS") {
33+
iterations = 1
34+
iterationTime = 100
35+
iterationTimeUnit = "ms"
36+
reportFormat = "json"
37+
}
38+
register("jsTest")
39+
kotlin {
40+
js("jsTest", KotlinConfiguration.IR) {
41+
d8()
42+
}
43+
}
44+
}
45+
46+
runner.runAndFail("invalidJsBenchmark") {
47+
assertOutputContains("Kotlin/JS does not support targeting D8 for benchmarks.")
48+
}
49+
}
50+
51+
@Test
52+
fun testLegacyJsBackend() {
53+
val runner = project("kotlin-multiplatform") {
54+
configuration("legacyJS") {
55+
iterations = 1
56+
iterationTime = 100
57+
iterationTimeUnit = "ms"
58+
reportFormat = "json"
59+
}
60+
register("legacyJsTest")
61+
kotlin {
62+
js("legacyJsTest", KotlinConfiguration.LEGACY) {
63+
nodejs()
64+
}
65+
}
66+
}
67+
68+
runner.runAndFail("legacyJsBenchmark") {
69+
assertOutputContains("Legacy Kotlin/JS backend is not supported. Please migrate to the Kotlin/JS IR compiler backend.")
70+
}
71+
}
72+
73+
@Test
74+
fun testBrowserEnvironmentForKotlinJS() {
75+
val runner = project("kotlin-multiplatform") {
76+
configuration("invalidBrowserJS") {
77+
iterations = 1
78+
iterationTime = 100
79+
iterationTimeUnit = "ms"
80+
reportFormat = "json"
81+
}
82+
register("jsBrowserTest")
83+
kotlin {
84+
js("jsBrowserTest", KotlinConfiguration.IR) {
85+
browser()
86+
}
87+
}
88+
}
89+
90+
runner.runAndFail("invalidBrowserJsBenchmark") {
91+
assertOutputContains("The browser() environment is not supported for Kotlin/JS benchmarks. Please use nodejs().")
92+
}
93+
}
94+
}

plugin/main/src/kotlinx/benchmark/gradle/JsEngineExecTasks.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ fun Project.createJsEngineBenchmarkExecTask(
2020
val taskName = "${target.name}${config.capitalizedName()}${BenchmarksPlugin.BENCHMARK_EXEC_SUFFIX}"
2121
val compilationTarget = compilation.target
2222

23+
check(compilation is KotlinJsIrCompilation) { "Legacy Kotlin/JS backend is not supported. Please migrate to the Kotlin/JS IR compiler backend." }
24+
2325
if (compilationTarget is KotlinJsSubTargetContainerDsl) {
2426
compilationTarget.whenNodejsConfigured {
2527
val execTask = createNodeJsExec(config, target, compilation, taskName)
2628
tasks.getByName(config.prefixName(RUN_BENCHMARKS_TASKNAME)).dependsOn(execTask)
2729
}
30+
compilationTarget.whenBrowserConfigured {
31+
throw GradleException("The browser() environment is not supported for Kotlin/JS benchmarks. Please use nodejs().")
32+
}
2833
}
2934

3035
if (compilationTarget is KotlinWasmSubTargetContainerDsl) {
31-
check(compilation is KotlinJsIrCompilation) { "Legacy Kotlin/JS is does not supported by D8 engine" }
3236
compilationTarget.whenD8Configured {
3337
val execTask = createD8Exec(config, target, compilation, taskName)
3438
tasks.getByName(config.prefixName(RUN_BENCHMARKS_TASKNAME)).dependsOn(execTask)
@@ -69,6 +73,9 @@ private fun Project.createNodeJsExec(
6973
compilation: KotlinJsCompilation,
7074
taskName: String
7175
): TaskProvider<NodeJsExec> = NodeJsExec.create(compilation, taskName) {
76+
if (compilation.target.platformType == KotlinPlatformType.wasm) {
77+
throw GradleException("Kotlin/WASM does not support targeting NodeJS for benchmarks.")
78+
}
7279
dependsOn(compilation.runtimeDependencyFiles)
7380
group = BenchmarksPlugin.BENCHMARKS_TASK_GROUP
7481
description = "Executes benchmark for '${target.name}' with NodeJS"
@@ -90,6 +97,9 @@ private fun Project.createD8Exec(
9097
compilation: KotlinJsIrCompilation,
9198
taskName: String
9299
): TaskProvider<D8Exec> = D8Exec.create(compilation, taskName) {
100+
if (compilation.target.platformType == KotlinPlatformType.js) {
101+
throw GradleException("Kotlin/JS does not support targeting D8 for benchmarks.")
102+
}
93103
dependsOn(compilation.runtimeDependencyFiles)
94104
group = BenchmarksPlugin.BENCHMARKS_TASK_GROUP
95105
description = "Executes benchmark for '${target.name}' with D8"

0 commit comments

Comments
 (0)