Skip to content

Commit 1818322

Browse files
Elena LepilkinaLepilkinaElena
authored andcommitted
Fixed extrenal benchmarks run mode
1 parent 6558c68 commit 1818322

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,27 @@ open class NativeBenchmarkExec() : DefaultTask() {
198198
.substringBefore(',').toInt()
199199
// Warm up
200200
var exceptionDuringExecution = false
201-
for(i in 0 until warmups) {
202-
val textResult = createTempFile("bench", ".txt").toFile()
201+
var textResult: File? = null
202+
for (i in 0 until warmups) {
203+
textResult = createTempFile("bench", ".txt").toFile()
203204
execute(listOf(configFile.absolutePath, "--warmup", runConfigPath, i.toString(), textResult.absolutePath))
204205
val result = textResult.readLines().getOrNull(0)
205206
if (result == "null") {
206207
exceptionDuringExecution = true
207208
break
208209
}
209210
}
211+
// Get cycles number
212+
val cycles = if (!exceptionDuringExecution && textResult != null) textResult.readText() else "1"
210213
// Execution
211214
val iterationResults = mutableListOf<Double>()
212215
var iteration = 0
213216
while (!exceptionDuringExecution && iteration in 0 until iterations) {
214217
val textResult = createTempFile("bench", ".txt").toFile()
215-
execute(listOf(configFile.absolutePath, "--iteration", runConfigPath, iteration.toString(), textResult.absolutePath))
218+
execute(
219+
listOf(configFile.absolutePath, "--iteration", runConfigPath, iteration.toString(),
220+
cycles, textResult.absolutePath)
221+
)
216222
val result = textResult.readLines()[0]
217223
if (result == "null")
218224
exceptionDuringExecution = true
@@ -221,6 +227,13 @@ open class NativeBenchmarkExec() : DefaultTask() {
221227
}
222228
// Store results
223229
if (iterationResults.size == iterations) {
230+
val iterationsResultsFile = createTempFile("bench_results").toFile()
231+
iterationsResultsFile.printWriter().use { out ->
232+
out.write(iterationResults.joinToString { it.toString() })
233+
}
234+
execute(
235+
listOf(configFile.absolutePath, "--end-run", runConfigPath, iterationsResultsFile.absolutePath)
236+
)
224237
runResults[runConfigPath] = iterationResults.joinToString()
225238
}
226239
}

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
3434
?: throw NoSuchElementException("Benchmark $name wasn't found.")
3535

3636
private fun runBenchmarkIteration(benchmarks: List<BenchmarkDescriptor<Any?>>) {
37-
val (configFileName, iteration, resultsFile) = additionalArguments
37+
val (_, configFileName, iteration, cycles, resultsFile) = additionalArguments
3838
val benchmarkRun = configFileName.parseBenchmarkConfig()
3939
val benchmark = benchmarks.getBenchmark(benchmarkRun.benchmarkName)
40-
val samples = run(benchmark, benchmarkRun, iteration.toInt())
40+
val samples = run(benchmark, benchmarkRun, iteration.toInt(), cycles.toInt())
4141
writeFile(resultsFile, samples?.let{ it[0].toString() } ?: "null")
4242
}
4343

@@ -46,14 +46,18 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
4646
val benchmarkRun = configFileName.parseBenchmarkConfig()
4747
val benchmark = benchmarks.getBenchmark(benchmarkRun.benchmarkName)
4848
val id = id(benchmark.name, benchmarkRun.parameters)
49-
if (iteration.toInt() == 0) {
50-
reporter.startBenchmark(executionName, id)
51-
}
49+
5250
val instance = benchmark.suite.factory() // TODO: should we create instance per bench or per suite?
5351
benchmark.suite.parametrize(instance, benchmarkRun.parameters)
5452
benchmark.suite.setup(instance)
53+
54+
if (iteration.toInt() == 0) {
55+
reporter.startBenchmark(executionName, id)
56+
}
5557
try {
56-
warmup(benchmark.suite.name, benchmarkRun.config, benchmark.suite.factory(), benchmark, iteration.toInt())
58+
val iterations = warmup(benchmark.suite.name, benchmarkRun.config, instance,
59+
benchmark, iteration.toInt())
60+
writeFile(resultsFile, iterations.toString())
5761
} catch (e: Throwable) {
5862
val error = e.toString()
5963
val stacktrace = e.stacktrace()
@@ -77,8 +81,16 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
7781
}
7882
}
7983

84+
private fun endExternalBenchmarksRun(benchmarks: List<BenchmarkDescriptor<Any?>>) {
85+
val (_, configFileName, samplesFile) = additionalArguments
86+
val samples = samplesFile.readFile().split(", ").map { it.toDouble() }.toDoubleArray()
87+
val benchmarkRun = configFileName.parseBenchmarkConfig()
88+
val benchmark = benchmarks.getBenchmark(benchmarkRun.benchmarkName)
89+
saveBenchmarkResults(benchmark, benchmarkRun, samples)
90+
}
91+
8092
fun run(benchmark: BenchmarkDescriptor<Any?>, benchmarkRun: BenchmarkRun,
81-
externalIterationNumber: Int? = null): DoubleArray? {
93+
externalIterationNumber: Int? = null, externalCyclesNumber: Int? = null): DoubleArray? {
8294
val id = id(benchmark.name, benchmarkRun.parameters)
8395
val suite = benchmark.suite
8496

@@ -90,7 +102,7 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
90102
val iterations = if (benchmarkRun.config.nativeIterationMode == NativeIterationMode.External) 1 else benchmarkRun.config.iterations
91103
val samples = try {
92104
// Execute warmup
93-
val cycles = warmup(suite.name, benchmarkRun.config, instance, benchmark)
105+
val cycles = externalCyclesNumber ?: warmup(suite.name, benchmarkRun.config, instance, benchmark)
94106
DoubleArray(iterations) { iteration ->
95107
val nanosecondsPerOperation = measure(instance, benchmark, cycles)
96108
val text = nanosecondsPerOperation.nanosToText(benchmarkRun.config.mode, benchmarkRun.config.outputTimeUnit)
@@ -164,9 +176,10 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
164176
val knownActions = mapOf(
165177
"--list" to 2,
166178
"--store-results" to 2,
179+
"--end-run" to 3,
167180
"--internal" to 3,
168-
"--iteration" to 4,
169-
"--warmup" to 5
181+
"--iteration" to 5,
182+
"--warmup" to 4
170183
)
171184

172185
val action = additionalArguments.first()
@@ -179,6 +192,7 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
179192
"--internal" -> runBenchmark(benchmarks)
180193
"--iteration" -> runBenchmarkIteration(benchmarks)
181194
"--warmup" -> runBenchmarkWarmup(benchmarks)
195+
"--end-run" -> endExternalBenchmarksRun(benchmarks)
182196
}
183197
}
184198

0 commit comments

Comments
 (0)