Skip to content

Commit 177290c

Browse files
Abduqodiri Qurbonzodaqurbonzoda
authored andcommitted
Add tests for SuiteSourceGenerator
1 parent fd0bfa0 commit 177290c

File tree

5 files changed

+210
-10
lines changed

5 files changed

+210
-10
lines changed

integration/src/main/kotlin/kotlinx/benchmark/integration/AnnotationsSpecifier.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ class AnnotationsSpecifier {
1212
)
1313
}
1414

15+
fun warmup(iterations: Int, time: Int, timeUnit: String) {
16+
classAnnotations.add(
17+
Annotation("@Warmup", listOf(iterations, time, timeUnit))
18+
)
19+
}
20+
1521
fun outputTimeUnit(timeUnit: String) {
1622
classAnnotations.add(
1723
Annotation("@OutputTimeUnit", listOf(timeUnit))

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

Lines changed: 141 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,153 @@ package kotlinx.benchmark.integration
33
import kotlin.test.*
44

55
class SuiteSourceGeneratorTest : GradleTest() {
6+
private fun Runner.assertGeneratedDescriptorContainsCode(code: String) {
7+
val regex = code.replace(" ", "\\s*").toRegex(RegexOption.DOT_MATCHES_ALL)
8+
generatedDir("native", "test/CommonBenchmark_Descriptor.kt") { descriptorFile ->
9+
val text = descriptorFile.readText()
10+
assertTrue(text.contains(regex), "Regex: <$regex> not found in <$text>")
11+
}
12+
}
13+
14+
private inline fun testSourceGenerator(setupBlock: Runner.() -> Unit, checkBlock: Runner.() -> Unit) {
15+
project("source-generation").apply {
16+
setupBlock()
17+
run("nativeBenchmarkGenerate")
18+
checkBlock()
19+
}
20+
}
21+
622
@Test
723
fun measurementAnnotation() {
8-
project("kotlin-multiplatform", true).let { runner ->
9-
runner.updateAnnotations("src/commonMain/kotlin/CommonBenchmark.kt") {
10-
measurement(iterations = 5, time = 200, timeUnit = "BenchmarkTimeUnit.MILLISECONDS")
24+
testSourceGenerator(
25+
setupBlock = {
26+
updateAnnotations("src/commonMain/kotlin/CommonBenchmark.kt") {
27+
measurement(iterations = 12, time = 200, timeUnit = "BenchmarkTimeUnit.MILLISECONDS")
28+
}
29+
},
30+
checkBlock = {
31+
val parameters = "iterations = 12, warmups = 5, iterationTime = IterationTime\\(200, BenchmarkTimeUnit.MILLISECONDS\\)"
32+
assertGeneratedDescriptorContainsCode(parameters)
1133
}
34+
)
35+
}
1236

13-
runner.run("nativeBenchmarkGenerate")
37+
@Test
38+
fun warmupAnnotation() {
39+
testSourceGenerator(
40+
setupBlock = {
41+
updateAnnotations("src/commonMain/kotlin/CommonBenchmark.kt") {
42+
warmup(iterations = 12, time = 200, timeUnit = "BenchmarkTimeUnit.MILLISECONDS")
43+
}
44+
},
45+
checkBlock = {
46+
// time and timeUnit of @Warmup are ignored: https://github.com/Kotlin/kotlinx-benchmark/issues/74
47+
val parameters = "iterations = 3, warmups = 12, iterationTime = IterationTime\\(1, BenchmarkTimeUnit.SECONDS\\)"
48+
assertGeneratedDescriptorContainsCode(parameters)
49+
}
50+
)
51+
}
1452

15-
runner.generatedDir("native", "test/CommonBenchmark_Descriptor.kt") { descriptorFile ->
16-
val text = descriptorFile.readText()
17-
val parameters = "iterations = 5, iterationTime = IterationTime\\(200, BenchmarkTimeUnit.MILLISECONDS\\)"
18-
.replace(" ", "\\s+").toRegex()
19-
assertTrue(text.contains(parameters), "Parameters: <$parameters> not found in <$text>")
53+
@Test
54+
fun outputTimeUnitAnnotation() {
55+
testSourceGenerator(
56+
setupBlock = {
57+
updateAnnotations("src/commonMain/kotlin/CommonBenchmark.kt") {
58+
outputTimeUnit("BenchmarkTimeUnit.SECONDS")
59+
}
60+
},
61+
checkBlock = {
62+
val parameters = "outputTimeUnit = BenchmarkTimeUnit.SECONDS"
63+
assertGeneratedDescriptorContainsCode(parameters)
2064
}
21-
}
65+
)
66+
}
67+
68+
@Test
69+
fun benchmarkModeAnnotation() {
70+
testSourceGenerator(
71+
setupBlock = {
72+
updateAnnotations("src/commonMain/kotlin/CommonBenchmark.kt") {
73+
benchmarkMode("Mode.AverageTime")
74+
}
75+
},
76+
checkBlock = {
77+
val parameters = "mode = Mode.AverageTime"
78+
assertGeneratedDescriptorContainsCode(parameters)
79+
}
80+
)
81+
}
82+
83+
@Test
84+
fun setupAnnotation() {
85+
testSourceGenerator(
86+
setupBlock = {
87+
addAnnotation("src/commonMain/kotlin/CommonBenchmark.kt") {
88+
setup("function1")
89+
setup("function2")
90+
}
91+
},
92+
checkBlock = {
93+
val functionCalls = """((instance.function1\(\).*instance.function2\(\))|(instance.function2\(\).*instance.function1\(\)))"""
94+
val regex = "private fun setUp\\(instance: CommonBenchmark\\) \\{ $functionCalls }"
95+
assertGeneratedDescriptorContainsCode(regex)
96+
}
97+
)
98+
}
99+
100+
@Test
101+
fun benchmarkAnnotation() {
102+
testSourceGenerator(
103+
setupBlock = {
104+
addAnnotation("src/commonMain/kotlin/CommonBenchmark.kt") {
105+
benchmark("function1")
106+
benchmark("function2")
107+
}
108+
},
109+
checkBlock = {
110+
val function1Ref = """CommonBenchmark::function1"""
111+
val function2Ref = """CommonBenchmark::function2"""
112+
assertGeneratedDescriptorContainsCode(function1Ref)
113+
assertGeneratedDescriptorContainsCode(function2Ref)
114+
}
115+
)
116+
}
117+
118+
@Test
119+
fun teardownAnnotation() {
120+
testSourceGenerator(
121+
setupBlock = {
122+
addAnnotation("src/commonMain/kotlin/CommonBenchmark.kt") {
123+
teardown("function1")
124+
teardown("function2")
125+
}
126+
},
127+
checkBlock = {
128+
val functionCalls = """((instance.function1\(\).*instance.function2\(\))|(instance.function2\(\).*instance.function1\(\)))"""
129+
val regex = "private fun tearDown\\(instance: CommonBenchmark\\) \\{ $functionCalls } "
130+
assertGeneratedDescriptorContainsCode(regex)
131+
}
132+
)
133+
}
134+
135+
@Test
136+
fun paramFieldAnnotation() {
137+
testSourceGenerator(
138+
setupBlock = {
139+
addAnnotation("src/commonMain/kotlin/CommonBenchmark.kt") {
140+
param("data1", "1", "2")
141+
param("data2", "a", "b")
142+
}
143+
},
144+
checkBlock = {
145+
val parameterList = "parameters = listOf\\(\"data1\", \"data2\"\\)"
146+
val data1Values = "\"data1\" to listOf\\(\"\"\"1\"\"\", \"\"\"2\"\"\"\\)"
147+
val data2Values = "\"data2\" to listOf\\(\"\"\"a\"\"\", \"\"\"b\"\"\"\\)"
148+
assertGeneratedDescriptorContainsCode(parameterList)
149+
assertGeneratedDescriptorContainsCode(data1Values)
150+
assertGeneratedDescriptorContainsCode(data2Values)
151+
}
152+
)
22153
}
23154

24155
@Test
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
var data1: Int = 0
14+
15+
var data2: String = ""
16+
17+
fun function1() {
18+
// println("function1")
19+
}
20+
21+
fun function2() {
22+
// println("function2")
23+
}
24+
}

0 commit comments

Comments
 (0)