Skip to content

Commit

Permalink
Support nodejs() environment for wasmJs
Browse files Browse the repository at this point in the history
  • Loading branch information
qurbonzoda committed Dec 7, 2023
1 parent 29b873e commit 3f32f2d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 13 deletions.
15 changes: 13 additions & 2 deletions examples/kotlin-multiplatform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ kotlin {
compilations.create("defaultExecutor") { associateWith(compilations.main) }
compilations.create("builtInExecutor") { associateWith(compilations.main) }
}
wasm('wasmJs') { d8() }
wasm('wasmJs') { nodejs() }

// Native targets
macosX64()
Expand Down Expand Up @@ -134,4 +134,15 @@ benchmark {
register("linuxX64")
register("mingwX64")
}
}
}

// Node.js with canary v8 that supports recent Wasm GC changes
rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with {
nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}

// Drop this when node js version become stable
rootProject.tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask.class).configureEach {
args.add("--ignore-engines")
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ class InvalidTargetingTest : GradleTest() {
@Test
fun testWasmNodeJs() {
val runner = project("invalid-target/wasm-nodejs", true)
runner.runAndFail("wasmJsBenchmark") {
assertOutputContains("kotlinx-benchmark supports only d8() environment for Kotlin/Wasm.")
}
runner.run("wasmJsBenchmark") // Successful
}

@Test
fun testWasmBrowser() {
val runner = project("invalid-target/wasm-browser", true)
runner.runAndFail("wasmJsBenchmark") {
assertOutputContains("kotlinx-benchmark supports only d8() environment for Kotlin/Wasm.")
assertOutputContains("kotlinx-benchmark only supports d8() and nodejs() environments for Kotlin/Wasm.")
}
}

@Test
fun testJsD8() {
val runner = project("invalid-target/js-d8", true)
runner.runAndFail("jsBenchmark") {
assertOutputContains("kotlinx-benchmark supports only nodejs() environment for Kotlin/JS.")
assertOutputContains("kotlinx-benchmark only supports nodejs() environment for Kotlin/JS.")
}
}

Expand All @@ -40,7 +38,7 @@ class InvalidTargetingTest : GradleTest() {
fun testJsBrowser() {
val runner = project("invalid-target/js-browser", true)
runner.runAndFail("jsBenchmark") {
assertOutputContains("kotlinx-benchmark supports only nodejs() environment for Kotlin/JS.")
assertOutputContains("kotlinx-benchmark only supports nodejs() environment for Kotlin/JS.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@ kotlin {
wasm("wasmJs") {
nodejs()
}

sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.5.0-SNAPSHOT")
}
}
}
}

benchmark {
targets {
register("wasmJs")
}
}

// Node.js with canary v8 that supports recent Wasm GC changes
rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with {
nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}

// Drop this when node js version become stable
tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask.class).configureEach {
args.add("--ignore-engines")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test

import kotlinx.benchmark.*
import kotlin.math.*

@State(Scope.Benchmark)
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.Throughput)
open class CommonBenchmark {
@Benchmark
open fun mathBenchmark(): Double {
return log(sqrt(3.0) * cos(3.0), 2.0)
}
}
8 changes: 5 additions & 3 deletions plugin/main/src/kotlinx/benchmark/gradle/JsEngineExecTasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ fun Project.createJsEngineBenchmarkExecTask(
if (compilationTarget.isD8Configured) {
val execTask = createD8Exec(config, target, compilation, taskName)
tasks.getByName(config.prefixName(RUN_BENCHMARKS_TASKNAME)).dependsOn(execTask)
} else if (compilationTarget.isNodejsConfigured) {
val execTask = createNodeJsExec(config, target, compilation, taskName)
tasks.getByName(config.prefixName(RUN_BENCHMARKS_TASKNAME)).dependsOn(execTask)
} else {
throw GradleException("kotlinx-benchmark supports only d8() environment for Kotlin/Wasm.")
throw GradleException("kotlinx-benchmark only supports d8() and nodejs() environments for Kotlin/Wasm.")
}
}
KotlinPlatformType.js -> {
if (compilationTarget.isNodejsConfigured) {
val execTask = createNodeJsExec(config, target, compilation, taskName)
tasks.getByName(config.prefixName(RUN_BENCHMARKS_TASKNAME)).dependsOn(execTask)
} else {
throw GradleException("kotlinx-benchmark supports only nodejs() environment for Kotlin/JS.")
throw GradleException("kotlinx-benchmark only supports nodejs() environment for Kotlin/JS.")
}
}
else -> {
Expand All @@ -62,7 +65,6 @@ private val KotlinJsIrCompilation.isWasmCompilation: Boolean get() =

private fun MutableList<String>.addWasmArguments() {
add("--experimental-wasm-gc")
add("--experimental-wasm-eh")
}

private fun MutableList<String>.addJsArguments() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class JsBenchmarkExecutor(name: String, @Suppress("UNUSED_PARAMETER") dummy_args
SuiteExecutor(name, jsEngineSupport.arguments()[0]) {

init {
check(!isD8) { "${JsBenchmarkExecutor::class.simpleName} does not supports d8 engine" }
check(!isD8) { "${JsBenchmarkExecutor::class.simpleName} does not support d8 engine" }
}

private val benchmarkJs: dynamic = require("benchmark")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class JsBuiltInExecutor(
complete: () -> Unit
) {
if (benchmarks.any { it.isAsync }) {
error("${JsBuiltInExecutor::class.simpleName} does not supports async functions")
error("${JsBuiltInExecutor::class.simpleName} does not support async functions")
}
super.run(runnerConfiguration, benchmarks, start, complete)
}
Expand Down

0 comments on commit 3f32f2d

Please sign in to comment.