From 2bea40b28476dcf56351893bc8d649a0417b91ab Mon Sep 17 00:00:00 2001
From: lorenz <stu203404@mail.uni-kiel.de>
Date: Sat, 20 Mar 2021 12:14:09 +0100
Subject: [PATCH] Add CSV Exporter

---
 .../theodolite/evaluation/CsvExporter.kt      | 43 +++++++++++++++++++
 .../execution/BenchmarkExecutorImpl.kt        |  3 +-
 2 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/evaluation/CsvExporter.kt

diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/CsvExporter.kt b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/CsvExporter.kt
new file mode 100644
index 000000000..929a7914f
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/CsvExporter.kt
@@ -0,0 +1,43 @@
+package theodolite.evaluation
+
+import theodolite.util.PrometheusResponse
+import java.io.File
+import java.io.PrintWriter
+
+class CsvExporter {
+
+    /**
+     * Uses the PrintWriter to transform a PrometheusResponse to Csv
+     */
+    fun toCsv(name : String,prom: PrometheusResponse){
+        val x = toArray(prom)
+        val csvOutputFile: File = File(name+".csv")
+
+        PrintWriter(csvOutputFile).use { pw ->
+            pw.println(listOf("name","time","value").joinToString())
+            x.forEach{
+                pw.println(it.joinToString())
+            }
+        }
+    }
+
+    /**
+     * Converts a PrometheusResponse into a List of List of Strings
+     */
+    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){
+                val y = x as List<*>
+
+                dataList.add(listOf(name,"${y[0]}","${y[1]}"))
+            }
+        }
+
+        return dataList
+    }
+}
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
index 19a0cb61c..458ff108a 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
@@ -25,7 +25,6 @@ class BenchmarkExecutorImpl(
         val benchmarkDeployment = benchmark.buildDeployment(load, res, this.configurationOverrides)
         benchmarkDeployment.setup()
         this.waitAndLog()
-        benchmarkDeployment.teardown()
 
         var result = false
         try {
@@ -46,6 +45,8 @@ class BenchmarkExecutorImpl(
             logger.error { "Evaluation failed for resource: ${res.get()} and load: ${load.get()} error: $e" }
         }
 
+        benchmarkDeployment.teardown()
+
         this.results.setResult(Pair(load, res), result)
         return result
     }
-- 
GitLab