Skip to content

Commit b531ef1

Browse files
Abduqodiri Qurbonzodaqurbonzoda
authored andcommitted
fixup! Test that the configuration options override the annotations
1 parent cd6a1a7 commit b531ef1

File tree

5 files changed

+209
-83
lines changed

5 files changed

+209
-83
lines changed

integration/src/test/kotlin/kotlinx/benchmark/integration/ConfigurationTest.kt

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package kotlinx.benchmark.integration
2+
3+
import kotlin.test.*
4+
5+
class OptionsOverrideAnnotationsTest : GradleTest() {
6+
7+
private fun testConfiguration(
8+
setupBlock: BenchmarkConfiguration.() -> Unit,
9+
checkBlock: (reportText: String, consoleOutput: String) -> Unit
10+
) {
11+
project("config-options") {
12+
configuration("config") {
13+
setupBlock()
14+
}
15+
}.run("nativeConfigBenchmark") {
16+
val reportText = reports("config").single().readText()
17+
checkBlock(reportText, output)
18+
}
19+
}
20+
21+
private fun Regex.testMatches(reportText: String, expectedResult: String) =
22+
testMatches(reportText, listOf(expectedResult))
23+
24+
private fun Regex.testMatches(reportText: String, expectedResults: List<String>) {
25+
var match = this.find(reportText)
26+
for (expectedResult in expectedResults) {
27+
assertNotNull(match, message = "Expected $expectedResult, but was null")
28+
val result = match.groupValues[1]
29+
assertEquals(expectedResult, result)
30+
31+
match = match.next()
32+
}
33+
assertNull(match)
34+
}
35+
36+
@Test
37+
fun testIterations() {
38+
val expectedIterations = 2
39+
40+
testConfiguration(
41+
setupBlock = {
42+
iterations = expectedIterations
43+
},
44+
checkBlock = { reportText, consoleOutput ->
45+
Regex(""""measurementIterations" : (\d+)""")
46+
.testMatches(reportText, "$expectedIterations")
47+
48+
Regex("^Iteration #(\\d+)", RegexOption.MULTILINE)
49+
.testMatches(consoleOutput, List(expectedIterations) { "$it" })
50+
51+
}
52+
)
53+
}
54+
55+
@Test
56+
fun testWarmups() {
57+
val expectedWarmups = 2
58+
59+
testConfiguration(
60+
setupBlock = {
61+
warmups = expectedWarmups
62+
},
63+
checkBlock = { reportText, consoleOutput ->
64+
Regex(""""warmupIterations" : (\d+)""")
65+
.testMatches(reportText, "$expectedWarmups")
66+
67+
Regex("^Warm-up #(\\d+)", RegexOption.MULTILINE)
68+
.testMatches(consoleOutput, List(expectedWarmups) { "$it" })
69+
}
70+
)
71+
}
72+
73+
@Test
74+
fun testIterationTime() {
75+
val expectedIterationTime = "400 ms"
76+
77+
testConfiguration(
78+
setupBlock = {
79+
iterationTime = 400
80+
iterationTimeUnit = "ms"
81+
},
82+
checkBlock = { reportText, _ ->
83+
Regex(""""measurementTime" : "(.*)"""")
84+
.testMatches(reportText, expectedIterationTime)
85+
Regex(""""warmupTime" : "(.*)"""")
86+
.testMatches(reportText, expectedIterationTime)
87+
}
88+
)
89+
}
90+
91+
@Test
92+
fun testOutputTimeUnit() {
93+
val expectedOutputTimeUnit = "ns"
94+
val expectedCount = /*warmups*/3 + /*iterations*/5 + /*Success:*/1 + /*summary*/1
95+
96+
testConfiguration(
97+
setupBlock = {
98+
outputTimeUnit = expectedOutputTimeUnit
99+
},
100+
checkBlock = { reportText, consoleOutput ->
101+
Regex(""""scoreUnit" : "(.*)"""")
102+
.testMatches(reportText, "ops/$expectedOutputTimeUnit")
103+
104+
Regex("ops/(\\w+)")
105+
.testMatches(consoleOutput, List(expectedCount) { expectedOutputTimeUnit })
106+
}
107+
)
108+
}
109+
110+
@Test
111+
fun testMode() {
112+
val expectedMode = "avgt"
113+
val expectedCount = /*warmups*/3 + /*iterations*/5 + /*Success:*/1 + /*summary*/1
114+
115+
testConfiguration(
116+
setupBlock = {
117+
mode = expectedMode
118+
},
119+
checkBlock = { reportText, consoleOutput ->
120+
Regex(""""mode" : "(.*)"""")
121+
.testMatches(reportText, expectedMode)
122+
123+
Regex("(\\w+)/op")
124+
.testMatches(consoleOutput, List(expectedCount) { "ms" })
125+
126+
val lines = consoleOutput.lines()
127+
val summaryIndex = lines.indexOfFirst { it.contains("native summary:") }
128+
val benchmarkLine = lines[summaryIndex + 2]
129+
val mode = benchmarkLine.split(Regex("\\s+"))[2]
130+
assertEquals(expectedMode, mode, benchmarkLine)
131+
}
132+
)
133+
}
134+
135+
@Test
136+
fun testParams() {
137+
testConfiguration(
138+
setupBlock = {
139+
param("data", 5.0, 12.5)
140+
},
141+
checkBlock = { reportText, consoleOutput ->
142+
Regex(""""params" : \{\s*"data" : "(\S*)"\s*}""")
143+
.testMatches(reportText, listOf("5.0", "12.5"))
144+
145+
Regex("test.CommonBenchmark.mathBenchmark [|] data=(.*)")
146+
.testMatches(consoleOutput, listOf("5.0", "12.5"))
147+
}
148+
)
149+
}
150+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import org.jetbrains.kotlin.konan.target.KonanTarget
2+
import org.jetbrains.kotlin.konan.target.HostManager
3+
4+
kotlin {
5+
jvm { }
6+
js('jsIr', IR) { nodejs() }
7+
wasm('wasmJs') { d8() }
8+
9+
if (HostManager.hostIsLinux) linuxX64('native')
10+
if (HostManager.hostIsMingw) mingwX64('native')
11+
if (HostManager.host == KonanTarget.MACOS_X64.INSTANCE) macosX64('native')
12+
if (HostManager.host == KonanTarget.MACOS_ARM64.INSTANCE) macosArm64('native')
13+
14+
sourceSets {
15+
commonMain {
16+
dependencies {
17+
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.5.0-SNAPSHOT")
18+
}
19+
}
20+
jvmMain {
21+
}
22+
jsIrMain {
23+
}
24+
wasmJsMain {
25+
}
26+
nativeMain {
27+
}
28+
}
29+
}
30+
31+
benchmark {
32+
targets {
33+
register("jvm")
34+
register("jsIr")
35+
register("wasmJs")
36+
register("native")
37+
}
38+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.gradle.jvmargs=-Xmx2g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test
2+
3+
import kotlinx.benchmark.*
4+
import kotlin.math.*
5+
6+
@State(Scope.Benchmark)
7+
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
8+
@Warmup(iterations = 5, time = 500, timeUnit = BenchmarkTimeUnit.MILLISECONDS)
9+
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
10+
@BenchmarkMode(Mode.Throughput)
11+
open class CommonBenchmark {
12+
13+
@Param("3.0")
14+
var data: Double = 0.0
15+
16+
@Benchmark
17+
fun mathBenchmark(): Double {
18+
return log(sqrt(data) * cos(data), 2.0)
19+
}
20+
}

0 commit comments

Comments
 (0)