org.jetbrains.kotlin.build.report.metrics.BuildMetricsReporter.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.build.report.metrics
import java.lang.management.ManagementFactory
interface BuildMetricsReporter {
fun startMeasure(time: B)
fun endMeasure(time: B)
fun addTimeMetricNs(time: B, durationNs: Long)
fun addTimeMetricMs(time: B, durationMs: Long) = addTimeMetricNs(time, durationMs * 1_000_000)
fun addMetric(metric: P, value: Long)
fun addTimeMetric(metric: P)
//Change metric to enum if possible
fun addGcMetric(metric: String, value: GcMetric)
fun startGcMetric(name: String, value: GcMetric)
fun endGcMetric(name: String, value: GcMetric)
fun addAttribute(attribute: BuildAttribute)
fun getMetrics(): BuildMetrics
fun addMetrics(metrics: BuildMetrics)
}
inline fun BuildMetricsReporter.measure(time: B, fn: () -> T): T {
startMeasure(time)
try {
return fn()
} finally {
endMeasure(time)
}
}
fun BuildMetricsReporter.startMeasureGc() {
ManagementFactory.getGarbageCollectorMXBeans().forEach {
startGcMetric(it.name, GcMetric(it.collectionTime, it.collectionCount))
}
}
fun BuildMetricsReporter.endMeasureGc() {
ManagementFactory.getGarbageCollectorMXBeans().forEach {
endGcMetric(it.name, GcMetric(it.collectionTime, it.collectionCount))
}
}