|
| 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 | +} |
0 commit comments