summaryrefslogtreecommitdiff
path: root/simulator/opendc-simulator/opendc-simulator-compute/src/test
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-03-24 13:05:33 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-03-24 13:05:33 +0100
commit69598598be2c248acc49e40607b3dd0998ec1ca5 (patch)
tree62e154b6cc5b22f79f3aa67cf245c40c1332f7b4 /simulator/opendc-simulator/opendc-simulator-compute/src/test
parent0ad600eb8e14c0ef3ba5529c59d300dc20c85ab2 (diff)
simulator: Move power models to simulator module
This change moves the power models from the `opendc-compute-simulator` to the `opendc-simulator-compute` module, since it better fits the scope of the models and allows them to be re-used for other purposes.
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/power/MachinePowerModelTest.kt50
-rw-r--r--simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/PStatePowerModelTest.kt44
2 files changed, 94 insertions, 0 deletions
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/MachinePowerModelTest.kt
new file mode 100644
index 00000000..9fdd0363
--- /dev/null
+++ b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/MachinePowerModelTest.kt
@@ -0,0 +1,50 @@
+package org.opendc.simulator.compute.power
+
+import io.mockk.*
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.flow.*
+import org.junit.jupiter.api.Assertions.assertEquals
+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 {
+ private val epsilon = 10.0.pow(-3)
+ private val cpuUtil = 0.9
+
+ @ParameterizedTest
+ @MethodSource("MachinePowerModelArgs")
+ fun `compute power consumption given CPU loads`(
+ powerModel: MachinePowerModel,
+ expectedPowerConsumption: Double
+ ) {
+ val computedPowerConsumption = powerModel.computeCpuPower(cpuUtil)
+ assertEquals(expectedPowerConsumption, computedPowerConsumption, epsilon)
+ }
+
+ @ParameterizedTest
+ @MethodSource("MachinePowerModelArgs")
+ fun `ignore idle power when computing power consumptions`(
+ powerModel: MachinePowerModel,
+ expectedPowerConsumption: Double
+ ) {
+ val zeroPowerModel = ZeroIdlePowerDecorator(powerModel)
+ val computedPowerConsumption = zeroPowerModel.computeCpuPower(0.0)
+ assertEquals(0.0, computedPowerConsumption)
+ }
+
+ @Suppress("unused")
+ private companion object {
+ @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),
+ )
+ }
+}
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
new file mode 100644
index 00000000..9116f928
--- /dev/null
+++ b/simulator/opendc-simulator/opendc-simulator-compute/src/test/kotlin/org/opendc/simulator/compute/power/PStatePowerModelTest.kt
@@ -0,0 +1,44 @@
+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() }
+ }
+}