Skip to content

Commit b8bbed8

Browse files
Add JMH benchmarks for datetime format creation and performance evaluation
- Implement SerialFormatBenchmark to test repeated datetime format sequences. - Implement PythonDateTimeFormatBenchmark to evaluate Python-compatible datetime formats. - Implement ParallelFormatBenchmark to test creation of formats using nested and alternative parsing logic.
1 parent 5070925 commit b8bbed8

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2019-2025 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+
@file:Suppress("unused")
7+
8+
package kotlinx.datetime
9+
10+
import kotlinx.datetime.format.alternativeParsing
11+
import kotlinx.datetime.format.char
12+
import org.openjdk.jmh.annotations.*
13+
import org.openjdk.jmh.infra.Blackhole
14+
import java.util.concurrent.TimeUnit
15+
16+
@Warmup(iterations = 5, time = 1)
17+
@Measurement(iterations = 5, time = 1)
18+
@BenchmarkMode(Mode.AverageTime)
19+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
20+
@State(Scope.Benchmark)
21+
@Fork(1)
22+
open class ParallelFormatBenchmark {
23+
24+
@Param("2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")
25+
var n = 0
26+
27+
@Benchmark
28+
fun formatCreationWithAlternativeParsing(blackhole: Blackhole) {
29+
val format = LocalDateTime.Format {
30+
repeat(n) {
31+
alternativeParsing(
32+
{ monthNumber() },
33+
{ day() },
34+
primaryFormat = { hour() }
35+
)
36+
char('@')
37+
minute()
38+
char('#')
39+
second()
40+
}
41+
}
42+
blackhole.consume(format)
43+
}
44+
45+
@Benchmark
46+
fun formatCreationWithNestedAlternativeParsing(blackhole: Blackhole) {
47+
val format = LocalDateTime.Format {
48+
repeat(n) { index ->
49+
alternativeParsing(
50+
{ monthNumber(); char('-'); day() },
51+
{ day(); char('/'); monthNumber() },
52+
primaryFormat = { year(); char('-'); monthNumber(); char('-'); day() }
53+
)
54+
55+
if (index and 1 == 0) {
56+
alternativeParsing(
57+
{
58+
alternativeParsing(
59+
{ hour(); char(':'); minute() },
60+
{ minute(); char(':'); second() },
61+
primaryFormat = { hour(); char(':'); minute(); char(':'); second() }
62+
)
63+
},
64+
primaryFormat = {
65+
year(); char('-'); monthNumber(); char('-'); day()
66+
char('T')
67+
hour(); char(':'); minute(); char(':'); second()
68+
}
69+
)
70+
}
71+
72+
char('|')
73+
if (index % 3 == 0) {
74+
char('|')
75+
}
76+
77+
if (index and 2 == 0) {
78+
alternativeParsing(
79+
{ char('Z') },
80+
{ char('+'); hour(); char(':'); minute() },
81+
primaryFormat = { char('-'); hour(); char(':'); minute() }
82+
)
83+
}
84+
}
85+
}
86+
blackhole.consume(format)
87+
}
88+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2019-2025 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+
@file:Suppress("unused")
7+
8+
package kotlinx.datetime
9+
10+
import kotlinx.datetime.format.char
11+
import kotlinx.datetime.format.optional
12+
import org.openjdk.jmh.annotations.*
13+
import org.openjdk.jmh.infra.Blackhole
14+
import java.util.concurrent.*
15+
16+
@Warmup(iterations = 5, time = 1)
17+
@Measurement(iterations = 5, time = 1)
18+
@BenchmarkMode(Mode.AverageTime)
19+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
20+
@State(Scope.Benchmark)
21+
@Fork(1)
22+
open class PythonDateTimeFormatBenchmark {
23+
24+
@Benchmark
25+
fun buildPythonDateTimeFormat(blackhole: Blackhole) {
26+
val v = LocalDateTime.Format {
27+
year()
28+
char('-')
29+
monthNumber()
30+
char('-')
31+
day()
32+
char(' ')
33+
hour()
34+
char(':')
35+
minute()
36+
optional {
37+
char(':')
38+
second()
39+
optional {
40+
char('.')
41+
secondFraction()
42+
}
43+
}
44+
}
45+
blackhole.consume(v)
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019-2025 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+
@file:Suppress("unused")
7+
8+
package kotlinx.datetime
9+
10+
import kotlinx.datetime.format.char
11+
import org.openjdk.jmh.annotations.*
12+
import org.openjdk.jmh.infra.Blackhole
13+
import java.util.concurrent.TimeUnit
14+
15+
@Warmup(iterations = 5, time = 1)
16+
@Measurement(iterations = 5, time = 1)
17+
@BenchmarkMode(Mode.AverageTime)
18+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
19+
@State(Scope.Benchmark)
20+
@Fork(1)
21+
open class SerialFormatBenchmark {
22+
23+
@Param("1", "2", "4", "8", "16", "32", "64", "128", "256", "512", "1024")
24+
var n = 0
25+
26+
@Benchmark
27+
fun largeSerialFormat(blackhole: Blackhole) {
28+
val format = LocalDateTime.Format {
29+
repeat(n) {
30+
char('^')
31+
monthNumber()
32+
char('&')
33+
day()
34+
char('!')
35+
hour()
36+
char('$')
37+
minute()
38+
char('#')
39+
second()
40+
char('@')
41+
}
42+
}
43+
blackhole.consume(format)
44+
}
45+
}

0 commit comments

Comments
 (0)