From 1c0568c31d60d4e690b4b9aec2e14f660b72a5c8 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 21 Apr 2021 14:30:55 +0200 Subject: simulator: Introduce SimulationCoroutineDispatcher This change introduces the SimulationCoroutineDispatcher implementation which replaces the TestCoroutineDispatcher for running single-threaded simulations. Previously, we used the TestCoroutineDispatcher from the kotlinx-coroutines-test modules for running simulations. However, this module is aimed at coroutine tests and not at simulations. In particular, having to construct a Clock object each time for the TestCoroutineDispatcher caused a lot of unnecessary lines. With the new approach, the SimulationCoroutineDispatcher automatically exposes a usable Clock object. In addition to ergonomic benefits, the SimulationCoroutineDispatcher is much faster than the TestCoroutineDispatcher due to the assumption that simulations run in only a single thread. As a result, the dispatcher does not need to perform synchronization and can use the fast PriorityQueue implementation. --- .../test/kotlin/org/opendc/serverless/service/ServerlessServiceTest.kt | 2 +- .../kotlin/org/opendc/serverless/simulator/SimServerlessServiceTest.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'simulator/opendc-serverless') diff --git a/simulator/opendc-serverless/opendc-serverless-service/src/test/kotlin/org/opendc/serverless/service/ServerlessServiceTest.kt b/simulator/opendc-serverless/opendc-serverless-service/src/test/kotlin/org/opendc/serverless/service/ServerlessServiceTest.kt index bf99d0e7..1ab2d033 100644 --- a/simulator/opendc-serverless/opendc-serverless-service/src/test/kotlin/org/opendc/serverless/service/ServerlessServiceTest.kt +++ b/simulator/opendc-serverless/opendc-serverless-service/src/test/kotlin/org/opendc/serverless/service/ServerlessServiceTest.kt @@ -34,7 +34,7 @@ import org.opendc.serverless.api.ServerlessFunction import org.opendc.serverless.service.deployer.FunctionDeployer import org.opendc.serverless.service.deployer.FunctionInstance import org.opendc.serverless.service.deployer.FunctionInstanceState -import org.opendc.simulator.utils.DelayControllerClockAdapter +import org.opendc.simulator.core.DelayControllerClockAdapter import java.util.* /** diff --git a/simulator/opendc-serverless/opendc-serverless-simulator/src/test/kotlin/org/opendc/serverless/simulator/SimServerlessServiceTest.kt b/simulator/opendc-serverless/opendc-serverless-simulator/src/test/kotlin/org/opendc/serverless/simulator/SimServerlessServiceTest.kt index 3a070475..7411ffa1 100644 --- a/simulator/opendc-serverless/opendc-serverless-simulator/src/test/kotlin/org/opendc/serverless/simulator/SimServerlessServiceTest.kt +++ b/simulator/opendc-serverless/opendc-serverless-simulator/src/test/kotlin/org/opendc/serverless/simulator/SimServerlessServiceTest.kt @@ -42,7 +42,7 @@ import org.opendc.simulator.compute.model.ProcessingNode import org.opendc.simulator.compute.model.ProcessingUnit import org.opendc.simulator.compute.workload.SimFlopsWorkload import org.opendc.simulator.compute.workload.SimWorkload -import org.opendc.simulator.utils.DelayControllerClockAdapter +import org.opendc.simulator.core.DelayControllerClockAdapter /** * A test suite for the [ServerlessService] implementation under simulated conditions. -- cgit v1.2.3 From 62678b2890a7f3640836b99ca2fec9efd7485929 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 21 Apr 2021 16:35:52 +0200 Subject: simulator: Migrate to SimulationCoroutineDispatcher This change migrates the remainder of the codebase to the SimulationCoroutineDispatcher implementation. --- .../serverless/service/ServerlessServiceTest.kt | 33 ++++++++-------------- .../simulator/SimServerlessServiceTest.kt | 6 ++-- 2 files changed, 13 insertions(+), 26 deletions(-) (limited to 'simulator/opendc-serverless') diff --git a/simulator/opendc-serverless/opendc-serverless-service/src/test/kotlin/org/opendc/serverless/service/ServerlessServiceTest.kt b/simulator/opendc-serverless/opendc-serverless-service/src/test/kotlin/org/opendc/serverless/service/ServerlessServiceTest.kt index 1ab2d033..d9f5ee81 100644 --- a/simulator/opendc-serverless/opendc-serverless-service/src/test/kotlin/org/opendc/serverless/service/ServerlessServiceTest.kt +++ b/simulator/opendc-serverless/opendc-serverless-service/src/test/kotlin/org/opendc/serverless/service/ServerlessServiceTest.kt @@ -25,7 +25,6 @@ package org.opendc.serverless.service import io.mockk.* import io.opentelemetry.api.metrics.MeterProvider import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.runBlockingTest import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow @@ -34,7 +33,7 @@ import org.opendc.serverless.api.ServerlessFunction import org.opendc.serverless.service.deployer.FunctionDeployer import org.opendc.serverless.service.deployer.FunctionInstance import org.opendc.serverless.service.deployer.FunctionInstanceState -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation import java.util.* /** @@ -44,9 +43,8 @@ import java.util.* internal class ServerlessServiceTest { @Test - fun testClientState() = runBlockingTest { + fun testClientState() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val service = ServerlessService(coroutineContext, clock, meter, mockk(), mockk()) val client = assertDoesNotThrow { service.newClient() } @@ -60,9 +58,8 @@ internal class ServerlessServiceTest { } @Test - fun testClientInvokeUnknown() = runBlockingTest { + fun testClientInvokeUnknown() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val service = ServerlessService(coroutineContext, clock, meter, mockk(), mockk()) val client = service.newClient() @@ -71,9 +68,8 @@ internal class ServerlessServiceTest { } @Test - fun testClientFunctionCreation() = runBlockingTest { + fun testClientFunctionCreation() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val service = ServerlessService(coroutineContext, clock, meter, mockk(), mockk()) val client = service.newClient() @@ -84,9 +80,8 @@ internal class ServerlessServiceTest { } @Test - fun testClientFunctionQuery() = runBlockingTest { + fun testClientFunctionQuery() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val service = ServerlessService(coroutineContext, clock, meter, mockk(), mockk()) val client = service.newClient() @@ -99,9 +94,8 @@ internal class ServerlessServiceTest { } @Test - fun testClientFunctionFindById() = runBlockingTest { + fun testClientFunctionFindById() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val service = ServerlessService(coroutineContext, clock, meter, mockk(), mockk()) val client = service.newClient() @@ -114,9 +108,8 @@ internal class ServerlessServiceTest { } @Test - fun testClientFunctionFindByName() = runBlockingTest { + fun testClientFunctionFindByName() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val service = ServerlessService(coroutineContext, clock, meter, mockk(), mockk()) val client = service.newClient() @@ -129,9 +122,8 @@ internal class ServerlessServiceTest { } @Test - fun testClientFunctionDuplicateName() = runBlockingTest { + fun testClientFunctionDuplicateName() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val service = ServerlessService(coroutineContext, clock, meter, mockk(), mockk()) val client = service.newClient() @@ -142,9 +134,8 @@ internal class ServerlessServiceTest { } @Test - fun testClientFunctionDelete() = runBlockingTest { + fun testClientFunctionDelete() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val service = ServerlessService(coroutineContext, clock, meter, mockk(), mockk()) val client = service.newClient() @@ -158,9 +149,8 @@ internal class ServerlessServiceTest { } @Test - fun testClientFunctionCannotInvokeDeleted() = runBlockingTest { + fun testClientFunctionCannotInvokeDeleted() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val service = ServerlessService(coroutineContext, clock, meter, mockk(), mockk()) val client = service.newClient() @@ -172,9 +162,8 @@ internal class ServerlessServiceTest { } @Test - fun testClientFunctionInvoke() = runBlockingTest { + fun testClientFunctionInvoke() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val deployer = mockk() val service = ServerlessService(coroutineContext, clock, meter, deployer, mockk()) diff --git a/simulator/opendc-serverless/opendc-serverless-simulator/src/test/kotlin/org/opendc/serverless/simulator/SimServerlessServiceTest.kt b/simulator/opendc-serverless/opendc-serverless-simulator/src/test/kotlin/org/opendc/serverless/simulator/SimServerlessServiceTest.kt index 7411ffa1..9592d870 100644 --- a/simulator/opendc-serverless/opendc-serverless-simulator/src/test/kotlin/org/opendc/serverless/simulator/SimServerlessServiceTest.kt +++ b/simulator/opendc-serverless/opendc-serverless-simulator/src/test/kotlin/org/opendc/serverless/simulator/SimServerlessServiceTest.kt @@ -27,7 +27,6 @@ import io.mockk.spyk import io.opentelemetry.api.metrics.MeterProvider import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay -import kotlinx.coroutines.test.runBlockingTest import kotlinx.coroutines.yield import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -42,7 +41,7 @@ import org.opendc.simulator.compute.model.ProcessingNode import org.opendc.simulator.compute.model.ProcessingUnit import org.opendc.simulator.compute.workload.SimFlopsWorkload import org.opendc.simulator.compute.workload.SimWorkload -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation /** * A test suite for the [ServerlessService] implementation under simulated conditions. @@ -63,9 +62,8 @@ internal class SimServerlessServiceTest { } @Test - fun testSmoke() = runBlockingTest { + fun testSmoke() = runBlockingSimulation { val meter = MeterProvider.noop().get("opendc-serverless") - val clock = DelayControllerClockAdapter(this) val workload = spyk(object : SimServerlessWorkload, SimWorkload by SimFlopsWorkload(1000) { override suspend fun invoke() {} }) -- cgit v1.2.3