xref: /aosp_15_r20/external/kotlinx.serialization/gradle/benchmark-parsing.gradle (revision 57b5a4a64c534cf7f27ac9427ceab07f3d8ed3d8)
1import groovy.json.JsonSlurper
2import org.gradle.api.*
3
4/**
5 * Utility for printing benchmark results.
6 * Results can be obtained with JMH flags
7 * -rf json -rff serialization-benchmark-results.json
8 */
9class PrintBenchmarksTask extends DefaultTask {
10    private String fileName = "serialization-benchmark-results.json"
11
12    @TaskAction
13    def printBenchmarkJsonAsTeamcityStats() {
14        File jsonFile = project.file(fileName)
15        if (!jsonFile.exists()) throw new TaskExecutionException(this, new FileNotFoundException("File $fileName not found"))
16        def parsedJson = new JsonSlurper().parseText(jsonFile.text)
17
18        parsedJson.each { v ->
19            def name = (v.benchmark - "kotlinx.benchmarks.")
20            def score = v.primaryMetric.score
21            println("##teamcity[buildStatisticValue key='" + name + "' value='" + score + "']")
22        }
23    }
24}
25
26rootProject.tasks.register("printBenchmarksJsonAsTeamcityStats", PrintBenchmarksTask)
27