Skip to content

Commit

Permalink
updated ScenarioFactory and ScenarioSpec (#239)
Browse files Browse the repository at this point in the history
* Initial Push

* Refactored the cartesian implementation of ScenariosSpec.kt

* Fixed typo

* Some small updates

* ran spotless
  • Loading branch information
DanteNiewenhuis authored Jul 31, 2024
1 parent 461bfa6 commit b287789
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package org.opendc.experiments.base.runner

import CheckpointModelSpec
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand All @@ -36,6 +35,7 @@ import org.opendc.compute.api.ServerWatcher
import org.opendc.compute.failure.models.FailureModel
import org.opendc.compute.service.ComputeService
import org.opendc.compute.workload.VirtualMachine
import org.opendc.experiments.base.scenario.specs.CheckpointModelSpec
import org.opendc.experiments.base.scenario.specs.FailureModelSpec
import org.opendc.experiments.base.scenario.specs.createFailureModel
import java.time.InstantSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

package org.opendc.experiments.base.runner

import getWorkloadType
import me.tongfei.progressbar.ProgressBarBuilder
import me.tongfei.progressbar.ProgressBarStyle
import org.opendc.compute.carbon.CarbonTrace
Expand All @@ -37,6 +36,7 @@ import org.opendc.compute.telemetry.export.parquet.ParquetComputeMonitor
import org.opendc.compute.topology.clusterTopology
import org.opendc.compute.workload.ComputeWorkloadLoader
import org.opendc.experiments.base.scenario.Scenario
import org.opendc.experiments.base.scenario.specs.getWorkloadType
import org.opendc.simulator.kotlin.runSimulation
import java.io.File
import java.time.Duration
Expand All @@ -60,7 +60,7 @@ public fun runScenarios(

setupOutputFolderStructure(scenarios[0].outputFolder)

for ((i, scenario) in scenarios.withIndex()) {
for (scenario in scenarios) {
val pool = ForkJoinPool(parallelism)
println(
"\n\n$ansiGreen================================================================================$ansiReset",
Expand All @@ -70,7 +70,6 @@ public fun runScenarios(
runScenario(
scenario,
pool,
i,
)
}
}
Expand All @@ -85,15 +84,14 @@ public fun runScenarios(
public fun runScenario(
scenario: Scenario,
pool: ForkJoinPool,
index: Int = -1,
) {
val pb =
ProgressBarBuilder().setInitialMax(scenario.runs.toLong()).setStyle(ProgressBarStyle.ASCII)
.setTaskName("Simulating...").build()

pool.submit {
LongStream.range(0, scenario.runs.toLong()).parallel().forEach {
runScenario(scenario, scenario.initialSeed + it, index)
runScenario(scenario, scenario.initialSeed + it)
pb.step()
}
pb.close()
Expand All @@ -109,7 +107,6 @@ public fun runScenario(
public fun runScenario(
scenario: Scenario,
seed: Long,
index: Int = 0,
): Unit =
runSimulation {
val serviceDomain = "compute.opendc.org"
Expand All @@ -129,7 +126,7 @@ public fun runScenario(

val carbonTrace = getCarbonTrace(scenario.carbonTracePath)
val startTime = Duration.ofMillis(vms.minOf { it.startTime }.toEpochMilli())
addExportModel(provisioner, serviceDomain, scenario, seed, startTime, carbonTrace, index)
addExportModel(provisioner, serviceDomain, scenario, seed, startTime, carbonTrace, scenario.id)

val service = provisioner.registry.resolve(serviceDomain, ComputeService::class.java)!!
service.replay(timeSource, vms, failureModelSpec = scenario.failureModelSpec, seed = seed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

package org.opendc.experiments.base.scenario

import AllocationPolicySpec
import CheckpointModelSpec
import ExportModelSpec
import ScenarioTopologySpec
import WorkloadSpec
import org.opendc.experiments.base.scenario.specs.AllocationPolicySpec
import org.opendc.experiments.base.scenario.specs.CheckpointModelSpec
import org.opendc.experiments.base.scenario.specs.ExportModelSpec
import org.opendc.experiments.base.scenario.specs.FailureModelSpec
import org.opendc.experiments.base.scenario.specs.ScenarioTopologySpec
import org.opendc.experiments.base.scenario.specs.WorkloadSpec

/**
* A data class representing a scenario for a set of experiments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package org.opendc.experiments.base.scenario

import org.opendc.experiments.base.scenario.specs.ScenarioSpec
import org.opendc.experiments.base.scenario.specs.ScenariosSpec
import java.io.File

private val scenarioReader = ScenarioReader()
Expand Down Expand Up @@ -52,48 +53,38 @@ public fun getScenarios(file: File): List<Scenario> {
* Returns a list of Scenarios from a given ScenarioSpec by generating all possible combinations of
* workloads, allocation policies, failure models, and export models within a topology.
*
* @param scenarioSpec The ScenarioSpec containing the scenario specifications.
* @param scenariosSpec The ScenarioSpec containing the scenario specifications.
* @return A list of Scenarios.
*/
public fun getScenarios(scenarioSpec: ScenarioSpec): List<Scenario> {
val outputFolder = scenarioSpec.outputFolder + "/" + scenarioSpec.name
public fun getScenarios(scenariosSpec: ScenariosSpec): List<Scenario> {
val outputFolder = scenariosSpec.outputFolder + "/" + scenariosSpec.name
File(outputFolder).mkdirs()

val trackrPath = "$outputFolder/trackr.json"
File(trackrPath).createNewFile()

val scenarios = mutableListOf<Scenario>()

for ((scenarioID, scenarioTopologySpec) in scenarioSpec.topologies.withIndex()) {
for (workloadSpec in scenarioSpec.workloads) {
for (allocationPolicySpec in scenarioSpec.allocationPolicies) {
for (failureModelSpec in scenarioSpec.failureModels) {
for (checkpointModelSpec in scenarioSpec.checkpointModels) {
for (carbonTracePath in scenarioSpec.carbonTracePaths) {
for (exportModelSpec in scenarioSpec.exportModels) {
val scenario =
Scenario(
id = scenarioID,
topologySpec = scenarioTopologySpec,
workloadSpec = workloadSpec,
allocationPolicySpec = allocationPolicySpec,
failureModelSpec = failureModelSpec,
checkpointModelSpec = checkpointModelSpec,
carbonTracePath = carbonTracePath,
exportModelSpec = exportModelSpec,
outputFolder = outputFolder,
name = scenarioID.toString(),
runs = scenarioSpec.runs,
initialSeed = scenarioSpec.initialSeed,
)
trackScenario(scenarioSpec, outputFolder, scenario)
scenarios.add(scenario)
}
}
}
}
}
}
val cartesianInput = scenariosSpec.getCartesian()

for ((scenarioID, scenarioSpec) in cartesianInput.withIndex()) {
val scenario =
Scenario(
id = scenarioID,
topologySpec = scenarioSpec.topology,
workloadSpec = scenarioSpec.workload,
allocationPolicySpec = scenarioSpec.allocationPolicy,
failureModelSpec = scenarioSpec.failureModel,
checkpointModelSpec = scenarioSpec.checkpointModel,
carbonTracePath = scenarioSpec.carbonTracePath,
exportModelSpec = scenarioSpec.exportModel,
outputFolder = outputFolder,
name = scenarioID.toString(),
runs = scenariosSpec.runs,
initialSeed = scenariosSpec.initialSeed,
)
trackScenario(scenarioSpec, outputFolder)
scenarios.add(scenario)
}

return scenarios
Expand All @@ -102,7 +93,7 @@ public fun getScenarios(scenarioSpec: ScenarioSpec): List<Scenario> {
/**
* Writes a ScenarioSpec to a file.
*
* @param scenarioSpec The ScenarioSpec.
* @param scenariosSpec The ScenarioSpec.
* @param outputFolder The output folder path.
* @param scenario The Scenario.
* @param topologySpec The TopologySpec.
Expand All @@ -111,24 +102,10 @@ public fun getScenarios(scenarioSpec: ScenarioSpec): List<Scenario> {
public fun trackScenario(
scenarioSpec: ScenarioSpec,
outputFolder: String,
scenario: Scenario,
) {
val trackrPath = "$outputFolder/trackr.json"
scenarioWriter.write(
ScenarioSpec(
id = scenario.id,
name = scenarioSpec.name,
topologies = listOf(scenario.topologySpec),
workloads = listOf(scenario.workloadSpec),
allocationPolicies = listOf(scenario.allocationPolicySpec),
failureModels = listOf(scenario.failureModelSpec),
checkpointModels = listOf(scenario.checkpointModelSpec),
carbonTracePaths = listOf(scenario.carbonTracePath),
exportModels = listOf(scenario.exportModelSpec),
outputFolder = scenario.outputFolder,
initialSeed = scenario.initialSeed,
runs = scenario.runs,
),
scenarioSpec,
File(trackrPath),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package org.opendc.experiments.base.scenario
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import org.opendc.experiments.base.scenario.specs.ScenarioSpec
import org.opendc.experiments.base.scenario.specs.ScenariosSpec
import java.io.File
import java.io.InputStream
import java.nio.file.Path
Expand All @@ -36,24 +36,24 @@ public class ScenarioReader {
private val jsonReader = Json

@OptIn(ExperimentalSerializationApi::class)
public fun read(file: File): ScenarioSpec {
public fun read(file: File): ScenariosSpec {
val input = file.inputStream()

return jsonReader.decodeFromStream<ScenarioSpec>(input)
return jsonReader.decodeFromStream<ScenariosSpec>(input)
}

@OptIn(ExperimentalSerializationApi::class)
public fun read(path: Path): ScenarioSpec {
public fun read(path: Path): ScenariosSpec {
val input = path.inputStream()

return jsonReader.decodeFromStream<ScenarioSpec>(input)
return jsonReader.decodeFromStream<ScenariosSpec>(input)
}

/**
* Read the specified [input].
*/
@OptIn(ExperimentalSerializationApi::class)
public fun read(input: InputStream): ScenarioSpec {
return jsonReader.decodeFromStream<ScenarioSpec>(input)
public fun read(input: InputStream): ScenariosSpec {
return jsonReader.decodeFromStream<ScenariosSpec>(input)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ScenarioWriter {
private val json = Json { prettyPrint = true }

/**
* Write the given [scenarioSpec] to the given [file].
* Write the given [scenariosSpec] to the given [file].
*/
public fun write(
scenarioSpec: ScenarioSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* SOFTWARE.
*/

package org.opendc.experiments.base.scenario.specs

import kotlinx.serialization.Serializable
import org.opendc.compute.service.scheduler.ComputeSchedulerEnum

Expand All @@ -32,7 +34,7 @@ import org.opendc.compute.service.scheduler.ComputeSchedulerEnum
*/
@Serializable
public data class AllocationPolicySpec(
val policyType: ComputeSchedulerEnum,
val policyType: ComputeSchedulerEnum = ComputeSchedulerEnum.Mem,
) {
public val name: String = policyType.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* SOFTWARE.
*/

package org.opendc.experiments.base.scenario.specs

import kotlinx.serialization.Serializable

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* SOFTWARE.
*/

package org.opendc.experiments.base.scenario.specs

import kotlinx.serialization.Serializable

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* SOFTWARE.
*/

package org.opendc.experiments.base.scenario.specs

import kotlinx.serialization.Serializable

@Serializable
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* SOFTWARE.
*/

package org.opendc.experiments.base.scenario.specs

import kotlinx.serialization.Serializable
import java.io.File

Expand Down
Loading

0 comments on commit b287789

Please sign in to comment.