Skip to content

Commit

Permalink
Add multiplatform targets and benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored and dlin-ta committed Dec 1, 2024
1 parent 162334e commit 83e6cbc
Show file tree
Hide file tree
Showing 15 changed files with 2,342 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# generated by Gradle Wrapper
kt/gradlew linguist-generated
kt/gradlew.bat linguist-generated

# generated by Kotlin/JS
kt/kotlin-js-store/yarn.lock
122 changes: 122 additions & 0 deletions .github/workflows/kt-bench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: Kotlin benchmarks

on:
workflow_dispatch:

permissions:
contents: write

jobs:
get-inputs:
uses: ephemient/aoc2024/.github/workflows/get-inputs.yml@main
secrets:
SESSION: ${{ secrets.SESSION }}

jmh-visualizer:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
repository: jzillmann/jmh-visualizer
- uses: actions/setup-node@v4
with:
node-version: 18
cache: npm
- run: npm install
- run: npm run providedZip
env:
NODE_OPTIONS: --openssl-legacy-provider
- uses: actions/upload-artifact@v4
with:
name: jmh-visualizer
path: jmh-visualizer.zip

assemble:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew assembleBenchmarks
working-directory: kt

build:
needs: [ assemble, get-inputs ]
runs-on: ubuntu-latest
strategy:
matrix:
target: [ jvm, js, linuxX64 ]

steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: inputs
path: inputs
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21
- uses: gradle/actions/setup-gradle@v4
with:
cache-read-only: true
- run: ./gradlew ${{ matrix.target }}Benchmark
working-directory: kt
env:
AOC2024_DATADIR: ${{ github.workspace }}/inputs
- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.target }}-benchmarks
path: kt/aoc2024-exe/build/reports/benchmarks

docs:
needs: [ jmh-visualizer, build ]
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
ref: gh-docs
- uses: actions/download-artifact@v4
with:
name: jmh-visualizer
- uses: actions/download-artifact@v4
with:
name: jvm-benchmarks
path: benchmarks
- uses: actions/download-artifact@v4
with:
name: js-benchmarks
path: benchmarks
- uses: actions/download-artifact@v4
with:
name: linuxX64-benchmarks
path: benchmarks
- run: rm -rf jmh-visualizer
- run: unzip -d jmh-visualizer jmh-visualizer.zip
- name: Create provided.js
run: |
shopt -s failglob
names=() jsonargs=()
for file in benchmarks/main/*/*.json; do
name=${file##*/}
name=${name%.*}
names+=("$name")
jsonargs+=(--slurpfile "$name" "$file")
done
cat >jmh-visualizer/provided.js <<EOF
// provided.js - generated by ${{ github.repository }}/${{ github.workflow }}, $(date +'%Y-%m-%d %H:%M:%S.%N')
var providedBenchmarks = $(jq --null-input --compact-output '$ARGS.positional|unique' --args "${names[@]}");
var providedBenchmarkStore = $(jq --null-input '$ARGS.named|map_values(.[0]|sort_by(.benchmark|[split("[.]|(?<!\\d)(?!\\D)|(?<!\\D)(?!\\d)")|map(try tonumber catch .),.]))' "${jsonargs[@]}");
EOF
- uses: EndBug/add-and-commit@v9
with:
add: jmh-visualizer
message: 'kotlinx.benchmark ${{ github.sha }}'
76 changes: 69 additions & 7 deletions .github/workflows/kt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,85 @@ jobs:
SESSION: ${{ secrets.SESSION }}

build:
needs: [ get-inputs ]
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew build distZip
working-directory: kt
- uses: actions/upload-artifact@v4
with:
name: aoc2024-exe
path: kt/aoc2024-exe/build/distributions/*.zip
- uses: actions/upload-artifact@v4
with:
name: aoc2024-kexe
path: |
kt/aoc2024-exe/build/bin/*/debugExecutable/*.kexe
kt/aoc2024-exe/build/bin/*/releaseExecutable/*.kexe
- uses: actions/upload-artifact@v4
with:
name: aoc2024-js
path: kt/build/js/packages/aoc2024-aoc2024-exe/kotlin/*

run-jvm:
needs: [ get-inputs, build ]
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v4
with:
name: inputs
path: inputs
- uses: actions/download-artifact@v4
with:
name: aoc2024-exe
- run: unzip aoc2024-exe.zip
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 21
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew check
working-directory: kt
- run: ./gradlew :aoc2024-exe:jvmRun
working-directory: kt
- run: aoc2024-exe/bin/aoc2024-exe
env:
AOC2024_DATADIR: inputs

run-native:
needs: [ get-inputs, build ]
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v4
with:
name: inputs
path: inputs
- uses: actions/download-artifact@v4
with:
name: aoc2024-kexe
- run: chmod +x linuxX64/*/aoc2024-exe.kexe
- run: linuxX64/releaseExecutable/aoc2024-exe.kexe
env:
AOC2024_DATADIR: inputs

run-node:
needs: [ get-inputs, build ]
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v4
with:
name: inputs
path: inputs
- uses: actions/download-artifact@v4
with:
name: aoc2024-js
- uses: actions/setup-node@v4
with:
node-version: 22.0.0
- run: node aoc2024-aoc2024-exe.js
env:
AOC2024_DATADIR: ${{ github.workspace }}/inputs
AOC2024_DATADIR: inputs
8 changes: 7 additions & 1 deletion kt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ Run the test suite:
./gradlew :aoc2024-lib:allTests
```

Run [kotlinx.benchmark](https://github.com/Kotlin/kotlinx-benchmark) ([JMH](https://openjdk.java.net/projects/code-tools/jmh/)) benchmarks:

```sh
./gradlew :aoc2024-exe:benchmark
```

Print solutions for the inputs provided in local data files:

```sh
./gradlew :aoc2024-exe:jvmRun
./gradlew :aoc2024-exe:jvmRun :aoc2024-exe:runReleaseExecutable{LinuxX64,Macos{X64,Arm64}} :aoc2024-exe:jsNodeProductionRun
```

Run all checks, including [Detekt](https://detekt.github.io/) static code analysis:
Expand Down
101 changes: 100 additions & 1 deletion kt/aoc2024-exe/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree

plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.kotlin.plugin.allopen)
alias(libs.plugins.detekt)
alias(libs.plugins.kotlinx.benchmark)
distribution
}

kotlin {
jvm {
mainRun {
mainClass = "com.github.ephemient.aoc2024.exe.Main"
}
compilations {
create("bench") {
associateWith(getByName("main"))
}
}
}
js {
nodejs()
binaries.executable()
}
for (target in arrayOf(linuxArm64(), linuxX64(), macosArm64(), macosX64(), mingwX64())) {
target.binaries.executable {
entryPoint("com.github.ephemient.aoc2024.exe.main")
}
target.compilations {
create("bench") {
associateWith(getByName("main"))
}
}
}

applyDefaultHierarchyTemplate()
applyHierarchyTemplate {
withSourceSetTree(KotlinSourceSetTree("bench"))
common {
withJvm()
withNative()
}
}

sourceSets {
Expand All @@ -19,9 +50,77 @@ kotlin {
implementation(libs.kotlinx.coroutines)
}
}
getByName("commonBench") {
dependencies {
implementation(libs.kotlinx.benchmark)
}
}
jsMain {
dependencies {
implementation(libs.kotlin.wrappers.node)
}
}
nativeMain {
dependencies {
implementation(libs.okio)
}
}
}
}

dependencies {
detektPlugins(libs.bundles.detekt.plugins)
}

distributions {
main {
val jvmRuntimeClasspath = files(
tasks.named("jvmJar"),
configurations.getByName("jvmRuntimeClasspath"),
)

val startScripts by tasks.registering(CreateStartScripts::class) {
mainClass = "com.github.ephemient.aoc2024.exe.Main"
classpath = jvmRuntimeClasspath
outputDir = File(buildDir, "scripts")
applicationName = project.name
}

contents {
from(startScripts) {
into("bin")
}
from(jvmRuntimeClasspath) {
into("lib")
}
from("src/dist")
}
}
}

allOpen {
annotation("org.openjdk.jmh.annotations.State")
}

benchmark {
targets {
register("jvmBench")
register("linuxArm64Bench")
register("linuxX64Bench")
register("macosArm64Bench")
register("macosX64Bench")
register("mingwX64Bench")
}

configurations {
getByName("main") {
warmups = 1
iterationTime = 1
iterationTimeUnit = "s"
mode = "avgt"
outputTimeUnit = "us"
project.findProperty("benchmarkInclude")?.let { include(it.toString()) }
project.findProperty("benchmarkExclude")?.let { exclude(it.toString()) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.ephemient.aoc2024.exe

import com.github.ephemient.aoc2024.Day1
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.Scope
import kotlinx.benchmark.Setup
import kotlinx.benchmark.State

@State(Scope.Benchmark)
class Day1Bench {
private lateinit var input: String

@Setup
fun setup() {
input = getDayInput(1)
}

@Benchmark
fun part1() = Day1(input).part1()

@Benchmark
fun part2() = Day1(input).part2()
}
Loading

0 comments on commit 83e6cbc

Please sign in to comment.