From 7feb87ff052b0a99b5790cf788814fbee46f08cc Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Sat, 19 Jun 2021 20:52:26 +0700 Subject: [PATCH] Refactor benchmark report writer --- build.gradle.kts | 4 +- buildSrc/build.gradle.kts | 5 +- .../benchmarks/addBenchmarkProperties.kt | 89 +++++++++---------- settings.gradle.kts | 2 +- 4 files changed, 47 insertions(+), 53 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9978281f2..93cd67d47 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,9 +10,7 @@ allprojects { maven("http://logicrunch.research.it.uu.se/maven") { isAllowInsecureProtocol = true } - maven("https://oss.sonatype.org/content/repositories/snapshots") { - - } + maven("https://oss.sonatype.org/content/repositories/snapshots") mavenCentral() } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 8ff1c4369..fe69b05c6 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -4,13 +4,14 @@ plugins { } repositories { - gradlePluginPortal() + maven("https://repo.kotlin.link") mavenCentral() + gradlePluginPortal() } dependencies { api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0") - api("ru.mipt.npm:gradle-tools:0.9.10") + api("ru.mipt.npm:gradle-tools:0.10.0") api("org.jetbrains.kotlinx:kotlinx-benchmark-plugin:0.3.1") } diff --git a/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/addBenchmarkProperties.kt b/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/addBenchmarkProperties.kt index 31d952157..b55e1320e 100644 --- a/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/addBenchmarkProperties.kt +++ b/buildSrc/src/main/kotlin/space/kscience/kmath/benchmarks/addBenchmarkProperties.kt @@ -5,8 +5,11 @@ package space.kscience.kmath.benchmarks +import kotlinx.benchmark.gradle.BenchmarksExtension import kotlinx.serialization.* +import kotlinx.serialization.json.* import org.gradle.api.Project +import ru.mipt.npm.gradle.KScienceReadmeExtension import java.time.* import java.time.format.* import java.time.temporal.ChronoField.* @@ -42,63 +45,55 @@ private fun noun(number: Number, singular: String, plural: String) = if (number. fun Project.addBenchmarkProperties() { val benchmarksProject = this rootProject.subprojects.forEach { p -> - p.extensions.findByType(ru.mipt.npm.gradle.KScienceReadmeExtension::class.java)?.run { - benchmarksProject.extensions.findByType(kotlinx.benchmark.gradle.BenchmarksExtension::class.java)?.configurations?.forEach { cfg -> - // TODO remove this hack when https://github.com/mipt-npm/gradle-tools/pull/15 is merged - @Suppress("UNCHECKED_CAST") - (javaClass.getDeclaredField("properties") - .also { - it.isAccessible = true - }[this] as MutableMap Any?>)["benchmark${cfg.name.replaceFirstChar(Char::uppercase)}"] = - { - val launches = benchmarksProject.buildDir.resolve("reports/benchmarks/${cfg.name}") + p.extensions.findByType(KScienceReadmeExtension::class.java)?.run { + benchmarksProject.extensions.findByType(BenchmarksExtension::class.java)?.configurations?.forEach { cfg -> + property("benchmark${cfg.name.replaceFirstChar(Char::uppercase)}") { + val launches = benchmarksProject.buildDir.resolve("reports/benchmarks/${cfg.name}") - val resDirectory = launches.listFiles()?.maxByOrNull { - LocalDateTime.parse(it.name, ISO_DATE_TIME).atZone(ZoneId.systemDefault()).toInstant() - } + val resDirectory = launches.listFiles()?.maxByOrNull { + LocalDateTime.parse(it.name, ISO_DATE_TIME).atZone(ZoneId.systemDefault()).toInstant() + } - if (resDirectory == null) { - "> **Can't find appropriate benchmark data. Try generating readme files after running benchmarks**." - } else { - val reports = - kotlinx.serialization.json.Json.decodeFromString>( - resDirectory.resolve("jvm.json").readText() - ) + if (resDirectory == null) { + "> **Can't find appropriate benchmark data. Try generating readme files after running benchmarks**." + } else { + val reports = + Json.decodeFromString>(resDirectory.resolve("jvm.json").readText()) - buildString { - appendLine("
") - appendLine("") - appendLine("Report for benchmark configuration ${cfg.name}") - appendLine("") - appendLine() - val first = reports.first() + buildString { + appendLine("
") + appendLine("") + appendLine("Report for benchmark configuration ${cfg.name}") + appendLine("") + appendLine() + val first = reports.first() - appendLine("* Run on ${first.vmName} (build ${first.vmVersion}) with Java process:") - appendLine() - appendLine("```") - appendLine("${first.jvm} ${ - first.jvmArgs.joinToString(" ") - }") - appendLine("```") + appendLine("* Run on ${first.vmName} (build ${first.vmVersion}) with Java process:") + appendLine() + appendLine("```") + appendLine("${first.jvm} ${ + first.jvmArgs.joinToString(" ") + }") + appendLine("```") - appendLine("* JMH ${first.jmhVersion} was used in `${first.mode}` mode with ${first.warmupIterations} warmup ${ - noun(first.warmupIterations, "iteration", "iterations") - } by ${first.warmupTime} and ${first.measurementIterations} measurement ${ - noun(first.measurementIterations, "iteration", "iterations") - } by ${first.measurementTime}.") + appendLine("* JMH ${first.jmhVersion} was used in `${first.mode}` mode with ${first.warmupIterations} warmup ${ + noun(first.warmupIterations, "iteration", "iterations") + } by ${first.warmupTime} and ${first.measurementIterations} measurement ${ + noun(first.measurementIterations, "iteration", "iterations") + } by ${first.measurementTime}.") - appendLine() - appendLine("| Benchmark | Score |") - appendLine("|:---------:|:-----:|") + appendLine() + appendLine("| Benchmark | Score |") + appendLine("|:---------:|:-----:|") - reports.forEach { report -> - appendLine("|`${report.benchmark}`|${report.primaryMetric.score} ± ${report.primaryMetric.scoreError} ${report.primaryMetric.scoreUnit}|") - } - - appendLine("
") + reports.forEach { report -> + appendLine("|`${report.benchmark}`|${report.primaryMetric.score} ± ${report.primaryMetric.scoreError} ${report.primaryMetric.scoreUnit}|") } + + appendLine("
") } } + } } } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 710017588..063c69280 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -5,7 +5,7 @@ pluginManagement { gradlePluginPortal() } - val toolsVersion = "0.9.10" + val toolsVersion = "0.10.0" val kotlinVersion = "1.5.0" plugins {