Skip to content

Commit 6558c68

Browse files
Elena LepilkinaLepilkinaElena
authored andcommitted
Fixed internal mode after rebase
1 parent a195452 commit 6558c68

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

plugin/main/src/kotlinx/benchmark/gradle/NativeMultiplatformTasks.kt

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ open class NativeBenchmarkExec() : DefaultTask() {
176176
execute(listOf(configFile.absolutePath, "--list", benchsDescriptionDir.absolutePath))
177177
val detailedConfigFiles = project.fileTree(benchsDescriptionDir).files.sortedBy { it.absolutePath }
178178
val jsonReportParts = mutableListOf<File>()
179+
val runResults = mutableMapOf<String, String>()
179180

180181
detailedConfigFiles.forEach { runConfig ->
181182
val runConfigPath = runConfig.absolutePath
@@ -185,9 +186,11 @@ open class NativeBenchmarkExec() : DefaultTask() {
185186

186187
// Execute benchmark
187188
if (config.nativeIterationMode == "internal") {
188-
val jsonFile = createTempFile("bench", ".json").toFile()
189-
jsonReportParts.add(jsonFile)
190-
execute(listOf(configFile.absolutePath, "--internal", runConfigPath, jsonFile.absolutePath))
189+
val suiteResultsFile = createTempFile("bench", ".txt").toFile()
190+
execute(listOf(configFile.absolutePath, "--internal", runConfigPath, suiteResultsFile.absolutePath))
191+
val suiteResults = suiteResultsFile.readText()
192+
if (suiteResults.isNotEmpty())
193+
runResults[runConfigPath] = suiteResults
191194
} else {
192195
val iterations = currentConfigDescription.substringAfter("iterations=")
193196
.substringBefore(',').toInt()
@@ -209,7 +212,7 @@ open class NativeBenchmarkExec() : DefaultTask() {
209212
var iteration = 0
210213
while (!exceptionDuringExecution && iteration in 0 until iterations) {
211214
val textResult = createTempFile("bench", ".txt").toFile()
212-
execute(listOf(configFile.absolutePath, runConfigPath, iteration.toString(), textResult.absolutePath))
215+
execute(listOf(configFile.absolutePath, "--iteration", runConfigPath, iteration.toString(), textResult.absolutePath))
213216
val result = textResult.readLines()[0]
214217
if (result == "null")
215218
exceptionDuringExecution = true
@@ -218,23 +221,15 @@ open class NativeBenchmarkExec() : DefaultTask() {
218221
}
219222
// Store results
220223
if (iterationResults.size == iterations) {
221-
val samplesFile = createTempFile("bench_results").toFile()
222-
samplesFile.printWriter().use { out ->
223-
out.write(iterationResults.joinToString { it.toString() })
224-
}
225-
execute(listOf(configFile.absolutePath, runConfigPath, samplesFile.absolutePath))
226-
jsonReportParts.add(samplesFile)
224+
runResults[runConfigPath] = iterationResults.joinToString()
227225
}
228226
}
229227
}
230-
// Complete
231-
execute(listOf(configFile.absolutePath, "--complete"))
232-
// Merge reports
233-
val fullResults = jsonReportParts.map {
234-
it.readText()
235-
}.joinToString(",", prefix = "[", postfix = "\n]")
236-
reportFile.printWriter().use {
237-
it.print(fullResults)
228+
// Merge results
229+
val samplesFile = createTempFile("bench_results").toFile()
230+
samplesFile.printWriter().use { out ->
231+
out.write(runResults.toList().joinToString("\n") { "${it.first}: ${it.second}"})
238232
}
233+
execute(listOf(configFile.absolutePath, "--store-results", samplesFile.absolutePath))
239234
}
240235
}

runtime/nativeMain/src/kotlinx/benchmark/Utils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal fun String.parseBenchmarkConfig(): NativeExecutor.BenchmarkRun {
6262

6363
val content = readFile()
6464
val lines = content.lines().filter { it.isNotEmpty() }
65-
require(lines.size == 3, { "Wrong fromat of detailed benchmark configuration file. "})
65+
require(lines.size == 3, { "Wrong format of detailed benchmark configuration file. "})
6666
val name = lines[0].getElement("benchmark")
6767
val configuration = BenchmarkConfiguration.parse(lines[1].getElement("configuration"))
6868
val parameters = lines[2].getElement("parameters").parseMap()

runtime/nativeMain/src/kotlinx/benchmark/native/NativeExecutor.kt

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
7272
reporter.startBenchmark(executionName, id)
7373
val samples = run(benchmark, benchmarkRun)
7474
if (samples != null) {
75-
saveBenchmarkResults(benchmark, benchmarkRun, samples, resultsFile)
75+
writeFile(resultsFile, samples.joinToString())
76+
saveBenchmarkResults(benchmark, benchmarkRun, samples)
7677
}
7778
}
7879

@@ -107,16 +108,17 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
107108
} finally {
108109
benchmark.suite.teardown(instance)
109110
}
110-
exception?.let {
111+
if (exception != null) {
111112
val error = exception.toString()
112113
val stacktrace = exception.stacktrace()
113114
reporter.endBenchmarkException(executionName, id, error, stacktrace)
114115
return null
115-
} ?: return samples
116+
}
117+
return samples
116118
}
117119

118120
private fun saveBenchmarkResults(benchmark: BenchmarkDescriptor<Any?>, benchmarkRun: BenchmarkRun,
119-
samples: DoubleArray, fileName: String? = null) {
121+
samples: DoubleArray) {
120122
val id = id(benchmark.name, benchmarkRun.parameters)
121123
val result = ReportBenchmarksStatistics.createResult(benchmark, benchmarkRun.parameters, benchmarkRun.config, samples)
122124
val message = with(result) {
@@ -136,15 +138,20 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
136138
message
137139
)
138140
result(result)
139-
fileName?.let { writeFile(it, JsonBenchmarkReportFormatter.format(result)) }
140141
}
141142

142-
fun storeResults(benchmarks: List<BenchmarkDescriptor<Any?>>) {
143-
val (configFileName, fileName) = additionalArguments
144-
val benchmarkRun = configFileName.parseBenchmarkConfig()
145-
val benchmark = benchmarks.getBenchmark(benchmarkRun.benchmarkName)
146-
val samples = fileName.readFile().split(", ").map { it.toDouble() }.toDoubleArray()
147-
saveBenchmarkResults(benchmark, benchmarkRun, samples, fileName)
143+
private fun storeResults(benchmarks: List<BenchmarkDescriptor<Any?>>, complete: () -> Unit) {
144+
val (_, resultsFileName) = additionalArguments
145+
resultsFileName.readFile().split("\n").filter { it.isNotEmpty() }.forEach {
146+
val (configFileName, samplesList) = it.split(": ")
147+
val samples = samplesList.split(", ").map { it.toDouble() }.toDoubleArray()
148+
val benchmarkRun = configFileName.parseBenchmarkConfig()
149+
val benchmark = benchmarks.getBenchmark(benchmarkRun.benchmarkName)
150+
val result = ReportBenchmarksStatistics.createResult(benchmark, benchmarkRun.parameters,
151+
benchmarkRun.config, samples)
152+
result(result)
153+
}
154+
complete()
148155
}
149156

150157
override fun run(
@@ -154,18 +161,24 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
154161
start: () -> Unit,
155162
complete: () -> Unit
156163
) {
157-
when(additionalArguments.size) {
158-
1 -> complete()
159-
2 -> when (additionalArguments.first()) {
160-
"--list" -> outputBenchmarks(runnerConfiguration, benchmarks, start)
161-
else -> storeResults(benchmarks)
162-
}
163-
3 -> when (additionalArguments.first()) {
164-
"--internal" -> runBenchmark(benchmarks)
165-
else -> runBenchmarkIteration(benchmarks)
166-
}
167-
4 -> runBenchmarkWarmup(benchmarks)
164+
val knownActions = mapOf(
165+
"--list" to 2,
166+
"--store-results" to 2,
167+
"--internal" to 3,
168+
"--iteration" to 4,
169+
"--warmup" to 5
170+
)
168171

172+
val action = additionalArguments.first()
173+
if (action !in knownActions.keys)
174+
throw NoSuchElementException("Action $action isn't found in the list of possible actions ${knownActions.keys}.")
175+
require(knownActions[action] == additionalArguments.size)
176+
when (action) {
177+
"--list" -> outputBenchmarks(runnerConfiguration, benchmarks, start)
178+
"--store-results" -> storeResults(benchmarks, complete)
179+
"--internal" -> runBenchmark(benchmarks)
180+
"--iteration" -> runBenchmarkIteration(benchmarks)
181+
"--warmup" -> runBenchmarkWarmup(benchmarks)
169182
}
170183
}
171184

0 commit comments

Comments
 (0)