summaryrefslogtreecommitdiff
path: root/simulator/opendc-simulator/opendc-simulator-compute/src/test
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-04-07 13:07:28 +0200
committerGitHub <noreply@github.com>2021-04-07 13:07:28 +0200
commit519141f9af525a853b40eb821e70ca209bc104bf (patch)
tree3c8a4a3db41ac5be3a67f6427289a858805f98dc /simulator/opendc-simulator/opendc-simulator-compute/src/test
parent8f2855b9b9d81b15ff431775ba584c1e3f4add2c (diff)
parent3860d9e1c042eefacd5accb771cf47990090f649 (diff)
simulator: Add CPUFreq subsystem
This pull request adds a CPUFreq subsystem to the simulator module. This subsystem allows a simulated machine to perform frequency scaling, which in turn should reduce energy consumption.
Diffstat (limited to 'simulator/opendc-simulator/opendc-simulator-compute/src/test')
-rw-r--r--simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimHypervisorTest.kt10
-rw-r--r--simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt19
-rw-r--r--simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimSpaceSharedHypervisorTest.kt34
-rw-r--r--simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/cpufreq/DemandScalingGovernorTest.kt48
-rw-r--r--simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriverTest.kt136
-rw-r--r--simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/PStatePowerModelTest.kt44
-rw-r--r--simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/PowerModelTest.kt (renamed from simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/MachinePowerModelTest.kt)47
7 files changed, 257 insertions, 81 deletions
diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimHypervisorTest.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimHypervisorTest.kt
index 5773b325..67295dfd 100644
--- a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimHypervisorTest.kt
+++ b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimHypervisorTest.kt
@@ -32,9 +32,12 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
+import org.opendc.simulator.compute.cpufreq.PerformanceScalingGovernor
+import org.opendc.simulator.compute.cpufreq.SimpleScalingDriver
import org.opendc.simulator.compute.model.MemoryUnit
import org.opendc.simulator.compute.model.ProcessingNode
import org.opendc.simulator.compute.model.ProcessingUnit
+import org.opendc.simulator.compute.power.ConstantPowerModel
import org.opendc.simulator.compute.workload.SimTraceWorkload
import org.opendc.simulator.utils.DelayControllerClockAdapter
@@ -91,7 +94,7 @@ internal class SimHypervisorTest {
),
)
- val machine = SimBareMetalMachine(coroutineContext, clock, model)
+ val machine = SimBareMetalMachine(coroutineContext, clock, model, PerformanceScalingGovernor(), SimpleScalingDriver(ConstantPowerModel(0.0)))
val hypervisor = SimFairShareHypervisor(listener)
launch {
@@ -163,7 +166,10 @@ internal class SimHypervisorTest {
)
)
- val machine = SimBareMetalMachine(coroutineContext, clock, model)
+ val machine = SimBareMetalMachine(
+ coroutineContext, clock, model, PerformanceScalingGovernor(),
+ SimpleScalingDriver(ConstantPowerModel(0.0))
+ )
val hypervisor = SimFairShareHypervisor(listener)
launch {
diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt
index 071bdf77..d947b9cb 100644
--- a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt
+++ b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimMachineTest.kt
@@ -28,9 +28,14 @@ import kotlinx.coroutines.test.runBlockingTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertDoesNotThrow
+import org.junit.jupiter.api.assertThrows
+import org.opendc.simulator.compute.cpufreq.PerformanceScalingGovernor
+import org.opendc.simulator.compute.cpufreq.SimpleScalingDriver
import org.opendc.simulator.compute.model.MemoryUnit
import org.opendc.simulator.compute.model.ProcessingNode
import org.opendc.simulator.compute.model.ProcessingUnit
+import org.opendc.simulator.compute.power.ConstantPowerModel
import org.opendc.simulator.compute.workload.SimFlopsWorkload
import org.opendc.simulator.utils.DelayControllerClockAdapter
@@ -54,7 +59,7 @@ class SimMachineTest {
@Test
fun testFlopsWorkload() = runBlockingTest {
val clock = DelayControllerClockAdapter(this)
- val machine = SimBareMetalMachine(coroutineContext, clock, machineModel)
+ val machine = SimBareMetalMachine(coroutineContext, clock, machineModel, PerformanceScalingGovernor(), SimpleScalingDriver(ConstantPowerModel(0.0)))
try {
machine.run(SimFlopsWorkload(2_000, utilization = 1.0))
@@ -69,7 +74,7 @@ class SimMachineTest {
@Test
fun testUsage() = runBlockingTest {
val clock = DelayControllerClockAdapter(this)
- val machine = SimBareMetalMachine(coroutineContext, clock, machineModel)
+ val machine = SimBareMetalMachine(coroutineContext, clock, machineModel, PerformanceScalingGovernor(), SimpleScalingDriver(ConstantPowerModel(0.0)))
val res = mutableListOf<Double>()
val job = launch { machine.usage.toList(res) }
@@ -83,4 +88,14 @@ class SimMachineTest {
machine.close()
}
}
+
+ @Test
+ fun testClose() = runBlockingTest {
+ val clock = DelayControllerClockAdapter(this)
+ val machine = SimBareMetalMachine(coroutineContext, clock, machineModel, PerformanceScalingGovernor(), SimpleScalingDriver(ConstantPowerModel(0.0)))
+
+ machine.close()
+ assertDoesNotThrow { machine.close() }
+ assertThrows<IllegalStateException> { machine.run(SimFlopsWorkload(2_000, utilization = 1.0)) }
+ }
}
diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimSpaceSharedHypervisorTest.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimSpaceSharedHypervisorTest.kt
index fb0523af..51e4305a 100644
--- a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimSpaceSharedHypervisorTest.kt
+++ b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/SimSpaceSharedHypervisorTest.kt
@@ -31,9 +31,12 @@ import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
+import org.opendc.simulator.compute.cpufreq.PerformanceScalingGovernor
+import org.opendc.simulator.compute.cpufreq.SimpleScalingDriver
import org.opendc.simulator.compute.model.MemoryUnit
import org.opendc.simulator.compute.model.ProcessingNode
import org.opendc.simulator.compute.model.ProcessingUnit
+import org.opendc.simulator.compute.power.ConstantPowerModel
import org.opendc.simulator.compute.workload.SimFlopsWorkload
import org.opendc.simulator.compute.workload.SimRuntimeWorkload
import org.opendc.simulator.compute.workload.SimTraceWorkload
@@ -75,7 +78,10 @@ internal class SimSpaceSharedHypervisorTest {
),
)
- val machine = SimBareMetalMachine(coroutineContext, clock, machineModel)
+ val machine = SimBareMetalMachine(
+ coroutineContext, clock, machineModel, PerformanceScalingGovernor(),
+ SimpleScalingDriver(ConstantPowerModel(0.0))
+ )
val hypervisor = SimSpaceSharedHypervisor()
val colA = launch { machine.usage.toList(usagePm) }
@@ -109,7 +115,10 @@ internal class SimSpaceSharedHypervisorTest {
val clock = DelayControllerClockAdapter(this)
val duration = 5 * 60L * 1000
val workload = SimRuntimeWorkload(duration)
- val machine = SimBareMetalMachine(coroutineContext, clock, machineModel)
+ val machine = SimBareMetalMachine(
+ coroutineContext, clock, machineModel, PerformanceScalingGovernor(),
+ SimpleScalingDriver(ConstantPowerModel(0.0))
+ )
val hypervisor = SimSpaceSharedHypervisor()
launch { machine.run(hypervisor) }
@@ -131,7 +140,10 @@ internal class SimSpaceSharedHypervisorTest {
val duration = 5 * 60L * 1000
val workload = SimFlopsWorkload((duration * 3.2).toLong(), 1.0)
- val machine = SimBareMetalMachine(coroutineContext, clock, machineModel)
+ val machine = SimBareMetalMachine(
+ coroutineContext, clock, machineModel, PerformanceScalingGovernor(),
+ SimpleScalingDriver(ConstantPowerModel(0.0))
+ )
val hypervisor = SimSpaceSharedHypervisor()
launch { machine.run(hypervisor) }
@@ -150,7 +162,10 @@ internal class SimSpaceSharedHypervisorTest {
fun testTwoWorkloads() = runBlockingTest {
val clock = DelayControllerClockAdapter(this)
val duration = 5 * 60L * 1000
- val machine = SimBareMetalMachine(coroutineContext, clock, machineModel)
+ val machine = SimBareMetalMachine(
+ coroutineContext, clock, machineModel, PerformanceScalingGovernor(),
+ SimpleScalingDriver(ConstantPowerModel(0.0))
+ )
val hypervisor = SimSpaceSharedHypervisor()
launch { machine.run(hypervisor) }
@@ -174,8 +189,10 @@ internal class SimSpaceSharedHypervisorTest {
@Test
fun testConcurrentWorkloadFails() = runBlockingTest {
val clock = DelayControllerClockAdapter(this)
-
- val machine = SimBareMetalMachine(coroutineContext, clock, machineModel)
+ val machine = SimBareMetalMachine(
+ coroutineContext, clock, machineModel, PerformanceScalingGovernor(),
+ SimpleScalingDriver(ConstantPowerModel(0.0))
+ )
val hypervisor = SimSpaceSharedHypervisor()
launch { machine.run(hypervisor) }
@@ -197,7 +214,10 @@ internal class SimSpaceSharedHypervisorTest {
@Test
fun testConcurrentWorkloadSucceeds() = runBlockingTest {
val clock = DelayControllerClockAdapter(this)
- val machine = SimBareMetalMachine(coroutineContext, clock, machineModel)
+ val machine = SimBareMetalMachine(
+ coroutineContext, clock, machineModel, PerformanceScalingGovernor(),
+ SimpleScalingDriver(ConstantPowerModel(0.0))
+ )
val hypervisor = SimSpaceSharedHypervisor()
launch { machine.run(hypervisor) }
diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/cpufreq/DemandScalingGovernorTest.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/cpufreq/DemandScalingGovernorTest.kt
new file mode 100644
index 00000000..19c06126
--- /dev/null
+++ b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/cpufreq/DemandScalingGovernorTest.kt
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2021 AtLarge Research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package org.opendc.simulator.compute.cpufreq
+
+import io.mockk.every
+import io.mockk.mockk
+import io.mockk.verify
+import org.junit.jupiter.api.Test
+
+/**
+ * Test suite for the [DemandScalingGovernor]
+ */
+internal class DemandScalingGovernorTest {
+ @Test
+ fun testSetDemandLimit() {
+ val ctx = mockk<ScalingContext>(relaxUnitFun = true)
+
+ every { ctx.resource.speed.value } returns 2100.0
+
+ val logic = DemandScalingGovernor().createLogic(ctx)
+
+ logic.onStart()
+ verify(exactly = 0) { ctx.setTarget(any()) }
+
+ logic.onLimit()
+ verify(exactly = 1) { ctx.setTarget(2100.0) }
+ }
+}
diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriverTest.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriverTest.kt
new file mode 100644
index 00000000..5c30bc1f
--- /dev/null
+++ b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/cpufreq/PStateScalingDriverTest.kt
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2021 AtLarge Research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package org.opendc.simulator.compute.cpufreq
+
+import io.mockk.every
+import io.mockk.mockk
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Test
+import org.opendc.simulator.compute.SimBareMetalMachine
+import org.opendc.simulator.compute.model.ProcessingUnit
+import org.opendc.simulator.compute.power.ConstantPowerModel
+import org.opendc.simulator.compute.power.LinearPowerModel
+import org.opendc.simulator.resources.SimResourceSource
+
+/**
+ * Test suite for [PStateScalingDriver].
+ */
+internal class PStateScalingDriverTest {
+ @Test
+ fun testPowerWithoutGovernor() {
+ val machine = mockk<SimBareMetalMachine>()
+
+ val driver = PStateScalingDriver(
+ sortedMapOf(
+ 2800.0 to ConstantPowerModel(200.0),
+ 3300.0 to ConstantPowerModel(300.0),
+ 3600.0 to ConstantPowerModel(350.0),
+ )
+ )
+
+ val logic = driver.createLogic(machine)
+ assertEquals(200.0, logic.computePower())
+ }
+
+ @Test
+ fun testPowerWithSingleGovernor() {
+ val machine = mockk<SimBareMetalMachine>()
+ val cpu = mockk<ProcessingUnit>()
+ val resource = mockk<SimResourceSource>()
+
+ every { cpu.frequency } returns 4100.0
+ every { resource.speed.value } returns 1200.0
+
+ val driver = PStateScalingDriver(
+ sortedMapOf(
+ 2800.0 to ConstantPowerModel(200.0),
+ 3300.0 to ConstantPowerModel(300.0),
+ 3600.0 to ConstantPowerModel(350.0),
+ )
+ )
+
+ val logic = driver.createLogic(machine)
+
+ val scalingContext = logic.createContext(cpu, resource)
+ scalingContext.setTarget(3200.0)
+
+ assertEquals(300.0, logic.computePower())
+ }
+
+ @Test
+ fun testPowerWithMultipleGovernors() {
+ val machine = mockk<SimBareMetalMachine>()
+ val cpu = mockk<ProcessingUnit>()
+ val resource = mockk<SimResourceSource>()
+
+ every { cpu.frequency } returns 4100.0
+ every { resource.speed.value } returns 1200.0
+
+ val driver = PStateScalingDriver(
+ sortedMapOf(
+ 2800.0 to ConstantPowerModel(200.0),
+ 3300.0 to ConstantPowerModel(300.0),
+ 3600.0 to ConstantPowerModel(350.0),
+ )
+ )
+
+ val logic = driver.createLogic(machine)
+
+ val scalingContextA = logic.createContext(cpu, resource)
+ scalingContextA.setTarget(1000.0)
+
+ val scalingContextB = logic.createContext(cpu, resource)
+ scalingContextB.setTarget(3400.0)
+
+ assertEquals(350.0, logic.computePower())
+ }
+
+ @Test
+ fun testPowerBasedOnUtilization() {
+ val machine = mockk<SimBareMetalMachine>()
+ val cpu = mockk<ProcessingUnit>()
+ val resource = mockk<SimResourceSource>()
+
+ every { cpu.frequency } returns 4200.0
+
+ val driver = PStateScalingDriver(
+ sortedMapOf(
+ 2800.0 to LinearPowerModel(200.0, 100.0),
+ 3300.0 to LinearPowerModel(250.0, 150.0),
+ 4000.0 to LinearPowerModel(300.0, 200.0),
+ )
+ )
+
+ val logic = driver.createLogic(machine)
+
+ val scalingContext = logic.createContext(cpu, resource)
+
+ every { resource.speed.value } returns 1400.0
+ scalingContext.setTarget(1400.0)
+ assertEquals(150.0, logic.computePower())
+
+ every { resource.speed.value } returns 1400.0
+ scalingContext.setTarget(4000.0)
+ assertEquals(235.0, logic.computePower())
+ }
+}
diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/PStatePowerModelTest.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/PStatePowerModelTest.kt
deleted file mode 100644
index 9116f928..00000000
--- a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/PStatePowerModelTest.kt
+++ /dev/null
@@ -1,44 +0,0 @@
-package org.opendc.simulator.compute.power
-
-import io.mockk.*
-import org.junit.jupiter.api.Assertions.assertEquals
-import org.junit.jupiter.api.Test
-import org.opendc.simulator.compute.SimBareMetalMachine
-import java.time.Clock
-
-internal class PStatePowerModelTest {
- @Test
- fun `update CPU power meter with P-states`() {
- val p0Power = 8.0
- val p3Power = 94.0
- val p4Power = 103.0
- val expectedP0Power = 8.0 * 10
- val expectedP0P4Power = expectedP0Power + 103.0 * 10
-
- val clock = mockkClass(Clock::class)
- val machine = mockkClass(SimBareMetalMachine::class)
- every { clock.millis() } returnsMany listOf(0L, 0L, 10_000L, 20_000L)
- every { machine.speed } returns
- listOf(2.8, 2.8, 2.8, 2.8).map { it * 1000 } andThen // Max. 2.8MHz covered by P0
- listOf(1.5, 3.1, 3.3, 3.6).map { it * 1000 } andThen // Max. 3.6MHz covered by P4
- listOf(1.5, 3.1, 3.1, 3.3).map { it * 1000 } // Max. 3.3MHz covered by P3
-
- // Power meter initialization.
- val pStatePowerModel = PStatePowerModel(machine, clock)
- verify(exactly = 2) { clock.millis() }
- verify(exactly = 1) { machine.speed }
- assertEquals(p0Power, pStatePowerModel.getInstantCpuPower())
-
- // The first measure.
- pStatePowerModel.updateCpuPowerMeter()
- assertEquals(p4Power, pStatePowerModel.getInstantCpuPower())
- assertEquals(expectedP0Power, pStatePowerModel.getAccumulatedCpuPower())
-
- // The second measure.
- pStatePowerModel.updateCpuPowerMeter()
- assertEquals(p3Power, pStatePowerModel.getInstantCpuPower())
- assertEquals(expectedP0P4Power, pStatePowerModel.getAccumulatedCpuPower())
-
- verify(exactly = 4) { clock.millis() }
- }
-}
diff --git a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/MachinePowerModelTest.kt b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/PowerModelTest.kt
index 13bb3668..439be274 100644
--- a/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/MachinePowerModelTest.kt
+++ b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/PowerModelTest.kt
@@ -1,40 +1,40 @@
package org.opendc.simulator.compute.power
-import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.jupiter.api.Assertions.assertAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
-import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream
import kotlin.math.pow
-@OptIn(ExperimentalCoroutinesApi::class)
-internal class MachinePowerModelTest {
+internal class PowerModelTest {
private val epsilon = 10.0.pow(-3)
private val cpuUtil = 0.9
@ParameterizedTest
@MethodSource("MachinePowerModelArgs")
fun `compute power consumption given CPU loads`(
- powerModel: MachinePowerModel,
+ powerModel: PowerModel,
expectedPowerConsumption: Double
) {
- val computedPowerConsumption = powerModel.computeCpuPower(cpuUtil)
+ val computedPowerConsumption = powerModel.computePower(cpuUtil)
assertEquals(expectedPowerConsumption, computedPowerConsumption, epsilon)
}
@ParameterizedTest
@MethodSource("MachinePowerModelArgs")
fun `ignore idle power when computing power consumptions`(
- powerModel: MachinePowerModel,
+ powerModel: PowerModel,
expectedPowerConsumption: Double
) {
val zeroPowerModel = ZeroIdlePowerDecorator(powerModel)
- val computedPowerConsumption = zeroPowerModel.computeCpuPower(0.0)
- assertEquals(0.0, computedPowerConsumption)
+
+ assertAll(
+ { assertEquals(expectedPowerConsumption, zeroPowerModel.computePower(cpuUtil), epsilon) },
+ { assertEquals(0.0, zeroPowerModel.computePower(0.0)) }
+ )
}
@Test
@@ -42,19 +42,14 @@ internal class MachinePowerModelTest {
val powerModel = InterpolationPowerModel("IBMx3550M3_XeonX5675")
assertAll(
- { assertThrows<IllegalArgumentException> { powerModel.computeCpuPower(-1.3) } },
- { assertThrows<IllegalArgumentException> { powerModel.computeCpuPower(1.3) } },
- )
-
- assertAll(
- { assertEquals(58.4, powerModel.computeCpuPower(0.0)) },
- { assertEquals(58.4 + (98 - 58.4) / 5, powerModel.computeCpuPower(0.02)) },
- { assertEquals(98.0, powerModel.computeCpuPower(0.1)) },
- { assertEquals(140.0, powerModel.computeCpuPower(0.5)) },
- { assertEquals(189.0, powerModel.computeCpuPower(0.8)) },
- { assertEquals(189.0 + 0.7 * 10 * (205 - 189) / 10, powerModel.computeCpuPower(0.87)) },
- { assertEquals(205.0, powerModel.computeCpuPower(0.9)) },
- { assertEquals(222.0, powerModel.computeCpuPower(1.0)) },
+ { assertEquals(58.4, powerModel.computePower(0.0)) },
+ { assertEquals(58.4 + (98 - 58.4) / 5, powerModel.computePower(0.02)) },
+ { assertEquals(98.0, powerModel.computePower(0.1)) },
+ { assertEquals(140.0, powerModel.computePower(0.5)) },
+ { assertEquals(189.0, powerModel.computePower(0.8)) },
+ { assertEquals(189.0 + 0.7 * 10 * (205 - 189) / 10, powerModel.computePower(0.87)) },
+ { assertEquals(205.0, powerModel.computePower(0.9)) },
+ { assertEquals(222.0, powerModel.computePower(1.0)) },
)
}
@@ -63,10 +58,10 @@ internal class MachinePowerModelTest {
@JvmStatic
fun MachinePowerModelArgs(): Stream<Arguments> = Stream.of(
Arguments.of(ConstantPowerModel(0.0), 0.0),
- Arguments.of(LinearPowerModel(350.0, 200 / 350.0), 335.0),
- Arguments.of(SquarePowerModel(350.0, 200 / 350.0), 321.5),
- Arguments.of(CubicPowerModel(350.0, 200 / 350.0), 309.35),
- Arguments.of(SqrtPowerModel(350.0, 200 / 350.0), 342.302),
+ Arguments.of(LinearPowerModel(350.0, 200.0), 335.0),
+ Arguments.of(SquarePowerModel(350.0, 200.0), 321.5),
+ Arguments.of(CubicPowerModel(350.0, 200.0), 309.35),
+ Arguments.of(SqrtPowerModel(350.0, 200.0), 342.302),
)
}
}