@@ -3,22 +3,153 @@ package kotlinx.benchmark.integration
3
3
import kotlin.test.*
4
4
5
5
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
+
6
22
@Test
7
23
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)
11
33
}
34
+ )
35
+ }
12
36
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
+ }
14
52
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)
20
64
}
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
+ )
22
153
}
23
154
24
155
@Test
0 commit comments