Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks #26

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 45 additions & 30 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,21 @@ env:
GRADLE_OPTS: -Dorg.gradle.daemon=false -Dkotlin.incremental=false -Dorg.gradle.jvmargs="-XX:+HeapDumpOnOutOfMemoryError -XX:MetaspaceSize=1g"

jobs:
build:
check:
strategy:
fail-fast: false
matrix:
os: [ macos-latest, ubuntu-latest, windows-latest ]

runs-on: ${{ matrix.os }}

steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Checkout Repository
uses: actions/checkout@v4

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Setup Caches [ Gradle Wrapper ]
uses: actions/cache@v1
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('gradle/wrapper/gradle-wrapper.*') }}
restore-keys: ${{ runner.os }}-gradle-wrapper-

- name: Setup Caches [ Konan ]
uses: actions/cache@v1
with:
path: ~/.konan
key: ${{ runner.os }}-konan-${{ hashFiles('**/*.gradle.kts') }}-${{ hashFiles('gradle/libs.versions.toml') }}
restore-keys: ${{ runner.os }}-konan-

- name: Setup Caches [ Gradle Caches ]
uses: actions/cache@v1
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-caches-${{ hashFiles('**/*.gradle.kts') }}-${{ hashFiles('gradle/libs.versions.toml') }}
restore-keys: ${{ runner.os }}-gradle-caches-
uses: gradle/actions/wrapper-validation@v3

- name: Setup JDK
uses: actions/setup-java@v3.4.0
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11
Expand All @@ -57,16 +34,54 @@ jobs:
if: matrix.os == 'macos-latest'
run: >
./gradlew check --stacktrace
-PKMP_TARGETS="JVM,JS,IOS_ARM64,IOS_X64,IOS_SIMULATOR_ARM64,MACOS_ARM64,MACOS_X64,TVOS_ARM64,TVOS_X64,TVOS_SIMULATOR_ARM64,WATCHOS_ARM32,WATCHOS_ARM64,WATCHOS_DEVICE_ARM64,WATCHOS_X64,WATCHOS_SIMULATOR_ARM64,WASM_JS,WASM_WASI"
-PKMP_TARGETS="IOS_ARM64,IOS_X64,IOS_SIMULATOR_ARM64,JVM,JS,MACOS_ARM64,MACOS_X64,TVOS_ARM64,TVOS_X64,TVOS_SIMULATOR_ARM64,WATCHOS_ARM32,WATCHOS_ARM64,WATCHOS_DEVICE_ARM64,WATCHOS_X64,WATCHOS_SIMULATOR_ARM64,WASM_JS,WASM_WASI"

- name: Run Linux Tests
if: matrix.os == 'ubuntu-latest'
run: >
./gradlew check --stacktrace
-PKMP_TARGETS="JVM,JS,ANDROID,ANDROID_ARM32,ANDROID_ARM64,ANDROID_X64,ANDROID_X86,LINUX_ARM64,LINUX_X64,WASM_JS,WASM_WASI"
-PKMP_TARGETS="ANDROID,ANDROID_ARM32,ANDROID_ARM64,ANDROID_X64,ANDROID_X86,JVM,JS,LINUX_ARM64,LINUX_X64,WASM_JS,WASM_WASI"

- name: Run Windows Tests
if: matrix.os == 'windows-latest'
run: >
./gradlew check --stacktrace
-PKMP_TARGETS="JVM,JS,MINGW_X64,WASM_JS,WASM_WASI"

- name: Upload Test Reports
uses: actions/upload-artifact@v4
if: ${{ always() }}
with:
name: test-report-${{ matrix.os }}
path: '**/build/reports/tests/**'
retention-days: 1

benchmark:
strategy:
fail-fast: false
matrix:
os: [ macos-latest, ubuntu-latest, windows-latest ]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Validate Gradle Wrapper
uses: gradle/actions/wrapper-validation@v3

- name: Setup JDK
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 11

- name: Run Benchmark
run: >
./gradlew benchmark
-PKMP_TARGETS="JVM,JS,LINUX_ARM64,LINUX_X64,MACOS_ARM64,MACOS_X64,MINGW_X64,WASM_JS,WASM_WASI"

- name: Upload Benchmark Reports
uses: actions/upload-artifact@v4
with:
name: benchmark-report-${{ matrix.os }}
path: '**/build/reports/benchmarks/**'
754 changes: 439 additions & 315 deletions .kotlin-js-store/yarn.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
35 changes: 35 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# benchmarks

<!-- TODO: CryptoRand Issue #23 -->
Benchmarks for tracking performance of `SecureRandom` implementation.

**NOTE:** Benchmarking is run on every Pull Request. Results can be viewed for each
workflow run on the [GitHub Actions][url-actions] tab of the repository.

- Run All platforms:
```shell
./gradlew benchmark
```

- Run Jvm:
```shell
./gradlew jvmBenchmark
```

- Run Js:
```shell
./gradlew jsBenchmark
```

- Run WasmJs:
```shell
./gradlew wasmJsBenchmark
```

- Run Native:
```shell
./gradlew nativeHostBenchmark
```

<!-- TODO: CryptoRand Issue #23 -->
[url-actions]: https://github.com/KotlinCrypto/secure-random/actions/
63 changes: 63 additions & 0 deletions benchmarks/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2025 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
import io.matthewnelson.kmp.configuration.extension.container.target.KmpTarget
import kotlinx.benchmark.gradle.BenchmarksExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget

plugins {
id("configuration")
}

kmpConfiguration {
val benchmarks by lazy { extensions.getByType<BenchmarksExtension>() }

@OptIn(ExperimentalWasmDsl::class)
configure {
fun <T: KotlinTarget> KmpTarget<T>.register() {
target { benchmarks.targets.register(name) }
}

jvm { register() }

js { target { browser(); nodejs() }; register() }
wasmJs { target { browser(); nodejs() }; register() }

val nativeHost = "nativeHost"
when (HostManager.host) {
is KonanTarget.LINUX_X64 -> linuxX64(nativeHost) { register() }
is KonanTarget.LINUX_ARM64 -> linuxArm64(nativeHost) { register() }
is KonanTarget.MACOS_X64 -> macosX64(nativeHost) { register() }
is KonanTarget.MACOS_ARM64 -> macosArm64(nativeHost) { register() }
is KonanTarget.MINGW_X64 -> mingwX64(nativeHost) { register() }
else -> {}
}

common {
pluginIds(libs.plugins.benchmark.get().pluginId)

sourceSetMain {
dependencies {
implementation(libs.benchmark.runtime)
// TODO: :library:secure-random Issue #22
implementation(project(":secure-random"))
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("unused")

package org.kotlincrypto.benchmarks

import kotlinx.benchmark.*
import org.kotlincrypto.SecureRandom

@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(BenchmarkTimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 2)
@Measurement(iterations = 5, time = 3)
open class SecureRandomBenchmark {

private val buf = ByteArray(20)

@Setup
fun setup() {
SECURE_RANDOM.nextBytesCopyTo(buf)
}

@Benchmark
fun nextBytesCopyTo() {
SECURE_RANDOM.nextBytesCopyTo(buf)
}

private companion object {
private val SECURE_RANDOM = SecureRandom()
}
}
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnPlugin
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension

plugins {
alias(libs.plugins.benchmark) apply(false)
alias(libs.plugins.binary.compat)
alias(libs.plugins.kotlin.multiplatform) apply(false)
}
Expand Down Expand Up @@ -44,6 +45,7 @@ apiValidation {
if (findProperty("CHECK_PUBLICATION") != null) {
ignoredProjects.add("check-publication")
} else {
ignoredProjects.add("benchmarks")
ignoredProjects.add("sample")
}
}
26 changes: 14 additions & 12 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
[versions]
gradle-binary-compat = "0.16.3"
gradle-kmp-configuration = "0.3.2"
gradle-kotlin = "1.9.24"
gradle-publish-maven = "0.29.0"
gradle-benchmark = "0.4.11"
gradle-binary-compat = "0.16.3"
gradle-kmp-configuration = "0.3.2"
gradle-kotlin = "1.9.24"
gradle-publish-maven = "0.29.0"

# tests
kotlinx-coroutines = "1.8.1"
kotlinx-coroutines = "1.8.1"

[libraries]
gradle-kmp-configuration = { module = "io.matthewnelson:gradle-kmp-configuration-plugin", version.ref = "gradle-kmp-configuration" }
gradle-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "gradle-kotlin" }
gradle-publish-maven = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "gradle-publish-maven" }
gradle-kmp-configuration = { module = "io.matthewnelson:gradle-kmp-configuration-plugin", version.ref = "gradle-kmp-configuration" }
gradle-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "gradle-kotlin" }
gradle-publish-maven = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "gradle-publish-maven" }

# tests
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
benchmark-runtime = { module = "org.jetbrains.kotlinx:kotlinx-benchmark-runtime", version.ref = "gradle-benchmark" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }

[plugins]
binary-compat = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "gradle-binary-compat" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "gradle-kotlin" }
benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "gradle-benchmark" }
binary-compat = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "gradle-binary-compat" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "gradle-kotlin" }
9 changes: 8 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ private val CHECK_PUBLICATION: String? by settings
if (CHECK_PUBLICATION != null) {
include(":tools:check-publication")
} else {
include(":secure-random")
listOf(
"secure-random",
).forEach { name ->
// TODO: :library Issue #22
include(":$name")
}

include(":benchmarks")
include(":sample")
}