|
| 1 | +package kotlinx.benchmark |
| 2 | + |
| 3 | +import kotlin.math.* |
| 4 | + |
| 5 | +sealed class BenchmarkReportFormatter { |
| 6 | + abstract fun format(results: Collection<ReportBenchmarkResult>): String |
| 7 | + |
| 8 | + companion object { |
| 9 | + fun create(format: String): BenchmarkReportFormatter = when (format.toLowerCase()) { |
| 10 | + "json" -> JsonBenchmarkReportFormatter |
| 11 | + "csv" -> CsvBenchmarkReportFormatter(",") |
| 12 | + "scsv" -> CsvBenchmarkReportFormatter(";") |
| 13 | + "text" -> TextBenchmarkReportFormatter |
| 14 | + else -> throw UnsupportedOperationException("Report format $format is not supported.") |
| 15 | + } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +internal object TextBenchmarkReportFormatter : BenchmarkReportFormatter() { |
| 20 | + private const val padding = 2 |
| 21 | + |
| 22 | + override fun format(results: Collection<ReportBenchmarkResult>): String { |
| 23 | + fun columnLength(column: String, selector: (ReportBenchmarkResult) -> String): Int = |
| 24 | + max(column.length, results.maxOf { selector(it).length }) |
| 25 | + |
| 26 | + val shortNames = denseBenchmarkNames(results.map { it.benchmark.name }) |
| 27 | + val nameLength = columnLength("Benchmark") { shortNames[it.benchmark.name]!! } |
| 28 | + val paramNames = results.flatMap { it.params.keys }.toSet() |
| 29 | + val paramLengths = paramNames.associateWith { paramName -> |
| 30 | + max(paramName.length + 2, results.mapNotNull { it.params[paramName] }.maxOf { it.length }) + padding |
| 31 | + } |
| 32 | + val modeLength = columnLength("Mode") { it.config.mode.toText() } + padding |
| 33 | + val samplesLength = columnLength("Cnt") { it.values.size.toString() } + padding |
| 34 | + val scopeLength = columnLength("Score") { it.score.format(3, useGrouping = false) } + padding |
| 35 | + val errorLength = columnLength("Error") { it.error.format(3, useGrouping = false) } + padding - 1 |
| 36 | + val unitsLength = columnLength("Units") { unitText(it.config.mode, it.config.outputTimeUnit) } + padding |
| 37 | + |
| 38 | + return buildString { |
| 39 | + appendPaddedAfter("Benchmark", nameLength) |
| 40 | + paramNames.forEach { |
| 41 | + appendPaddedBefore("($it)", paramLengths[it]!!) |
| 42 | + } |
| 43 | + appendPaddedBefore("Mode", modeLength) |
| 44 | + appendPaddedBefore("Cnt", samplesLength) |
| 45 | + appendPaddedBefore("Score", scopeLength) |
| 46 | + append(" ") |
| 47 | + appendPaddedBefore("Error", errorLength) |
| 48 | + appendPaddedBefore("Units", unitsLength) |
| 49 | + appendLine() |
| 50 | + |
| 51 | + results.forEach { result -> |
| 52 | + appendPaddedAfter(shortNames[result.benchmark.name]!!, nameLength) |
| 53 | + paramNames.forEach { |
| 54 | + appendPaddedBefore(result.params[it] ?: "N/A", paramLengths[it]!!) |
| 55 | + } |
| 56 | + appendPaddedBefore(result.config.mode.toText(), modeLength) |
| 57 | + appendPaddedBefore(result.values.size.takeIf { it > 1 }?.toString() ?: " ", samplesLength) |
| 58 | + appendPaddedBefore(result.score.format(3, useGrouping = false), scopeLength) |
| 59 | + if (result.error.isNaNOrZero()) { |
| 60 | + append(" ") |
| 61 | + appendPaddedBefore("", errorLength) |
| 62 | + } else { |
| 63 | + append(" \u00B1") |
| 64 | + appendPaddedBefore(result.error.format(3, useGrouping = false), errorLength) |
| 65 | + } |
| 66 | + appendPaddedBefore(unitText(result.config.mode, result.config.outputTimeUnit), unitsLength) |
| 67 | + appendLine() |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private fun StringBuilder.appendSpace(l: Int): StringBuilder = append(" ".repeat(l)) |
| 73 | + |
| 74 | + private fun StringBuilder.appendPaddedBefore(value: String, l: Int): StringBuilder = |
| 75 | + appendSpace(l - value.length).append(value) |
| 76 | + |
| 77 | + private fun StringBuilder.appendPaddedAfter(value: String, l: Int): StringBuilder = |
| 78 | + append(value).appendSpace(l - value.length) |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * Algorithm: |
| 83 | + * 1. remove package names, if it is the same for all benchmarks |
| 84 | + * 2. if not, shorthand same package names |
| 85 | + * |
| 86 | + * (jmh similar logic) |
| 87 | + */ |
| 88 | + private fun denseBenchmarkNames(src: List<String>): Map<String, String> { |
| 89 | + if (src.isEmpty()) return emptyMap() |
| 90 | + |
| 91 | + var first = true |
| 92 | + var prefixCut = false |
| 93 | + |
| 94 | + val prefix = src.fold(emptyList<String>()) { prefix, s -> |
| 95 | + val names = s.split(".") |
| 96 | + if (first) { |
| 97 | + first = false |
| 98 | + names.takeWhile { it.toLowerCase() == it } |
| 99 | + } else { |
| 100 | + val common = prefix.zip(names).takeWhile { (p, n) -> p == n && n.toLowerCase() == n } |
| 101 | + if (prefix.size != common.size) prefixCut = true |
| 102 | + prefix.take(common.size) |
| 103 | + } |
| 104 | + }.map { if (prefixCut) it[0].toString() else "" } |
| 105 | + |
| 106 | + return src.associateWith { s -> |
| 107 | + val names = prefix + s.split(".").drop(prefix.size) |
| 108 | + names.joinToString("") { if (it.isNotEmpty()) "$it." else "" }.removeSuffix(".") |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +private class CsvBenchmarkReportFormatter(val delimiter: String) : BenchmarkReportFormatter() { |
| 114 | + override fun format(results: Collection<ReportBenchmarkResult>): String = buildString { |
| 115 | + val allParams = results.flatMap { it.params.keys }.toSet() |
| 116 | + appendHeader(allParams) |
| 117 | + results.forEach { |
| 118 | + appendResult(allParams, it) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private fun StringBuilder.appendHeader(params: Set<String>) { |
| 123 | + appendEscaped("Benchmark").append(delimiter) |
| 124 | + appendEscaped("Mode").append(delimiter) |
| 125 | + appendEscaped("Threads").append(delimiter) |
| 126 | + appendEscaped("Samples").append(delimiter) |
| 127 | + appendEscaped("Score").append(delimiter) |
| 128 | + appendEscaped("Score Error (99.9%)").append(delimiter) |
| 129 | + appendEscaped("Unit") |
| 130 | + params.forEach { |
| 131 | + append(delimiter) |
| 132 | + appendEscaped("Param: $it") |
| 133 | + } |
| 134 | + append("\r\n") |
| 135 | + } |
| 136 | + |
| 137 | + private fun StringBuilder.appendResult(params: Set<String>, result: ReportBenchmarkResult) { |
| 138 | + appendEscaped(result.benchmark.name).append(delimiter) |
| 139 | + appendEscaped(result.config.mode.toText()).append(delimiter) |
| 140 | + append(1).append(delimiter) |
| 141 | + append(result.values.size).append(delimiter) |
| 142 | + append(result.score.format(6, useGrouping = false)).append(delimiter) |
| 143 | + append(result.error.format(6, useGrouping = false)).append(delimiter) |
| 144 | + appendEscaped(unitText(result.config.mode, result.config.outputTimeUnit)) |
| 145 | + params.forEach { |
| 146 | + append(delimiter) |
| 147 | + result.params[it]?.let { param -> |
| 148 | + appendEscaped(param) |
| 149 | + } |
| 150 | + } |
| 151 | + append("\r\n") |
| 152 | + } |
| 153 | + |
| 154 | + private fun StringBuilder.appendEscaped(value: String): StringBuilder = |
| 155 | + append("\"").append(value.replace("\"", "\"\"")).append("\"") |
| 156 | + |
| 157 | +} |
| 158 | + |
| 159 | +private object JsonBenchmarkReportFormatter : BenchmarkReportFormatter() { |
| 160 | + |
| 161 | + override fun format(results: Collection<ReportBenchmarkResult>): String = |
| 162 | + results.joinToString(",", prefix = "[", postfix = "\n]", transform = this::format) |
| 163 | + |
| 164 | + private fun format(result: ReportBenchmarkResult): String = |
| 165 | + """ |
| 166 | + { |
| 167 | + "benchmark" : "${result.benchmark.name}", |
| 168 | + "mode" : "${result.config.mode.toText()}", |
| 169 | + "warmupIterations" : ${result.config.warmups}, |
| 170 | + "warmupTime" : "${result.config.iterationTime} ${result.config.iterationTimeUnit.toText()}", |
| 171 | + "measurementIterations" : ${result.config.iterations}, |
| 172 | + "measurementTime" : "${result.config.iterationTime} ${result.config.iterationTimeUnit.toText()}", |
| 173 | + "params" : { |
| 174 | + ${result.params.entries.joinToString(separator = ",\n ") { "\"${it.key}\" : \"${it.value}\"" }} |
| 175 | + }, |
| 176 | + "primaryMetric" : { |
| 177 | + "score": ${result.score}, |
| 178 | + "scoreError": ${result.error}, |
| 179 | + "scoreConfidence" : [ |
| 180 | + ${result.confidence.first}, |
| 181 | + ${result.confidence.second} |
| 182 | + ], |
| 183 | + "scorePercentiles" : { |
| 184 | + ${result.percentiles.entries.joinToString(separator = ",\n ") { "\"${it.key.format(2)}\" : ${it.value}" }} |
| 185 | + }, |
| 186 | + "scoreUnit" : "${unitText(result.config.mode, result.config.outputTimeUnit)}", |
| 187 | + "rawData" : [ |
| 188 | + ${ |
| 189 | + result.values.joinToString( |
| 190 | + prefix = "[\n ", |
| 191 | + postfix = "\n ]", |
| 192 | + separator = ",\n " |
| 193 | + ) |
| 194 | + } |
| 195 | + ] |
| 196 | + }, |
| 197 | + "secondaryMetrics" : { |
| 198 | + } |
| 199 | + }""" |
| 200 | + |
| 201 | +} |
0 commit comments