summaryrefslogtreecommitdiff
path: root/plugins/kotlin/performance-tests/test/org/jetbrains/kotlin/idea/perf/util/Metric.kt
blob: 2f93ad0a2664925ecacb8d97d0d570b7593d9fe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.kotlin.idea.perf.util

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import org.jetbrains.kotlin.idea.perf.Stats
import org.jetbrains.kotlin.idea.perf.calcMean
import java.util.ArrayList

@JsonInclude(JsonInclude.Include.NON_NULL)
data class Benchmark(
    val version: Int = 4,
    @set:JsonProperty("agentName")
    var agentName: String?,
    @set:JsonProperty("benchmark")
    var benchmark: String,
    @set:JsonProperty("name")
    var name: String? = null,
    @set:JsonProperty("synthetic")
    var synthetic: Boolean? = null,
    @set:JsonProperty("index")
    var index: Int? = null,
    @set:JsonProperty("warmUp")
    var warmUp: Boolean? = null,
    @set:JsonProperty("buildTimestamp")
    var buildTimestamp: String,
    @set:JsonProperty("buildBranch")
    var buildBranch: String?,
    @set:JsonProperty("commit")
    var commit: String? = null,
    @set:JsonProperty("buildId")
    var buildId: Int?,
    @set:JsonProperty("metricValue")
    var metricValue: Long? = null,
    @set:JsonProperty("metricError")
    var metricError: Long? = null,
    @set:JsonProperty("hasError")
    var hasError: Boolean? = null,

    @set:JsonProperty("metrics")
    var metrics: List<Metric> = ArrayList()
) {
    init {
        hasError = if (metrics.any { it.ifHasError() == true }) true else null
    }

    private fun String?.escapeName() = this?.replace(Regex("[^A-Za-z0-9]"), "")

    fun id(): String =
        listOfNotNull(
            benchmark?.escapeName(),
            name?.escapeName(),
            buildId?.toString(),
            warmUp?.let { "warmUp" } ?: null,
            index?.toString()
        ).joinToString(separator = "_")

    fun cleanUp() {
        metrics?.forEach { it.cleanUp() }
        metrics = metrics?.filter { it.metricValue != null || (it.metrics?.isNotEmpty() == true) }
    }

    fun merge(extraBenchmark: Benchmark) {
        val benchmarkMetrics = extraBenchmark.metrics.filter {
            it.metricName !in Stats.extraMetricNames
        }
        benchmarkMetrics.forEach { it.rawMetrics = null }
        val extraBenchmarkName = extraBenchmark.name!!
        metrics = metrics.filterNot { it.metricName == extraBenchmarkName } + Metric(
            metricName = extraBenchmarkName,
            metricValue = extraBenchmark.metricValue,
            metricError = extraBenchmark.metricError,
            hasError = extraBenchmark.hasError,
            metrics = benchmarkMetrics
        )
        cleanUp()

        hasError = if (metrics.any { it.ifHasError() == true }) true else null
        val values = metrics.mapNotNull { it.metricValue }.toLongArray()
        val calcMean = calcMean(values)
        metricValue = calcMean.geomMean.toLong()
        metricError = calcMean.stdDev.toLong()
    }
}

@JsonInclude(JsonInclude.Include.NON_NULL)
data class Metric(
    @set:JsonProperty("metricName")
    var metricName: String,
    @set:JsonProperty("index")
    var index: Int? = null,
    @set:JsonProperty("warmUp")
    var warmUp: Boolean? = null,
    @set:JsonProperty("legacyName")
    var legacyName: String? = null,
    @set:JsonProperty("metricValue")
    var metricValue: Long?,
    @set:JsonProperty("metricError")
    var metricError: Long? = null,
    @set:JsonProperty("hasError")
    var hasError: Boolean? = null,
    @set:JsonProperty("metrics")
    var metrics: List<Metric>? = null,
    @set:JsonProperty("rawMetrics")
    var rawMetrics: List<Metric>? = null
) {
    fun ifHasError(): Boolean? {
        if (hasError == true) return true
        hasError =
            if ((metrics?.any { it.ifHasError() == true } == true) || (rawMetrics?.any { it.ifHasError() == true } == true)) {
                true
            } else {
                null
            }
        return hasError
    }

    fun resetValue() {
        metricValue = null
        metricError = null
        metrics?.forEach { it.resetValue() }
        rawMetrics?.forEach { it.resetValue() }
    }

    fun cleanUp() {
        legacyName = null
        metrics?.forEach { it.cleanUp() }
        rawMetrics?.forEach { it.cleanUp() }

        metrics = metrics?.filter { it.metricValue != null || (it.metrics?.isNotEmpty() == true) }
        if (metrics?.isEmpty() == true) metrics = null

        rawMetrics = rawMetrics?.filter { it.metricValue != null || (it.metrics?.isNotEmpty() == true) }
        if (rawMetrics?.isEmpty() == true) rawMetrics = null
    }
}