From 4e225ac5a867b193c0f07c9c46ff15f2cfee8ede Mon Sep 17 00:00:00 2001 From: lorenz <stu203404@mail.uni-kiel.de> Date: Sun, 21 Mar 2021 00:08:17 +0100 Subject: [PATCH] Refactored Analysis --- .../theodolite/evaluation/AnalysisExecutor.kt | 44 +++++++------------ .../theodolite/evaluation/CsvExporter.kt | 18 ++++---- .../evaluation/ExternalSloChecker.kt | 3 -- .../evaluation/SloCheckerFactory.kt | 6 --- .../execution/BenchmarkExecutorImpl.kt | 4 +- 5 files changed, 27 insertions(+), 48 deletions(-) diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/AnalysisExecutor.kt b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/AnalysisExecutor.kt index 9d061d7ff..2910d8499 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/AnalysisExecutor.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/AnalysisExecutor.kt @@ -3,56 +3,46 @@ package theodolite.evaluation import mu.KotlinLogging import theodolite.benchmark.BenchmarkExecution import theodolite.util.LoadDimension -import theodolite.util.PrometheusResponse import theodolite.util.Resource import java.time.Duration import java.time.Instant private val logger = KotlinLogging.logger {} -/** - * Executes the Analysis. - */ -class AnalysisExecutor { +class AnalysisExecutor(private val slo: BenchmarkExecution.Slo) { - fun analyse(load:LoadDimension,executionDuration: Duration, res: Resource,slo: BenchmarkExecution.Slo): Boolean { + private val fetcher = MetricFetcher( + prometheusURL = slo.prometheusUrl, + offset = Duration.ofHours(slo.offset.toLong()) + ) + + fun analyse(load: LoadDimension, res: Resource, executionDuration: Duration): Boolean { var result = false try { + val prometheusData = fetcher.fetchMetric( + start = Instant.now().minus(executionDuration), + end = Instant.now(), + query = "sum by(group)(kafka_consumergroup_group_lag >= 0)" + ) - val prometheusData = fetch(Instant.now().minus(executionDuration), - Instant.now(), - slo, - "sum by(group)(kafka_consumergroup_group_lag >= 0)") - - CsvExporter().toCsv("${load.get()}_${res.get()}_${slo.sloType}",prometheusData) + CsvExporter().toCsv(name = "${load.get()}_${res.get()}_${slo.sloType}", prom = prometheusData) val sloChecker = SloCheckerFactory().create( slotype = slo.sloType, - prometheusURL = slo.prometheusUrl, - query = "sum by(group)(kafka_consumergroup_group_lag >= 0)", externalSlopeURL = slo.externalSloUrl, threshold = slo.threshold, - offset = Duration.ofHours(slo.offset.toLong()), warmup = slo.warmup ) - result = sloChecker.evaluate(start = Instant.now().minus(executionDuration), - end = Instant.now(),fetchedData = prometheusData) + result = sloChecker.evaluate( + start = Instant.now().minus(executionDuration), + end = Instant.now(), fetchedData = prometheusData + ) } catch (e: Exception) { logger.error { "Evaluation failed for resource: ${res.get()} and load: ${load.get()} error: $e" } } - return result } - - fun fetch(start: Instant, end: Instant,slo: BenchmarkExecution.Slo,query: String): PrometheusResponse { - - val metricFetcher = MetricFetcher(prometheusURL = slo.prometheusUrl, - offset = Duration.ofHours(slo.offset.toLong())) - val fetchedData = metricFetcher.fetchMetric(start, end, query) - - return fetchedData - } } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/CsvExporter.kt b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/CsvExporter.kt index 116bfdfd8..516f17a45 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/CsvExporter.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/CsvExporter.kt @@ -12,32 +12,32 @@ class CsvExporter { /** * Uses the PrintWriter to transform a PrometheusResponse to Csv */ - fun toCsv(name : String,prom: PrometheusResponse){ + fun toCsv(name: String, prom: PrometheusResponse) { val responseArray = toArray(prom) - val csvOutputFile: File = File("$name.csv") + val csvOutputFile = File("$name.csv") PrintWriter(csvOutputFile).use { pw -> - pw.println(listOf("name","time","value").joinToString()) - responseArray.forEach{ + pw.println(listOf("name", "time", "value").joinToString()) + responseArray.forEach { pw.println(it.joinToString()) } } - logger.debug{csvOutputFile.absolutePath} - logger.info { "Wrote csv to $name" } + logger.debug { csvOutputFile.absolutePath } + logger.info { "Wrote csv file: $name to ${csvOutputFile.absolutePath}" } } /** * Converts a PrometheusResponse into a List of List of Strings */ - private fun toArray(prom : PrometheusResponse): MutableList<List<String>> { + private fun toArray(prom: PrometheusResponse): MutableList<List<String>> { val name = prom.data?.result?.get(0)?.metric?.group.toString() val values = prom.data?.result?.get(0)?.values val dataList = mutableListOf<List<String>>() if (values != null) { - for (x in values){ + for (x in values) { val y = x as List<*> - dataList.add(listOf(name,"${y[0]}","${y[1]}")) + dataList.add(listOf(name, "${y[0]}", "${y[1]}")) } } return dataList diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/ExternalSloChecker.kt b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/ExternalSloChecker.kt index 4cf417bc7..bc9ca12cb 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/ExternalSloChecker.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/ExternalSloChecker.kt @@ -9,11 +9,8 @@ import java.time.Duration import java.time.Instant class ExternalSloChecker( - private val prometheusURL: String, - private val query: String, private val externalSlopeURL: String, private val threshold: Int, - private val offset: Duration, private val warmup: Int ) : SloChecker { diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/SloCheckerFactory.kt b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/SloCheckerFactory.kt index 2170ef7b6..50b7b0aec 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/SloCheckerFactory.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/SloCheckerFactory.kt @@ -6,21 +6,15 @@ class SloCheckerFactory { fun create( slotype: String, - prometheusURL: String, - query: String, externalSlopeURL: String, threshold: Int, - offset: Duration, warmup: Int ): SloChecker { return when (slotype) { "lag trend" -> ExternalSloChecker( - prometheusURL = prometheusURL, - query = query, externalSlopeURL = externalSlopeURL, threshold = threshold, - offset = offset, warmup = warmup ) else -> throw IllegalArgumentException("Slotype $slotype not found.") diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt index 0795e9893..25ec1bf5f 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt @@ -4,13 +4,11 @@ import mu.KotlinLogging import theodolite.benchmark.Benchmark import theodolite.benchmark.BenchmarkExecution import theodolite.evaluation.AnalysisExecutor -import theodolite.evaluation.SloCheckerFactory import theodolite.util.ConfigurationOverride import theodolite.util.LoadDimension import theodolite.util.Resource import theodolite.util.Results import java.time.Duration -import java.time.Instant private val logger = KotlinLogging.logger {} @@ -26,7 +24,7 @@ class BenchmarkExecutorImpl( benchmarkDeployment.setup() this.waitAndLog() - var result = AnalysisExecutor().analyse(load,executionDuration,res,slo) + val result = AnalysisExecutor(slo = slo).analyse(load = load, res = res, executionDuration = executionDuration) benchmarkDeployment.teardown() -- GitLab