From d76b715357ca37df5ccaeb8555dc42bb44059d2f Mon Sep 17 00:00:00 2001
From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de>
Date: Thu, 15 Apr 2021 10:24:44 +0200
Subject: [PATCH] Introduce new patcher to scale the number of load generator
 correctly

---
 ...NestedGroupsLoadGeneratorReplicaPatcher.kt | 20 +++++++++++++++++++
 .../NumSensorsLoadGeneratorReplicaPatcher.kt  | 17 ++++++++++++++++
 .../theodolite/patcher/PatcherFactory.kt      |  2 ++
 3 files changed, 39 insertions(+)
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/patcher/NumNestedGroupsLoadGeneratorReplicaPatcher.kt
 create mode 100644 theodolite-quarkus/src/main/kotlin/theodolite/patcher/NumSensorsLoadGeneratorReplicaPatcher.kt

diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NumNestedGroupsLoadGeneratorReplicaPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NumNestedGroupsLoadGeneratorReplicaPatcher.kt
new file mode 100644
index 000000000..7cf56f845
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NumNestedGroupsLoadGeneratorReplicaPatcher.kt
@@ -0,0 +1,20 @@
+package theodolite.patcher
+
+import io.fabric8.kubernetes.api.model.KubernetesResource
+import io.fabric8.kubernetes.api.model.apps.Deployment
+import kotlin.math.pow
+
+private const val NUM_SENSORS = 4.0
+private const val LOAD_GEN_MAX_RECORDS = 150000
+
+class NumNestedGroupsLoadGeneratorReplicaPatcher(private val k8sResource: KubernetesResource) : AbstractPatcher(k8sResource) {
+    override fun <String> patch(value: String) {
+        if (k8sResource is Deployment) {
+            if (value is kotlin.String) {
+                val approxNumSensors = NUM_SENSORS.pow(Integer.parseInt(value).toDouble())
+                val loadGenInstances = (approxNumSensors + LOAD_GEN_MAX_RECORDS -1) / LOAD_GEN_MAX_RECORDS
+                this.k8sResource.spec.replicas = loadGenInstances.toInt()
+            }
+        }
+    }
+}
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NumSensorsLoadGeneratorReplicaPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NumSensorsLoadGeneratorReplicaPatcher.kt
new file mode 100644
index 000000000..6f2ebcb8b
--- /dev/null
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NumSensorsLoadGeneratorReplicaPatcher.kt
@@ -0,0 +1,17 @@
+package theodolite.patcher
+
+import io.fabric8.kubernetes.api.model.KubernetesResource
+import io.fabric8.kubernetes.api.model.apps.Deployment
+
+private const val LOAD_GEN_MAX_RECORDS = 150000
+
+class NumSensorsLoadGeneratorReplicaPatcher(private val k8sResource: KubernetesResource) : AbstractPatcher(k8sResource) {
+    override fun <String> patch(value: String) {
+        if (k8sResource is Deployment) {
+            if (value is kotlin.String) {
+                val loadGenInstances = (Integer.parseInt(value) + LOAD_GEN_MAX_RECORDS - 1) / LOAD_GEN_MAX_RECORDS
+                this.k8sResource.spec.replicas = loadGenInstances
+            }
+        }
+    }
+}
diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherFactory.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
index dd391c599..b34fed685 100644
--- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
+++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherFactory.kt
@@ -10,6 +10,8 @@ class PatcherFactory {
             k8sResources.filter { it.first == patcherDefinition.resource }.map { resource -> resource.second }[0]
         return when (patcherDefinition.type) {
             "ReplicaPatcher" -> ReplicaPatcher(resource)
+            "NumNestedGroupsLoadGeneratorReplicaPatcher" ->NumNestedGroupsLoadGeneratorReplicaPatcher(resource)
+            "NumSensorsLoadGeneratorReplicaPatcher" -> NumSensorsLoadGeneratorReplicaPatcher(resource)
             "EnvVarPatcher" -> EnvVarPatcher(resource, patcherDefinition.container, patcherDefinition.variableName)
             "NodeSelectorPatcher" -> NodeSelectorPatcher(resource, patcherDefinition.variableName)
             "ResourceLimitPatcher" -> ResourceLimitPatcher(
-- 
GitLab