Skip to content

Commit

Permalink
Merge pull request #33 from lounres/feature/benchmark
Browse files Browse the repository at this point in the history
Add kotlinx-benchmark and examples
  • Loading branch information
lounres authored Jan 2, 2023
2 parents a6091bf + 8061982 commit 4c8ba10
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 28 deletions.
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Example user template template
### Example user template

### Lounres's template
# IntelliJ project files
.idea
!.idea/icon.svg
!.idea/scopes
!.idea/copyright
*.iml
out
gen

### Gradle template
.gradle
**/build/
Expand Down
6 changes: 6 additions & 0 deletions .idea/copyright/Kone.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions .idea/scopes/Kone_sources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 127 additions & 17 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
@file:Suppress("SuspiciousCollectionReassignment")

import io.kotest.framework.multiplatform.gradle.KotestMultiplatformCompilerGradlePlugin
import kotlinx.benchmark.gradle.BenchmarksExtension
import kotlinx.benchmark.gradle.BenchmarksPlugin
import org.jetbrains.dokka.gradle.DokkaPlugin
import org.jetbrains.dokka.gradle.AbstractDokkaLeafTask
import org.jetbrains.kotlin.allopen.gradle.AllOpenExtension
import org.jetbrains.kotlin.allopen.gradle.AllOpenGradleSubplugin
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode.Warning
import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinCompilationToRunnableFiles
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget


@Suppress("DSL_SCOPE_VIOLATION")
plugins {
with(libs.plugins) {
alias(kotlin.multiplatform) apply false
alias(allopen) apply false
alias(kotlinx.benchmark) apply false
alias(kotest.multiplatform) apply false
alias(dokka)
}
Expand Down Expand Up @@ -44,6 +53,7 @@ val jvmTargetApi = properties["jvmTarget"] as String

fun PluginManager.withPlugin(pluginDep: PluginDependency, block: AppliedPlugin.() -> Unit) = withPlugin(pluginDep.pluginId, block)
fun PluginManager.withPlugin(pluginDepProvider: Provider<PluginDependency>, block: AppliedPlugin.() -> Unit) = withPlugin(pluginDepProvider.get().pluginId, block)
inline fun <T> Iterable<T>.withEach(action: T.() -> Unit) = forEach { it.action() }


publishing {
Expand All @@ -64,7 +74,8 @@ featuresManagement {
kotlinOptions {
jvmTarget = jvmTargetApi
}
compileKotlinTask.apply {
compileTaskProvider.apply {
// TODO: Check if really is necessary
kotlinOptions {
jvmTarget = jvmTargetApi
}
Expand All @@ -84,17 +95,13 @@ featuresManagement {
on("kotlin multiplatform") {
apply<KotlinMultiplatformPluginWrapper>()
configure<KotlinMultiplatformExtension> {
// explicitApi = Warning

jvm {
compilations.all {
kotlinOptions {
jvmTarget = jvmTargetApi
}
compileKotlinTask.apply {
kotlinOptions {
jvmTarget = jvmTargetApi
}
freeCompilerArgs += listOf(
// "-Xlambdas=indy"
)
}
}
testRuns.all {
Expand Down Expand Up @@ -169,15 +176,13 @@ featuresManagement {
configure<KotlinMultiplatformExtension> {
@Suppress("UNUSED_VARIABLE")
sourceSets {
all {
if (name.endsWith("test", ignoreCase = true)) {
dependencies {
with(rootProject.libs.kotest) {
implementation(framework.engine)
implementation(framework.datatest)
implementation(assertions.core)
implementation(property)
}
val commonTest by getting {
dependencies {
with(rootProject.libs.kotest) {
implementation(framework.engine)
implementation(framework.datatest)
implementation(assertions.core)
implementation(property)
}
}
}
Expand All @@ -190,6 +195,111 @@ featuresManagement {
}
}
}
on("examples") {
@Suppress("UNUSED_VARIABLE")
fun NamedDomainObjectContainer<out AbstractKotlinCompilationToRunnableFiles<*>>.configureExamples() {
val main by getting
val examples by creating {
defaultSourceSet {
dependsOn(main.defaultSourceSet)
kotlin.setSrcDirs(listOf("src/examples/kotlin"))
resources.setSrcDirs(listOf("src/examples/resources"))
}

task<JavaExec>("runJvmExample") {
group = "examples"
classpath = output.classesDirs + compileDependencyFiles + runtimeDependencyFiles
mainClass.set("com.lounres.${project.extra["artifactPrefix"]}${project.name}.examples.MainKt")
}
}
}
pluginManager.withPlugin(rootProject.libs.plugins.kotlin.jvm) {
configure<KotlinJvmProjectExtension> {
@Suppress("UNUSED_VARIABLE")
target.compilations.configureExamples()
}
}
pluginManager.withPlugin(rootProject.libs.plugins.kotlin.multiplatform) {
configure<KotlinMultiplatformExtension> {
@Suppress("UNUSED_VARIABLE")
targets.getByName<KotlinJvmTarget>("jvm").compilations.configureExamples()
}
}
}
on("benchmark") {
apply<AllOpenGradleSubplugin>()
apply<BenchmarksPlugin>()
the<AllOpenExtension>().annotation("org.openjdk.jmh.annotations.State")

pluginManager.withPlugin(rootProject.libs.plugins.kotlin.jvm) {
logger.error("kotlinx.benchmark plugging in and setting is not yet implemented for Kotlin/JVM plug-in")
// configure<KotlinJvmProjectExtension> {
// // ...
// }
}
pluginManager.withPlugin(rootProject.libs.plugins.kotlin.multiplatform) {
val kotlinxBenchmarkDebug = (property("kotlinx.benchmark.debug") as String?).toBoolean()
val benchmarksExtension = the<BenchmarksExtension>()
@Suppress("UNUSED_VARIABLE")
configure<KotlinMultiplatformExtension> {
val commonBenchmarks by sourceSets.creating {
dependencies {
implementation(rootProject.libs.kotlinx.benchmark.runtime)
}
}
targets.filter { it.platformType != KotlinPlatformType.common }.withEach {
compilations {
val main by getting
val benchmarks by creating {
defaultSourceSet {
dependsOn(main.defaultSourceSet)
dependsOn(commonBenchmarks)
}
}

val benchmarksSourceSetName = benchmarks.defaultSourceSet.name

// TODO: For now js target causes problems with tasks initialisation
// Looks similar to
// 1. https://github.com/Kotlin/kotlinx-benchmark/issues/101
// 2. https://github.com/Kotlin/kotlinx-benchmark/issues/93
// TODO: For now native targets work unstable
// May be similar to https://github.com/Kotlin/kotlinx-benchmark/issues/94
// Because of all the issues, only JVM targets are registered for now
if (platformType == KotlinPlatformType.jvm) {
benchmarksExtension.targets.register(benchmarksSourceSetName)
// Fix kotlinx-benchmarks bug
if (platformType == KotlinPlatformType.jvm) afterEvaluate {
val jarTaskName = "${benchmarksSourceSetName}BenchmarkJar"
tasks.findByName(jarTaskName).let { if (it is org.gradle.jvm.tasks.Jar) it else null }?.apply {
if (kotlinxBenchmarkDebug) logger.warn("Corrected kotlinx.benchmark task $jarTaskName")
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
}
}
}
}
}
}

@Suppress("UNUSED_VARIABLE")
configure<BenchmarksExtension> {
configurations {
// TODO: Create my own configurations
val main by getting {
warmups = 20
iterations = 10
iterationTime = 3
}
val smoke by creating {
warmups = 5
iterations = 3
iterationTime = 500
iterationTimeUnit = "ms"
}
}
}
}
on("dokka") {
apply<DokkaPlugin>()
dependencies {
Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
kotlin.code.style=official
kotlin.mpp.stability.nowarn=true
kotlin.native.ignoreDisabledTargets=true
kotlin.incremental.js.ir=true

jvmTarget=11

kotlinx.benchmark.debug=false

#org.gradle.configureondemand=true
org.gradle.parallel=true
org.gradle.jvmargs=-XX:MaxMetaspaceSize=2G
35 changes: 28 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
[versions]
kotlin = "1.8.0"

kmath = "0.3.0"

kotlinx-coroutines = "1.6.4"
kotlinx-atomicfu = "0.18.5"
kotlinx-serialization = "1.4.1"
kotlinx-datetime = "0.4.0"
kotlinx-kover = "0.6.0"
kotlinx-benchmark = "0.4.6"
kotlinx-binary-compatibility-validator = "0.12.0"
kotlinx-knit = "0.4.0"

kotest = "5.5.1"
dokka = "1.7.20"
mkdocs-gradle-plugin = "2.4.0"
kover = "0.6.0"
benchmark = "0.4.4"
binary-compatibility-validator = "0.12.0"
kmath = "0.3.0"

[plugins]
# Kotlin plugins
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }

# Kotlin side plugins
allopen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "kotlin" }

# kotlinx plugins
kotlinx-atomicfu = { id = "org.jetbrains.kotlinx:atomicfu-gradle-plugin", version.ref = "kotlinx-atomicfu" }
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kotlinx-kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kotlinx-kover" }
kotlinx-benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "kotlinx-benchmark" }
kotlinx-binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "kotlinx-binary-compatibility-validator" }
kotlinx-knit = { id = "org.jetbrains.kotlinx:kotlinx-knit", version.ref = "kotlinx-knit" }

kotest-multiplatform = { id = "io.kotest.multiplatform", version.ref = "kotest" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
mkdocs = { id = "ru.vyarus.mkdocs", version.ref = "mkdocs-gradle-plugin" }
kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" }
benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "benchmark" }
binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binary-compatibility-validator" }

[libraries]
# kotlinx.coroutines
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-debug = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-debug", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }

# kotlinx.serialization
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }

# kotlinx.datetime
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }

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

# Kotest
kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
kotest-framework-engine = { module = "io.kotest:kotest-framework-engine", version.ref = "kotest" }
Expand Down
18 changes: 18 additions & 0 deletions libs/main/polynomial/src/commonBenchmarks/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@file:Suppress("unused")

package com.lounres.kone.polynomial.benchmarks

//import com.lounres.kone.algebraic.field
//import com.lounres.kone.polynomial.ListPolynomial
//import com.lounres.kone.polynomial.listPolynomialSpace
//import kotlinx.benchmark.*
//
//@State(Scope.Benchmark)
//class ListPolynomialBenchmark {
// @Benchmark
// fun stupidTest(blackhole: Blackhole) = Double.field.listPolynomialSpace {
// val polynomial = ListPolynomial(1.1, 1.1)
// blackhole.consume(polynomial pow 16u)
// }
//}
//
11 changes: 11 additions & 0 deletions libs/main/polynomial/src/examples/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.lounres.kone.polynomial.examples

import com.lounres.kone.algebraic.field
import com.lounres.kone.polynomial.labeledPolynomialSpace
import space.kscience.kmath.expressions.Symbol


fun main() = Double.field.labeledPolynomialSpace {
val x = Symbol.x
println(x * (x - 1) * (x - 2))
}
Loading

0 comments on commit 4c8ba10

Please sign in to comment.