Skip to content

Commit 0e15b5c

Browse files
qwwdfsaddkhalanskyjb
authored andcommitted
Introduce benchmarks module with basic formatting benchmark
1 parent 7a1d97e commit 0e15b5c

File tree

6 files changed

+96
-1
lines changed

6 files changed

+96
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
*.iml
55
target
66
build
7-
/local.properties
7+
/local.properties
8+
benchmarks.jar

benchmarks/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
### Benchmarks utility module
2+
3+
Module that provides benchmarking infrastructure for kotlinx-datetime.
4+
Please note that these benchmarks are typically written with the specific target, hypothesis and effect in mind.
5+
6+
They provide numbers, not insights, and shouldn't be used as the generic comparison and statements like
7+
"X implementaiton or format is faster/slower than Y"
8+
9+
10+
#### Usage
11+
12+
```
13+
// Build `benchmarks.jar` into the project's root
14+
./gradlew :benchmarks:jmhJar
15+
16+
// Run all benchmarks
17+
java -jar benchmarks.jar
18+
19+
// Run dedicated benchmark(s)
20+
java -jar benchmarks.jar Formatting
21+
java -jar benchmarks.jar FormattingBenchmark.formatIso
22+
23+
// Run with the specified number of warmup iterations, measurement iterations, timeunit and mode
24+
java -jar benchmarks.jar -wi 5 -i 5 -tu us -bm thrpt Formatting
25+
26+
// Extensive help
27+
java -jar benchmarks.jar -help
28+
```

benchmarks/build.gradle.kts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2019-2023 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
plugins {
7+
id("kotlin")
8+
id("me.champeau.jmh")
9+
}
10+
11+
12+
val mainJavaToolchainVersion by ext(project.property("java.mainToolchainVersion"))
13+
val modularJavaToolchainVersion by ext(project.property("java.modularToolchainVersion"))
14+
15+
sourceSets {
16+
dependencies {
17+
implementation(project(":kotlinx-datetime"))
18+
implementation("org.openjdk.jmh:jmh-core:1.35")
19+
}
20+
}
21+
22+
// Publish benchmarks to the root for the easier 'java -jar benchmarks.jar`
23+
tasks.named<Jar>("jmhJar") {
24+
val nullString: String? = null
25+
archiveBaseName.set("benchmarks")
26+
archiveClassifier.set(nullString)
27+
archiveVersion.set(nullString)
28+
archiveVersion.convention(nullString)
29+
destinationDirectory.set(file("$rootDir"))
30+
}
31+
32+
repositories {
33+
mavenCentral()
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2019-2023 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime
7+
8+
import org.openjdk.jmh.annotations.*
9+
import java.util.concurrent.*
10+
11+
@Warmup(iterations = 5, time = 1)
12+
@Measurement(iterations = 5, time = 1)
13+
@BenchmarkMode(Mode.Throughput)
14+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
15+
@State(Scope.Benchmark)
16+
@Fork(1)
17+
open class FormattingBenchmark {
18+
19+
private val localDateTime = LocalDateTime(2023, 11, 9, 12, 21, 31, 41)
20+
private val formatted = LocalDateTime.Formats.ISO.format(localDateTime)
21+
22+
@Benchmark
23+
fun formatIso() = LocalDateTime.Formats.ISO.format(localDateTime)
24+
25+
@Benchmark
26+
fun parseIso() = LocalDateTime.Formats.ISO.parse(formatted)
27+
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ versionSuffix=SNAPSHOT
88
defaultKotlinVersion=1.9.21
99
dokkaVersion=1.9.10
1010
serializationVersion=1.6.2
11+
benchmarksVersion=0.7.2
1112

1213
java.mainToolchainVersion=8
1314
java.modularToolchainVersion=11

settings.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ pluginManagement {
55
gradlePluginPortal()
66
}
77
val dokkaVersion: String by settings
8+
val benchmarksVersion: String by settings
89
plugins {
910
id("org.jetbrains.dokka") version dokkaVersion
11+
id("me.champeau.jmh") version benchmarksVersion
1012
}
1113
}
1214

@@ -16,3 +18,5 @@ include(":core")
1618
project(":core").name = "kotlinx-datetime"
1719
include(":serialization")
1820
project(":serialization").name = "kotlinx-datetime-serialization"
21+
include(":benchmarks")
22+

0 commit comments

Comments
 (0)