From 9bf0d5771d973066b9a8797996ea3bdcb1747937 Mon Sep 17 00:00:00 2001 From: lorenz <stu203404@mail.uni-kiel.de> Date: Mon, 8 Mar 2021 20:18:16 +0100 Subject: [PATCH] Refactoring using detekt suggestions --- .../kotlin/theodolite/benchmark/Benchmark.kt | 10 +- .../benchmark/BenchmarkDeployment.kt | 2 +- .../benchmark/BenchmarkExecution.kt | 5 +- .../benchmark/KubernetesBenchmark.kt | 40 +++--- .../KubernetesBenchmarkDeployment.kt | 3 +- .../theodolite/evaluation/SLOChecker.kt | 3 +- .../theodolite/execution/BenchmarkExecutor.kt | 12 +- .../execution/BenchmarkExecutorImpl.kt | 14 +- .../execution/TheodoliteExecutor.kt | 23 ++-- .../execution/TheodoliteYamlExecutor.kt | 10 +- .../main/kotlin/theodolite/k8s/K8sManager.kt | 3 +- .../theodolite/k8s/K8sResourceLoader.kt | 4 +- .../kotlin/theodolite/k8s/TopicManager.kt | 1 - .../theodolite/patcher/AbstractPatcher.kt | 6 +- .../theodolite/patcher/EnvVarPatcher.kt | 16 ++- .../kotlin/theodolite/patcher/ImagePatcher.kt | 5 +- .../theodolite/patcher/NodeSelectorPatcher.kt | 7 +- .../main/kotlin/theodolite/patcher/Patcher.kt | 2 +- .../theodolite/patcher/PatcherManager.kt | 53 +++++--- .../theodolite/patcher/ReplicaPatcher.kt | 4 +- .../theodolite/strategies/StrategyFactory.kt | 3 +- .../restriction/LowerBoundRestriction.kt | 16 +-- .../restriction/RestrictionStrategy.kt | 11 +- .../strategies/searchstrategy/BinarySearch.kt | 17 ++- .../searchstrategy/CompositeStrategy.kt | 9 +- .../strategies/searchstrategy/LinearSearch.kt | 2 +- .../searchstrategy/SearchStrategy.kt | 2 +- .../src/main/kotlin/theodolite/util/Config.kt | 6 +- .../theodolite/util/ConfigurationOverride.kt | 2 +- .../kotlin/theodolite/util/KafkaConfig.kt | 4 +- .../kotlin/theodolite/util/LoadDimension.kt | 2 +- .../src/main/kotlin/theodolite/util/Parser.kt | 4 +- .../theodolite/util/PatcherDefinition.kt | 10 +- .../main/kotlin/theodolite/util/Resource.kt | 4 +- .../main/kotlin/theodolite/util/Results.kt | 18 +-- .../main/kotlin/theodolite/util/TypeName.kt | 4 +- .../main/kotlin/theodolite/util/YamlParser.kt | 5 +- .../yaml/testBenchmarkExecution.yaml | 2 +- .../theodolite/CompositeStrategyTest.kt | 121 +++++++++--------- .../theodolite/ResourceLimitPatcherTest.kt | 21 +-- .../theodolite/ResourceRequestPatcherTest.kt | 21 +-- .../theodolite/TestBenchmarkDeployment.kt | 10 +- .../theodolite/TestBenchmarkExecutorImpl.kt | 13 +- 43 files changed, 303 insertions(+), 227 deletions(-) diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Benchmark.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Benchmark.kt index a21d2c747..8c15fa1dc 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Benchmark.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/Benchmark.kt @@ -1,9 +1,13 @@ package theodolite.benchmark -import theodolite.util.LoadDimension import theodolite.util.ConfigurationOverride +import theodolite.util.LoadDimension import theodolite.util.Resource interface Benchmark { - fun buildDeployment(load: LoadDimension, res: Resource, configurationOverrides: List<ConfigurationOverride>): BenchmarkDeployment -} \ No newline at end of file + fun buildDeployment( + load: LoadDimension, + res: Resource, + configurationOverrides: List<ConfigurationOverride> + ): BenchmarkDeployment +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkDeployment.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkDeployment.kt index 5cba87f9b..df01fae2e 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkDeployment.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkDeployment.kt @@ -3,4 +3,4 @@ package theodolite.benchmark interface BenchmarkDeployment { fun setup() fun teardown() -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecution.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecution.kt index bb816fb1d..32b06d10f 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecution.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/BenchmarkExecution.kt @@ -3,7 +3,6 @@ package theodolite.benchmark import theodolite.util.ConfigurationOverride import kotlin.properties.Delegates - class BenchmarkExecution { lateinit var name: String lateinit var benchmark: String @@ -14,13 +13,13 @@ class BenchmarkExecution { lateinit var configOverrides: List<ConfigurationOverride> class Execution { - lateinit var strategy: String + lateinit var strategy: String var duration by Delegates.notNull<Long>() var repetitions by Delegates.notNull<Int>() lateinit var restrictions: List<String> } - class Slo{ + class Slo { lateinit var sloType: String var threshold by Delegates.notNull<Int>() } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt index 69e45e46a..1aab97164 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmark.kt @@ -7,16 +7,14 @@ import theodolite.patcher.PatcherManager import theodolite.util.* import java.util.* -class KubernetesBenchmark: Benchmark { +class KubernetesBenchmark : Benchmark { lateinit var name: String - private lateinit var appResource: List<String> - private lateinit var loadGenResource: List<String> - private lateinit var resourceTypes: List<TypeName> - private lateinit var loadTypes: List<TypeName> - private lateinit var kafkaConfig: KafkaConfig - private lateinit var zookeeperConfig: HashMap<String,String> - - + lateinit var appResource: List<String> + lateinit var loadGenResource: List<String> + lateinit var resourceTypes: List<TypeName> + lateinit var loadTypes: List<TypeName> + lateinit var kafkaConfig: KafkaConfig + lateinit var zookeeperConfig: HashMap<String, String> private fun loadKubernetesResources(resources: List<String>): List<Pair<String, KubernetesResource>> { val basePath = "./../../../resources/main/yaml/" @@ -25,13 +23,17 @@ class KubernetesBenchmark: Benchmark { return resources .map { resource -> val resourcePath = "$basePath/$resource" - val kind = parser.parse(resourcePath, HashMap<String, String>()::class.java)?.get("kind") !! - val k8sResource = loader.loadK8sResource(kind , resourcePath) + val kind = parser.parse(resourcePath, HashMap<String, String>()::class.java)?.get("kind")!! + val k8sResource = loader.loadK8sResource(kind, resourcePath) Pair(resource, k8sResource) } - } + } - override fun buildDeployment(load: LoadDimension, res: Resource, configurationOverrides: List<ConfigurationOverride>): BenchmarkDeployment { + override fun buildDeployment( + load: LoadDimension, + res: Resource, + configurationOverrides: List<ConfigurationOverride> + ): BenchmarkDeployment { val resources = loadKubernetesResources(this.appResource + this.loadGenResource) val patcherManager = PatcherManager() @@ -40,13 +42,19 @@ class KubernetesBenchmark: Benchmark { patcherManager.createAndApplyPatcher(load.getType(), this.loadTypes, resources, load.get().toString()) // patch overrides - configurationOverrides.forEach{ override -> patcherManager.applyPatcher(listOf(override.patcher), resources, override.value)} + configurationOverrides.forEach { override -> + patcherManager.applyPatcher( + listOf(override.patcher), + resources, + override.value + ) + } return KubernetesBenchmarkDeployment( resources = resources.map { r -> r.second }, kafkaConfig = hashMapOf("bootstrap.servers" to kafkaConfig.bootstrapServer), zookeeperConfig = zookeeperConfig["server"].toString(), - topics = kafkaConfig.getKafkaTopics()) + topics = kafkaConfig.getKafkaTopics() + ) } } - diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt index 2c30bea40..8dc916351 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/benchmark/KubernetesBenchmarkDeployment.kt @@ -13,7 +13,7 @@ class KubernetesBenchmarkDeployment( private val kafkaConfig: HashMap<String, Any>, private val zookeeperConfig: String, private val topics: Collection<NewTopic> -): BenchmarkDeployment { +) : BenchmarkDeployment { private val workloadGeneratorStateCleaner = WorkloadGeneratorStateCleaner(this.zookeeperConfig) private val kafkaController = TopicManager(this.kafkaConfig) private val kubernetesManager = K8sManager(DefaultKubernetesClient().inNamespace("theodolite-she")) @@ -32,6 +32,5 @@ class KubernetesBenchmarkDeployment( resources.forEach { kubernetesManager.remove(it) } - } } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/SLOChecker.kt b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/SLOChecker.kt index 10e113fdf..396e1864b 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/SLOChecker.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/evaluation/SLOChecker.kt @@ -1,4 +1,3 @@ package theodolite.evaluation -interface SLOChecker { -} \ No newline at end of file +interface SLOChecker {} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutor.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutor.kt index 6540004e0..1e9398122 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutor.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutor.kt @@ -2,7 +2,10 @@ package theodolite.execution import mu.KotlinLogging import theodolite.benchmark.Benchmark -import theodolite.util.* +import theodolite.util.ConfigurationOverride +import theodolite.util.LoadDimension +import theodolite.util.Resource +import theodolite.util.Results import java.time.Duration private val logger = KotlinLogging.logger {} @@ -15,7 +18,12 @@ private val logger = KotlinLogging.logger {} * @property executionDuration * @constructor Create empty Benchmark executor */ -abstract class BenchmarkExecutor(val benchmark: Benchmark, val results: Results, val executionDuration: Duration, configurationOverrides: List<ConfigurationOverride>) { +abstract class BenchmarkExecutor( + val benchmark: Benchmark, + val results: Results, + val executionDuration: Duration, + configurationOverrides: List<ConfigurationOverride> +) { /** * Run a experiment for the given parametrization, evaluate the experiment and save the result. diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt index 1c286b7f2..45616a100 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt @@ -1,10 +1,18 @@ package theodolite.execution import theodolite.benchmark.Benchmark -import theodolite.util.* +import theodolite.util.ConfigurationOverride +import theodolite.util.LoadDimension +import theodolite.util.Resource +import theodolite.util.Results import java.time.Duration -class BenchmarkExecutorImpl(benchmark: Benchmark, results: Results, executionDuration: Duration, private val configurationOverrides: List<ConfigurationOverride>) : BenchmarkExecutor(benchmark, results, executionDuration, configurationOverrides) { +class BenchmarkExecutorImpl( + benchmark: Benchmark, + results: Results, + executionDuration: Duration, + private val configurationOverrides: List<ConfigurationOverride> +) : BenchmarkExecutor(benchmark, results, executionDuration, configurationOverrides) { override fun runExperiment(load: LoadDimension, res: Resource): Boolean { val benchmarkDeployment = benchmark.buildDeployment(load, res, this.configurationOverrides) benchmarkDeployment.setup() @@ -15,4 +23,4 @@ class BenchmarkExecutorImpl(benchmark: Benchmark, results: Results, executionDur this.results.setResult(Pair(load, res), result) return result } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteExecutor.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteExecutor.kt index c6694943d..87388cb61 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteExecutor.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteExecutor.kt @@ -12,9 +12,10 @@ import java.time.Duration class TheodoliteExecutor( private val config: BenchmarkExecution, - private val kubernetesBenchmark: KubernetesBenchmark) { + private val kubernetesBenchmark: KubernetesBenchmark +) { - private fun buildConfig(): Config{ + private fun buildConfig(): Config { val results = Results() val strategyFactory = StrategyFactory() @@ -22,12 +23,18 @@ class TheodoliteExecutor( val executor = BenchmarkExecutorImpl(kubernetesBenchmark, results, executionDuration, config.configOverrides) return Config( - loads = config.load.loadValues.map { load -> LoadDimension(load, config.load.loadType ) }, - resources = config.resources.resourceValues.map { resource -> Resource(resource, config.resources.resourceType) }, - compositeStrategy = CompositeStrategy( - benchmarkExecutor = executor, - searchStrategy = strategyFactory.createSearchStrategy(executor, config.execution.strategy), - restrictionStrategies = strategyFactory.createRestrictionStrategy(results, config.execution.restrictions))) + loads = config.load.loadValues.map { load -> LoadDimension(load, config.load.loadType) }, + resources = config.resources.resourceValues.map + { resource -> Resource(resource, config.resources.resourceType) }, + compositeStrategy = CompositeStrategy( + benchmarkExecutor = executor, + searchStrategy = strategyFactory.createSearchStrategy(executor, config.execution.strategy), + restrictionStrategies = strategyFactory.createRestrictionStrategy( + results, + config.execution.restrictions + ) + ) + ) } fun run() { diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt index 8ae567f97..95d7c57fb 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/execution/TheodoliteYamlExecutor.kt @@ -1,17 +1,19 @@ package theodolite.execution import theodolite.benchmark.BenchmarkExecution -import theodolite.util.YamlParser import theodolite.benchmark.KubernetesBenchmark +import theodolite.util.YamlParser class TheodoliteYamlExecutor { fun run() { // load the BenchmarkExecution and the BenchmarkType val parser = YamlParser() - val benchmarkExecution = parser.parse("./../../../resources/main/yaml/testBenchmarkExecution.yaml", BenchmarkExecution::class.java) !! - val benchmark = parser.parse("./../../../resources/main/yaml/testBenchmarkType.yaml", KubernetesBenchmark::class.java) !! + val benchmarkExecution = + parser.parse("./../../../resources/main/yaml/testBenchmarkExecution.yaml", BenchmarkExecution::class.java)!! + val benchmark = + parser.parse("./../../../resources/main/yaml/testBenchmarkType.yaml", KubernetesBenchmark::class.java)!! val executor = TheodoliteExecutor(benchmarkExecution, benchmark) executor.run() } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sManager.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sManager.kt index 8a7a9304b..6a4c5827a 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sManager.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sManager.kt @@ -6,7 +6,6 @@ import io.fabric8.kubernetes.api.model.Service import io.fabric8.kubernetes.api.model.apps.Deployment import io.fabric8.kubernetes.api.model.apps.StatefulSet import io.fabric8.kubernetes.client.NamespacedKubernetesClient -import java.lang.IllegalArgumentException class K8sManager(private val client: NamespacedKubernetesClient) { fun deploy(resource: KubernetesResource) { @@ -36,4 +35,4 @@ class K8sManager(private val client: NamespacedKubernetesClient) { else -> throw IllegalArgumentException("Unknown Kubernetes resource.") } } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt index a34efc90f..dbc31de96 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/K8sResourceLoader.kt @@ -59,7 +59,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { try { resource = f(path) } catch (e: Exception) { - logger.warn {"You potentially misspelled the path: $path"} + logger.warn { "You potentially misspelled the path: $path" } logger.warn { e } } @@ -70,7 +70,7 @@ class K8sResourceLoader(private val client: NamespacedKubernetesClient) { } fun loadK8sResource(kind: String, path: String): KubernetesResource { - return when (kind){ + return when (kind) { "Deployment" -> loadDeployment(path) "Service" -> loadService(path) "ServiceMonitor" -> loadServiceMonitor(path) diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/TopicManager.kt b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/TopicManager.kt index 1336f5751..9cdf1c43f 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/k8s/TopicManager.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/k8s/TopicManager.kt @@ -4,7 +4,6 @@ import mu.KotlinLogging import org.apache.kafka.clients.admin.AdminClient import org.apache.kafka.clients.admin.ListTopicsResult import org.apache.kafka.clients.admin.NewTopic -import java.util.* private val logger = KotlinLogging.logger {} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/AbstractPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/AbstractPatcher.kt index 7ee31881e..2757d1068 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/AbstractPatcher.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/AbstractPatcher.kt @@ -2,4 +2,8 @@ package theodolite.patcher import io.fabric8.kubernetes.api.model.KubernetesResource -abstract class AbstractPatcher(k8sResource: KubernetesResource, container: String? = null, variableName: String? = null): Patcher +abstract class AbstractPatcher( + k8sResource: KubernetesResource, + container: String? = null, + variableName: String? = null +) : Patcher diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/EnvVarPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/EnvVarPatcher.kt index a9eafb93e..5bdf66f18 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/EnvVarPatcher.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/EnvVarPatcher.kt @@ -6,12 +6,18 @@ import io.fabric8.kubernetes.api.model.EnvVarSource import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.api.model.apps.Deployment -class EnvVarPatcher(private val k8sResource: KubernetesResource, private val container: String, private val variableName: String): AbstractPatcher(k8sResource, container, variableName) { +class EnvVarPatcher( + private val k8sResource: KubernetesResource, + private val container: String, + private val variableName: String +) : AbstractPatcher(k8sResource, container, variableName) { override fun <String> patch(value: String) { if (k8sResource is Deployment) { - this.setEnv(k8sResource, this.container, - mapOf(this.variableName to value) as Map<kotlin.String, kotlin.String>) + this.setEnv( + k8sResource, this.container, + mapOf(this.variableName to value) as Map<kotlin.String, kotlin.String> + ) } } @@ -43,6 +49,4 @@ class EnvVarPatcher(private val k8sResource: KubernetesResource, private val con workloadDeployment.spec.template.spec.containers.filter { it.name == containerName } .forEach { setContainerEnv(it, map) } } - - -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ImagePatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ImagePatcher.kt index f1ac252fa..960ed6d6f 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ImagePatcher.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ImagePatcher.kt @@ -4,7 +4,8 @@ import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.api.model.apps.Deployment import io.fabric8.kubernetes.api.model.apps.StatefulSet -class ImagePatcher(private val k8sResource: KubernetesResource, private val container: String): AbstractPatcher(k8sResource, container) { +class ImagePatcher(private val k8sResource: KubernetesResource, private val container: String) : + AbstractPatcher(k8sResource, container) { override fun <String> patch(imagePath: String) { if (k8sResource is Deployment) { @@ -17,4 +18,4 @@ class ImagePatcher(private val k8sResource: KubernetesResource, private val cont } } } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NodeSelectorPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NodeSelectorPatcher.kt index d588e7dae..ad0b8d411 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NodeSelectorPatcher.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/NodeSelectorPatcher.kt @@ -3,10 +3,11 @@ package theodolite.patcher import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.api.model.apps.Deployment -class NodeSelectorPatcher(private val k8sResource: KubernetesResource, private val variableName: String): AbstractPatcher(k8sResource, variableName){ +class NodeSelectorPatcher(private val k8sResource: KubernetesResource, private val variableName: String) : + AbstractPatcher(k8sResource, variableName) { override fun <String> patch(value: String) { if (k8sResource is Deployment) { - k8sResource.spec.template.spec.nodeSelector = mapOf(variableName to value as kotlin.String) + k8sResource.spec.template.spec.nodeSelector = mapOf(variableName to value as kotlin.String) } } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/Patcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/Patcher.kt index 61c2dd5fd..82c562572 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/Patcher.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/Patcher.kt @@ -2,4 +2,4 @@ package theodolite.patcher interface Patcher { fun <T> patch(value: T) -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt index 9fbbf98fe..5557eb4b9 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/PatcherManager.kt @@ -3,39 +3,57 @@ package theodolite.patcher import io.fabric8.kubernetes.api.model.KubernetesResource import theodolite.util.PatcherDefinition import theodolite.util.TypeName -import java.lang.IllegalArgumentException class PatcherManager { - private fun createK8sPatcher(patcherDefinition: PatcherDefinition, k8sResources: List<Pair<String, KubernetesResource>>): Patcher { - val resource = k8sResources.filter { it.first == patcherDefinition.resource}.map { resource -> resource.second }[0] - return when(patcherDefinition.type) { + private fun createK8sPatcher( + patcherDefinition: PatcherDefinition, + k8sResources: List<Pair<String, KubernetesResource>> + ): Patcher { + val resource = + k8sResources.filter { it.first == patcherDefinition.resource }.map { resource -> resource.second }[0] + return when (patcherDefinition.type) { "ReplicaPatcher" -> ReplicaPatcher(resource) "EnvVarPatcher" -> EnvVarPatcher(resource, patcherDefinition.container, patcherDefinition.variableName) "NodeSelectorPatcher" -> NodeSelectorPatcher(resource, patcherDefinition.variableName) - "ResourceLimitPatcher" -> ResourceLimitPatcher(resource, patcherDefinition.container, patcherDefinition.variableName) - "ResourceRequestPatcher" -> ResourceRequestPatcher(resource, patcherDefinition.container, patcherDefinition.variableName) + "ResourceLimitPatcher" -> ResourceLimitPatcher( + resource, + patcherDefinition.container, + patcherDefinition.variableName + ) + "ResourceRequestPatcher" -> ResourceRequestPatcher( + resource, + patcherDefinition.container, + patcherDefinition.variableName + ) else -> throw IllegalArgumentException("Patcher type ${patcherDefinition.type} not found") } } private fun getPatcherDef(requiredType: String, patcherTypes: List<TypeName>): List<PatcherDefinition> { return patcherTypes - .filter { type -> type.typeName == requiredType} - .flatMap { type -> type.patchers} + .filter { type -> type.typeName == requiredType } + .flatMap { type -> type.patchers } } /** - * This function first creates a patcher definition and then patches the list of resources based on this patcher definition + * This function first creates a patcher definition and + * then patches the list of resources based on this patcher definition * * @param type Patcher type, for example "EnvVarPatcher" * @param patcherTypes List of patcher types definitions, for example for resources and threads * @param resources List of K8s resources, a patcher takes the resources that are needed * @param value The value to patch */ - fun createAndApplyPatcher(type: String, patcherTypes: List<TypeName>, resources: List<Pair<String, KubernetesResource>>, value: Any) { + fun createAndApplyPatcher( + type: String, + patcherTypes: List<TypeName>, + resources: List<Pair<String, KubernetesResource>>, + value: Any + ) { this.getPatcherDef(type, patcherTypes) - .forEach {patcherDef -> - createK8sPatcher(patcherDef, resources).patch(value) } + .forEach { patcherDef -> + createK8sPatcher(patcherDef, resources).patch(value) + } } /** @@ -45,8 +63,11 @@ class PatcherManager { * @param resources List of patcher types definitions, for example for resources and threads * @param value The value to patch */ - fun applyPatcher(patcherDefinition: List<PatcherDefinition>, resources: List<Pair<String, KubernetesResource>>, value: Any) { - patcherDefinition.forEach { def -> this.createK8sPatcher(def, resources).patch(value) } - + fun applyPatcher( + patcherDefinition: List<PatcherDefinition>, + resources: List<Pair<String, KubernetesResource>>, + value: Any + ) { + patcherDefinition.forEach { def -> this.createK8sPatcher(def, resources).patch(value) } } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ReplicaPatcher.kt b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ReplicaPatcher.kt index 9e0e04856..ca34e9e35 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ReplicaPatcher.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/patcher/ReplicaPatcher.kt @@ -3,7 +3,7 @@ package theodolite.patcher import io.fabric8.kubernetes.api.model.KubernetesResource import io.fabric8.kubernetes.api.model.apps.Deployment -class ReplicaPatcher(private val k8sResource: KubernetesResource): AbstractPatcher(k8sResource){ +class ReplicaPatcher(private val k8sResource: KubernetesResource) : AbstractPatcher(k8sResource) { override fun <Int> patch(value: Int) { if (k8sResource is Deployment) { if (value is kotlin.Int) { @@ -11,4 +11,4 @@ class ReplicaPatcher(private val k8sResource: KubernetesResource): AbstractPatch } } } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/StrategyFactory.kt b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/StrategyFactory.kt index ccbe6fac4..3d0135a88 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/StrategyFactory.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/StrategyFactory.kt @@ -7,7 +7,6 @@ import theodolite.strategies.searchstrategy.BinarySearch import theodolite.strategies.searchstrategy.LinearSearch import theodolite.strategies.searchstrategy.SearchStrategy import theodolite.util.Results -import java.lang.IllegalArgumentException class StrategyFactory { @@ -28,4 +27,4 @@ class StrategyFactory { } }.toSet() } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/restriction/LowerBoundRestriction.kt b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/restriction/LowerBoundRestriction.kt index aa5878ef2..dfd6bc805 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/restriction/LowerBoundRestriction.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/restriction/LowerBoundRestriction.kt @@ -1,8 +1,8 @@ package theodolite.strategies.restriction -import theodolite.util.Results import theodolite.util.LoadDimension import theodolite.util.Resource +import theodolite.util.Results /** * The Lower Bound Restriction sets the lower bound of the resources to be examined to the value @@ -12,11 +12,11 @@ import theodolite.util.Resource */ class LowerBoundRestriction(results: Results) : RestrictionStrategy(results) { override fun next(load: LoadDimension, resources: List<Resource>): List<Resource> { - val maxLoad: LoadDimension? = this.results.getMaxBenchmarkedLoad(load) - var lowerBound: Resource? = this.results.getMinRequiredInstances(maxLoad, resources[0].getType()) - if(lowerBound == null) { - lowerBound = resources[0] - } - return resources.filter{x -> x.get() >= lowerBound.get()} + val maxLoad: LoadDimension? = this.results.getMaxBenchmarkedLoad(load) + var lowerBound: Resource? = this.results.getMinRequiredInstances(maxLoad, resources[0].getType()) + if (lowerBound == null) { + lowerBound = resources[0] + } + return resources.filter { x -> x.get() >= lowerBound.get() } } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/restriction/RestrictionStrategy.kt b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/restriction/RestrictionStrategy.kt index 6cca627e6..b2e09a0cc 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/restriction/RestrictionStrategy.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/restriction/RestrictionStrategy.kt @@ -1,12 +1,12 @@ package theodolite.strategies.restriction -import theodolite.util.Results import theodolite.util.LoadDimension import theodolite.util.Resource - +import theodolite.util.Results /** - * A "Restriction Strategy" restricts a list of resources based on the current results of all previously performed benchmarks. + * A "Restriction Strategy" restricts a list of resources based on the current + * results of all previously performed benchmarks. */ abstract class RestrictionStrategy(val results: Results) { /** @@ -14,7 +14,8 @@ abstract class RestrictionStrategy(val results: Results) { * * @param load Load dimension for which a subset of resources are required. * @param resources List of resources to be restricted. - * @return Returns a list containing only elements that have not been filtered out by the restriction (possibly empty). + * @return Returns a list containing only elements that have not been filtered out by the + * restriction (possibly empty). */ abstract fun next(load: LoadDimension, resources: List<Resource>): List<Resource> -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/BinarySearch.kt b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/BinarySearch.kt index 88972ffdb..04f25fd99 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/BinarySearch.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/BinarySearch.kt @@ -3,7 +3,6 @@ package theodolite.strategies.searchstrategy import theodolite.execution.BenchmarkExecutor import theodolite.util.LoadDimension import theodolite.util.Resource -import java.lang.IllegalArgumentException /** * Search for the smallest suitable resource with binary search. @@ -13,13 +12,13 @@ import java.lang.IllegalArgumentException class BinarySearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchmarkExecutor) { override fun findSuitableResource(load: LoadDimension, resources: List<Resource>): Resource? { val result = binarySearch(load, resources, 0, resources.size - 1) - if( result == -1 ) { + if (result == -1) { return null } return resources[result] } - private fun binarySearch (load: LoadDimension, resources: List<Resource>, lower: Int, upper: Int): Int { + private fun binarySearch(load: LoadDimension, resources: List<Resource>, lower: Int, upper: Int): Int { if (lower > upper) { throw IllegalArgumentException() } @@ -27,21 +26,21 @@ class BinarySearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchm if (lower == upper) { if (this.benchmarkExecutor.runExperiment(load, resources[lower])) return lower else { - if (lower + 1 == resources.size) return - 1 + if (lower + 1 == resources.size) return -1 return lower + 1 } } else { - // apply binary search for a list with length > 2 and adjust upper and lower depending on the result for `resources[mid]` + // apply binary search for a list with + // length > 2 and adjust upper and lower depending on the result for `resources[mid]` val mid = (upper + lower) / 2 if (this.benchmarkExecutor.runExperiment(load, resources[mid])) { if (mid == lower) { return lower } - return binarySearch(load, resources, lower, mid - 1 ) + return binarySearch(load, resources, lower, mid - 1) } else { - return binarySearch(load, resources, mid + 1 , upper) + return binarySearch(load, resources, mid + 1, upper) } } } - -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/CompositeStrategy.kt b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/CompositeStrategy.kt index b44d5ea5e..8ffca54fa 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/CompositeStrategy.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/CompositeStrategy.kt @@ -4,9 +4,12 @@ import theodolite.execution.BenchmarkExecutor import theodolite.strategies.restriction.RestrictionStrategy import theodolite.util.LoadDimension import theodolite.util.Resource -import theodolite.util.Results -class CompositeStrategy(benchmarkExecutor: BenchmarkExecutor, private val searchStrategy: SearchStrategy, val restrictionStrategies: Set<RestrictionStrategy>) : SearchStrategy(benchmarkExecutor) { +class CompositeStrategy( + benchmarkExecutor: BenchmarkExecutor, + private val searchStrategy: SearchStrategy, + val restrictionStrategies: Set<RestrictionStrategy> +) : SearchStrategy(benchmarkExecutor) { override fun findSuitableResource(load: LoadDimension, resources: List<Resource>): Resource? { var restrictedResources = resources.toList() @@ -15,4 +18,4 @@ class CompositeStrategy(benchmarkExecutor: BenchmarkExecutor, private val search } return this.searchStrategy.findSuitableResource(load, restrictedResources) } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/LinearSearch.kt b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/LinearSearch.kt index c2d6c0933..f1e8591a0 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/LinearSearch.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/LinearSearch.kt @@ -12,4 +12,4 @@ class LinearSearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchm } return null } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/SearchStrategy.kt b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/SearchStrategy.kt index 2c03d1eeb..d57246ca4 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/SearchStrategy.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/SearchStrategy.kt @@ -13,4 +13,4 @@ abstract class SearchStrategy(val benchmarkExecutor: BenchmarkExecutor) { * @return suitable resource for the specified load, or null if no suitable resource exists. */ abstract fun findSuitableResource(load: LoadDimension, resources: List<Resource>): Resource? -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/Config.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/Config.kt index 36f4d9bf9..a6120564c 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/Config.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/Config.kt @@ -1,11 +1,9 @@ package theodolite.util -import theodolite.strategies.restriction.RestrictionStrategy import theodolite.strategies.searchstrategy.CompositeStrategy -import theodolite.strategies.searchstrategy.SearchStrategy -import java.time.Duration data class Config( val loads: List<LoadDimension>, val resources: List<Resource>, - val compositeStrategy: CompositeStrategy) {} \ No newline at end of file + val compositeStrategy: CompositeStrategy +) diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/ConfigurationOverride.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/ConfigurationOverride.kt index f9099929c..fcf8244e8 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/ConfigurationOverride.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/ConfigurationOverride.kt @@ -1,6 +1,6 @@ package theodolite.util -class ConfigurationOverride() { +class ConfigurationOverride { lateinit var patcher: PatcherDefinition lateinit var value: String } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/KafkaConfig.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/KafkaConfig.kt index 1ad2f1b21..8c529e3d8 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/KafkaConfig.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/KafkaConfig.kt @@ -3,7 +3,7 @@ package theodolite.util import org.apache.kafka.clients.admin.NewTopic import kotlin.properties.Delegates -class KafkaConfig() { +class KafkaConfig { lateinit var bootstrapServer: String lateinit var topics: List<TopicWrapper> @@ -16,4 +16,4 @@ class KafkaConfig() { var numPartitions by Delegates.notNull<Int>() var replicationFactor by Delegates.notNull<Short>() } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/LoadDimension.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/LoadDimension.kt index 2f6fcb22e..29d47460b 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/LoadDimension.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/LoadDimension.kt @@ -2,7 +2,7 @@ package theodolite.util data class LoadDimension(private val number: Int, private val type: String) { fun get(): Int { - return this.number; + return this.number } fun getType(): String { diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/Parser.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/Parser.kt index 11e637c89..886fd0b1f 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/Parser.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/Parser.kt @@ -1,5 +1,5 @@ package theodolite.util interface Parser { - fun <T> parse(path: String, E:Class<T>): T? -} \ No newline at end of file + fun <T> parse(path: String, E: Class<T>): T? +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/PatcherDefinition.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/PatcherDefinition.kt index e1bbc64bd..13b1e721c 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/PatcherDefinition.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/PatcherDefinition.kt @@ -1,8 +1,8 @@ package theodolite.util -class PatcherDefinition() { - lateinit var type: String - lateinit var resource: String - lateinit var container: String - lateinit var variableName: String +class PatcherDefinition { + lateinit var type: String + lateinit var resource: String + lateinit var container: String + lateinit var variableName: String } diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/Resource.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/Resource.kt index f315bb54d..cb172e0b8 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/Resource.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/Resource.kt @@ -2,10 +2,10 @@ package theodolite.util data class Resource(private val number: Int, private val type: String) { fun get(): Int { - return this.number; + return this.number } fun getType(): String { return this.type } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/Results.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/Results.kt index b46286f5a..91bde7179 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/Results.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/Results.kt @@ -7,7 +7,7 @@ class Results { this.results[experiment] = successful } - fun getResult (experiment: Pair<LoadDimension, Resource>): Boolean? { + fun getResult(experiment: Pair<LoadDimension, Resource>): Boolean? { return this.results[experiment] } @@ -15,11 +15,11 @@ class Results { if (this.results.isEmpty()) return Resource(Int.MIN_VALUE, resourceTyp) var requiredInstances: Resource? = Resource(Int.MAX_VALUE, resourceTyp) - for(experiment in results) { - if(experiment.key.first == load && experiment.value){ - if(requiredInstances == null) { + for (experiment in results) { + if (experiment.key.first == load && experiment.value) { + if (requiredInstances == null) { requiredInstances = experiment.key.second - }else if (experiment.key.second.get() < requiredInstances.get()) { + } else if (experiment.key.second.get() < requiredInstances.get()) { requiredInstances = experiment.key.second } } @@ -28,10 +28,10 @@ class Results { } fun getMaxBenchmarkedLoad(load: LoadDimension): LoadDimension? { - var maxBenchmarkedLoad: LoadDimension? = null; - for(experiment in results) { + var maxBenchmarkedLoad: LoadDimension? = null + for (experiment in results) { if (experiment.value) { - if(experiment.key.first.get() <= load.get()) { + if (experiment.key.first.get() <= load.get()) { if (maxBenchmarkedLoad == null) { maxBenchmarkedLoad = experiment.key.first } else if (maxBenchmarkedLoad.get() < experiment.key.first.get()) { @@ -42,4 +42,4 @@ class Results { } return maxBenchmarkedLoad } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/TypeName.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/TypeName.kt index f4a65bb58..3568a355e 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/TypeName.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/TypeName.kt @@ -1,6 +1,6 @@ package theodolite.util -class TypeName() { +class TypeName { lateinit var typeName: String lateinit var patchers: List<PatcherDefinition> -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/util/YamlParser.kt b/theodolite-quarkus/src/main/kotlin/theodolite/util/YamlParser.kt index fb953a744..ec91150df 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/util/YamlParser.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/util/YamlParser.kt @@ -6,11 +6,10 @@ import java.io.File import java.io.FileInputStream import java.io.InputStream - -class YamlParser: Parser { +class YamlParser : Parser { override fun <T> parse(path: String, E: Class<T>): T? { val input: InputStream = FileInputStream(File(path)) val parser = Yaml(Constructor(E)) return parser.loadAs(input, E) } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/main/resources/yaml/testBenchmarkExecution.yaml b/theodolite-quarkus/src/main/resources/yaml/testBenchmarkExecution.yaml index cbb4bf3b5..b1c9af6e4 100644 --- a/theodolite-quarkus/src/main/resources/yaml/testBenchmarkExecution.yaml +++ b/theodolite-quarkus/src/main/resources/yaml/testBenchmarkExecution.yaml @@ -14,7 +14,7 @@ slos: execution: strategy: "LinearSearch" duration: 300 - repititions: 1 + repetitions: 1 restrictions: - "LowerBound" configOverrides: diff --git a/theodolite-quarkus/src/test/kotlin/theodolite/CompositeStrategyTest.kt b/theodolite-quarkus/src/test/kotlin/theodolite/CompositeStrategyTest.kt index 7975da57a..31eaa40fb 100644 --- a/theodolite-quarkus/src/test/kotlin/theodolite/CompositeStrategyTest.kt +++ b/theodolite-quarkus/src/test/kotlin/theodolite/CompositeStrategyTest.kt @@ -1,14 +1,15 @@ package theodolite import io.quarkus.test.junit.QuarkusTest -import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test -import theodolite.strategies.searchstrategy.LinearSearch -import theodolite.strategies.searchstrategy.BinarySearch import theodolite.strategies.restriction.LowerBoundRestriction +import theodolite.strategies.searchstrategy.BinarySearch import theodolite.strategies.searchstrategy.CompositeStrategy -import theodolite.util.* -import java.util.* +import theodolite.strategies.searchstrategy.LinearSearch +import theodolite.util.LoadDimension +import theodolite.util.Resource +import theodolite.util.Results @QuarkusTest class CompositeStrategyTest { @@ -16,28 +17,29 @@ class CompositeStrategyTest { @Test fun testEnd2EndLinearSearch() { val mockResults = arrayOf( - arrayOf( true , true , true , true , true , true , true), - arrayOf( false, false, true , true , true , true , true), - arrayOf( false, false, true , true , true , true , true), - arrayOf( false, false, false, true , true , true , true), - arrayOf( false, false, false, false, true , true , true), - arrayOf( false, false, false, false, false, false, true), - arrayOf( false, false, false, false, false, false, false) + arrayOf(true, true, true, true, true, true, true), + arrayOf(false, false, true, true, true, true, true), + arrayOf(false, false, true, true, true, true, true), + arrayOf(false, false, false, true, true, true, true), + arrayOf(false, false, false, false, true, true, true), + arrayOf(false, false, false, false, false, false, true), + arrayOf(false, false, false, false, false, false, false) ) - val mockLoads: List<LoadDimension> = (0..6).map{number -> LoadDimension(number, "NumSensors")} - val mockResources: List<Resource> = (0..6).map{number -> Resource(number, "Instances")} - val results: Results = Results(); + val mockLoads: List<LoadDimension> = (0..6).map { number -> LoadDimension(number, "NumSensors") } + val mockResources: List<Resource> = (0..6).map { number -> Resource(number, "Instances") } + val results = Results() val benchmark = TestBenchmark() - val benchmarkExecutor: TestBenchmarkExecutorImpl = TestBenchmarkExecutorImpl(mockResults, benchmark, results) - val linearSearch: LinearSearch = LinearSearch(benchmarkExecutor); - val lowerBoundRestriction: LowerBoundRestriction = LowerBoundRestriction(results); - val strategy: CompositeStrategy = CompositeStrategy(benchmarkExecutor, linearSearch, setOf(lowerBoundRestriction)) + val benchmarkExecutor = TestBenchmarkExecutorImpl(mockResults, benchmark, results) + val linearSearch = LinearSearch(benchmarkExecutor) + val lowerBoundRestriction = LowerBoundRestriction(results) + val strategy = + CompositeStrategy(benchmarkExecutor, linearSearch, setOf(lowerBoundRestriction)) - val actual: ArrayList<Resource?> = ArrayList<Resource?>() - val expected: ArrayList<Resource?> = ArrayList(listOf(0,2,2,3,4,6).map{ x -> Resource(x, "Instances")}) + val actual: ArrayList<Resource?> = ArrayList() + val expected: ArrayList<Resource?> = ArrayList(listOf(0, 2, 2, 3, 4, 6).map { x -> Resource(x, "Instances") }) expected.add(null) - for(load in mockLoads) { + for (load in mockLoads) { actual.add(strategy.findSuitableResource(load, mockResources)) } @@ -47,28 +49,30 @@ class CompositeStrategyTest { @Test fun testEnd2EndBinarySearch() { val mockResults = arrayOf( - arrayOf( true , true , true , true , true , true , true), - arrayOf( false, false, true , true , true , true , true), - arrayOf( false, false, true , true , true , true , true), - arrayOf( false, false, false, true , true , true , true), - arrayOf( false, false, false, false, true , true , true), - arrayOf( false, false, false, false, false, false, true), - arrayOf( false, false, false, false, false, false, false) + arrayOf(true, true, true, true, true, true, true), + arrayOf(false, false, true, true, true, true, true), + arrayOf(false, false, true, true, true, true, true), + arrayOf(false, false, false, true, true, true, true), + arrayOf(false, false, false, false, true, true, true), + arrayOf(false, false, false, false, false, false, true), + arrayOf(false, false, false, false, false, false, false) ) - val mockLoads: List<LoadDimension> = (0..6).map{number -> LoadDimension(number, "NumSensors")} - val mockResources: List<Resource> = (0..6).map{number -> Resource(number, "Instances")} - val results: Results = Results(); + val mockLoads: List<LoadDimension> = (0..6).map { number -> LoadDimension(number, "NumSensors") } + val mockResources: List<Resource> = (0..6).map { number -> Resource(number, "Instances") } + val results = Results() val benchmark = TestBenchmark() - val benchmarkExecutorImpl: TestBenchmarkExecutorImpl = TestBenchmarkExecutorImpl(mockResults, benchmark, results) - val binarySearch: BinarySearch = BinarySearch(benchmarkExecutorImpl); - val lowerBoundRestriction: LowerBoundRestriction = LowerBoundRestriction(results); - val strategy: CompositeStrategy = CompositeStrategy(benchmarkExecutorImpl, binarySearch, setOf(lowerBoundRestriction)) + val benchmarkExecutorImpl = + TestBenchmarkExecutorImpl(mockResults, benchmark, results) + val binarySearch = BinarySearch(benchmarkExecutorImpl) + val lowerBoundRestriction = LowerBoundRestriction(results) + val strategy = + CompositeStrategy(benchmarkExecutorImpl, binarySearch, setOf(lowerBoundRestriction)) - val actual: ArrayList<Resource?> = ArrayList<Resource?>() - val expected: ArrayList<Resource?> = ArrayList(listOf(0,2,2,3,4,6).map{ x -> Resource(x, "Instances")}) + val actual: ArrayList<Resource?> = ArrayList() + val expected: ArrayList<Resource?> = ArrayList(listOf(0, 2, 2, 3, 4, 6).map { x -> Resource(x, "Instances") }) expected.add(null) - for(load in mockLoads) { + for (load in mockLoads) { actual.add(strategy.findSuitableResource(load, mockResources)) } @@ -78,31 +82,32 @@ class CompositeStrategyTest { @Test fun testEnd2EndBinarySearch2() { val mockResults = arrayOf( - arrayOf( true , true , true , true , true , true , true, true), - arrayOf( false, false, true , true , true , true , true, true), - arrayOf( false, false, true , true , true , true , true, true), - arrayOf( false, false, false, true , true , true , true, true), - arrayOf( false, false, false, false, true , true , true, true), - arrayOf( false, false, false, false, false, false, true, true), - arrayOf( false, false, false, false, false, false, false, true) + arrayOf(true, true, true, true, true, true, true, true), + arrayOf(false, false, true, true, true, true, true, true), + arrayOf(false, false, true, true, true, true, true, true), + arrayOf(false, false, false, true, true, true, true, true), + arrayOf(false, false, false, false, true, true, true, true), + arrayOf(false, false, false, false, false, false, true, true), + arrayOf(false, false, false, false, false, false, false, true) ) - val mockLoads: List<LoadDimension> = (0..6).map{number -> LoadDimension(number, "NumSensors")} - val mockResources: List<Resource> = (0..7).map{number -> Resource(number, "Instances")} - val results: Results = Results(); + val mockLoads: List<LoadDimension> = (0..6).map { number -> LoadDimension(number, "NumSensors") } + val mockResources: List<Resource> = (0..7).map { number -> Resource(number, "Instances") } + val results = Results() val benchmark = TestBenchmark() - val benchmarkExecutor: TestBenchmarkExecutorImpl = TestBenchmarkExecutorImpl(mockResults, benchmark, results) - val binarySearch: BinarySearch = BinarySearch(benchmarkExecutor); - val lowerBoundRestriction: LowerBoundRestriction = LowerBoundRestriction(results); - val strategy: CompositeStrategy = CompositeStrategy(benchmarkExecutor, binarySearch, setOf(lowerBoundRestriction)) + val benchmarkExecutor = TestBenchmarkExecutorImpl(mockResults, benchmark, results) + val binarySearch = BinarySearch(benchmarkExecutor) + val lowerBoundRestriction = LowerBoundRestriction(results) + val strategy = + CompositeStrategy(benchmarkExecutor, binarySearch, setOf(lowerBoundRestriction)) - val actual: ArrayList<Resource?> = ArrayList<Resource?>() - val expected: ArrayList<Resource?> = ArrayList(listOf(0,2,2,3,4,6,7).map{ x -> Resource(x, "Instances")}) + val actual: ArrayList<Resource?> = ArrayList() + val expected: ArrayList<Resource?> = + ArrayList(listOf(0, 2, 2, 3, 4, 6, 7).map { x -> Resource(x, "Instances") }) - for(load in mockLoads) { + for (load in mockLoads) { actual.add(strategy.findSuitableResource(load, mockResources)) } assertEquals(actual, expected) } - -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/test/kotlin/theodolite/ResourceLimitPatcherTest.kt b/theodolite-quarkus/src/test/kotlin/theodolite/ResourceLimitPatcherTest.kt index 3e7be5bd9..9b0da36dd 100644 --- a/theodolite-quarkus/src/test/kotlin/theodolite/ResourceLimitPatcherTest.kt +++ b/theodolite-quarkus/src/test/kotlin/theodolite/ResourceLimitPatcherTest.kt @@ -12,7 +12,8 @@ import theodolite.util.PatcherDefinition /** * Resource patcher test * - * This class tested 4 scenarios for the ResourceLimitPatcher and the ResourceRequestPatcher. The different test cases specifies four possible situations: + * This class tested 4 scenarios for the ResourceLimitPatcher and the ResourceRequestPatcher. + * The different test cases specifies four possible situations: * Case 1: In the given YAML declaration memory and cpu are defined * Case 2: In the given YAML declaration only cpu is defined * Case 3: In the given YAML declaration only memory is defined @@ -24,10 +25,9 @@ class ResourceLimitPatcherTest { val loader = K8sResourceLoader(DefaultKubernetesClient().inNamespace("")) val manager = PatcherManager() - fun applyTest(fileName: String) { - val CPUValue = "50m" - val MEMValue = "3Gi" + val cpuValue = "50m" + val memValue = "3Gi" val k8sResource = loader.loadK8sResource("Deployment", testPath + fileName) as Deployment val defCPU = PatcherDefinition() @@ -45,19 +45,19 @@ class ResourceLimitPatcherTest { manager.applyPatcher( patcherDefinition = listOf(defCPU), resources = listOf(Pair("cpu-memory-deployment.yaml", k8sResource)), - value = CPUValue + value = cpuValue ) manager.applyPatcher( patcherDefinition = listOf(defMEM), resources = listOf(Pair("cpu-memory-deployment.yaml", k8sResource)), - value = MEMValue + value = memValue ) k8sResource.spec.template.spec.containers.filter { it.name == defCPU.container } .forEach { println(it) - assertTrue(it.resources.limits["cpu"].toString() == "$CPUValue") - assertTrue(it.resources.limits["memory"].toString() == "$MEMValue") + assertTrue(it.resources.limits["cpu"].toString() == cpuValue) + assertTrue(it.resources.limits["memory"].toString() == memValue) } } @@ -66,19 +66,22 @@ class ResourceLimitPatcherTest { // Case 1: In the given YAML declaration memory and cpu are defined applyTest("cpu-memory-deployment.yaml") } + @Test fun case2() { // Case 2: In the given YAML declaration only cpu is defined applyTest("cpu-deployment.yaml") } + @Test fun case3() { // Case 3: In the given YAML declaration only memory is defined applyTest("memory-deployment.yaml") } + @Test fun case4() { // Case 4: In the given YAML declaration neither `Resource Request` nor `Request Limit` is defined applyTest("no-resources-deployment.yaml") } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/test/kotlin/theodolite/ResourceRequestPatcherTest.kt b/theodolite-quarkus/src/test/kotlin/theodolite/ResourceRequestPatcherTest.kt index 10ff5320e..22d19d221 100644 --- a/theodolite-quarkus/src/test/kotlin/theodolite/ResourceRequestPatcherTest.kt +++ b/theodolite-quarkus/src/test/kotlin/theodolite/ResourceRequestPatcherTest.kt @@ -12,7 +12,8 @@ import theodolite.util.PatcherDefinition /** * Resource patcher test * - * This class tested 4 scenarios for the ResourceLimitPatcher and the ResourceRequestPatcher. The different test cases specifies four possible situations: + * This class tested 4 scenarios for the ResourceLimitPatcher and the ResourceRequestPatcher. + * The different test cases specifies four possible situations: * Case 1: In the given YAML declaration memory and cpu are defined * Case 2: In the given YAML declaration only cpu is defined * Case 3: In the given YAML declaration only memory is defined @@ -24,10 +25,9 @@ class ResourceRequestPatcherTest { val loader = K8sResourceLoader(DefaultKubernetesClient().inNamespace("")) val manager = PatcherManager() - fun applyTest(fileName: String) { - val CPUValue = "50m" - val MEMValue = "3Gi" + val cpuValue = "50m" + val memValue = "3Gi" val k8sResource = loader.loadK8sResource("Deployment", testPath + fileName) as Deployment val defCPU = PatcherDefinition() @@ -45,19 +45,19 @@ class ResourceRequestPatcherTest { manager.applyPatcher( patcherDefinition = listOf(defCPU), resources = listOf(Pair("cpu-memory-deployment.yaml", k8sResource)), - value = CPUValue + value = cpuValue ) manager.applyPatcher( patcherDefinition = listOf(defMEM), resources = listOf(Pair("cpu-memory-deployment.yaml", k8sResource)), - value = MEMValue + value = memValue ) k8sResource.spec.template.spec.containers.filter { it.name == defCPU.container } .forEach { println(it) - assertTrue(it.resources.requests["cpu"].toString() == "$CPUValue") - assertTrue(it.resources.requests["memory"].toString() == "$MEMValue") + assertTrue(it.resources.requests["cpu"].toString() == cpuValue) + assertTrue(it.resources.requests["memory"].toString() == memValue) } } @@ -66,19 +66,22 @@ class ResourceRequestPatcherTest { // Case 1: In the given YAML declaration memory and cpu are defined applyTest("cpu-memory-deployment.yaml") } + @Test fun case2() { // Case 2: In the given YAML declaration only cpu is defined applyTest("cpu-deployment.yaml") } + @Test fun case3() { // Case 3: In the given YAML declaration only memory is defined applyTest("memory-deployment.yaml") } + @Test fun case4() { // Case 4: In the given YAML declaration neither `Resource Request` nor `Request Limit` is defined applyTest("no-resources-deployment.yaml") } -} \ No newline at end of file +} diff --git a/theodolite-quarkus/src/test/kotlin/theodolite/TestBenchmarkDeployment.kt b/theodolite-quarkus/src/test/kotlin/theodolite/TestBenchmarkDeployment.kt index c59e8b4e1..68b08c294 100644 --- a/theodolite-quarkus/src/test/kotlin/theodolite/TestBenchmarkDeployment.kt +++ b/theodolite-quarkus/src/test/kotlin/theodolite/TestBenchmarkDeployment.kt @@ -2,10 +2,8 @@ package theodolite import theodolite.benchmark.BenchmarkDeployment -class TestBenchmarkDeployment: BenchmarkDeployment { - override fun setup() { - } +class TestBenchmarkDeployment : BenchmarkDeployment { + override fun setup() {} - override fun teardown() { - } -} \ No newline at end of file + override fun teardown() {} +} diff --git a/theodolite-quarkus/src/test/kotlin/theodolite/TestBenchmarkExecutorImpl.kt b/theodolite-quarkus/src/test/kotlin/theodolite/TestBenchmarkExecutorImpl.kt index e6a6f672a..bab20b7b5 100644 --- a/theodolite-quarkus/src/test/kotlin/theodolite/TestBenchmarkExecutorImpl.kt +++ b/theodolite-quarkus/src/test/kotlin/theodolite/TestBenchmarkExecutorImpl.kt @@ -7,8 +7,13 @@ import theodolite.util.Resource import theodolite.util.Results import java.time.Duration -class TestBenchmarkExecutorImpl(private val mockResults: Array<Array<Boolean>>, benchmark: Benchmark, results: Results): - BenchmarkExecutor(benchmark, results, executionDuration = Duration.ofSeconds(1), +class TestBenchmarkExecutorImpl( + private val mockResults: Array<Array<Boolean>>, + benchmark: Benchmark, + results: Results +) : + BenchmarkExecutor( + benchmark, results, executionDuration = Duration.ofSeconds(1), configurationOverrides = emptyList() ) { @@ -16,6 +21,6 @@ class TestBenchmarkExecutorImpl(private val mockResults: Array<Array<Boolean>>, val result = this.mockResults[load.get()][res.get()] this.results.setResult(Pair(load, res), result) - return result; + return result } -} \ No newline at end of file +} -- GitLab