Refactor TestSaveExamplesNet to improve structure and add CSV loading tests

This commit is contained in:
2025-02-11 13:34:20 +01:00
parent 651280aba2
commit 7712b4aaa8
4 changed files with 8098 additions and 8040 deletions

View File

@@ -3,13 +3,19 @@ package net.berack.upo.valpre.sim;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import com.esotericsoftware.kryo.KryoException;
import net.berack.upo.valpre.SimulationBuilder;
import net.berack.upo.valpre.rand.Distribution;
import net.berack.upo.valpre.rand.Rng;
import net.berack.upo.valpre.sim.stats.CsvResult;
import net.berack.upo.valpre.sim.stats.NodeStats;
public class TestSaveExamplesNet {
@@ -28,21 +34,44 @@ public class TestSaveExamplesNet {
private static final int spawn = 10000;
private static final String path = "src/main/resources/example%d.%s";
private static final String net1 = path.formatted(1, "net");
private static final String net2 = path.formatted(2, "net");
private static final String net3 = path.formatted(3, "net");
private static final String netFile1 = path.formatted(1, "net");
private static final String netFile2 = path.formatted(2, "net");
private static final String netFile3 = path.formatted(3, "net");
private static final String csv1 = path.formatted(1, "csv");
private static final String csv2 = path.formatted(2, "csv");
private static final String csv3 = path.formatted(3, "csv");
private static final Net net1 = new Net();
private static final Net net2 = new Net();
private static final Net net3 = new Net();
static {
net1.addNode(ServerNode.Builder.sourceLimited("Source", spawn, exp0_22));
net1.addNode(ServerNode.Builder.queue("Queue", 1, norm3_2));
net1.addConnection(0, 1, 1.0);
net2.addNode(ServerNode.Builder.sourceLimited("Source", spawn, exp0_22));
net2.addNode(ServerNode.Builder.queue("Queue", 1, norm3_2));
net2.addNode(ServerNode.Builder.queue("Queue Wait", 1, norm3_2, unNorm));
net2.addConnection(0, 1, 1.0);
net2.addConnection(1, 2, 1.0);
net3.addNode(ServerNode.Builder.sourceLimited("Source", spawn, exp1_5));
net3.addNode(ServerNode.Builder.queue("Service1", 1, exp2));
net3.addNode(ServerNode.Builder.queue("Service2", 1, exp3_5, unExp));
net3.addConnection(0, 1, 1.0);
net3.addConnection(1, 2, 1.0);
}
@BeforeAll
public void saveAll() throws IOException {
net1.save(netFile1);
net2.save(netFile2);
net3.save(netFile3);
}
@Test
public void testSaveExample1() throws KryoException, IOException {
var net = new Net();
net.addNode(ServerNode.Builder.sourceLimited("Source", spawn, exp0_22));
net.addNode(ServerNode.Builder.queue("Queue", 1, norm3_2));
net.addConnection(0, 1, 1.0);
net.save(net1);
net = Net.load(net1);
var sim = new Simulation(net, new Rng());
public void loadExample1() throws KryoException, IOException {
var sim = new Simulation(Net.load(netFile1), new Rng());
var res = sim.run();
var time = 44782.0;
var maxErr = time / 1000.0;
@@ -54,18 +83,8 @@ public class TestSaveExamplesNet {
}
@Test
public void testSaveExample2() throws KryoException, IOException {
var net = new Net();
net.addNode(ServerNode.Builder.sourceLimited("Source", spawn, exp0_22));
net.addNode(ServerNode.Builder.queue("Queue", 1, norm3_2));
net.addNode(ServerNode.Builder.queue("Queue Wait", 1, norm3_2, unNorm));
net.addConnection(0, 1, 1.0);
net.addConnection(1, 2, 1.0);
net.save(net2);
net = Net.load(net2);
var sim = new Simulation(net, new Rng());
public void loadExample2() throws KryoException, IOException {
var sim = new Simulation(Net.load(netFile2), new Rng());
var res = sim.run();
var time = 45417.0;
var maxErr = time / 1000.0;
@@ -78,18 +97,8 @@ public class TestSaveExamplesNet {
}
@Test
public void testSaveExample3() throws KryoException, IOException {
var net = new Net();
net.addNode(ServerNode.Builder.sourceLimited("Source", spawn, exp1_5));
net.addNode(ServerNode.Builder.queue("Service1", 1, exp2));
net.addNode(ServerNode.Builder.queue("Service2", 1, exp3_5, unExp));
net.addConnection(0, 1, 1.0);
net.addConnection(1, 2, 1.0);
net.save(net3);
net = Net.load(net3);
var sim = new Simulation(net, new Rng());
public void loadExample3() throws KryoException, IOException {
var sim = new Simulation(Net.load(netFile3), new Rng());
var res = sim.run();
var time = 6736.0;
var maxErr = time / 1000.0;
@@ -125,4 +134,53 @@ public class TestSaveExamplesNet {
assertEquals("% Busy", stat.busyTime / stat.lastEventTime, stat.utilization, 0.001);
assertEquals("% Unavailable", stat.unavailableTime / stat.lastEventTime, stat.unavailable, 0.001);
}
@Test
public void loadCsv() throws IOException {
var list = new CsvResult(csv1).loadResults();
var seeds = new HashSet<Long>();
for (var element : list) {
assertEquals(Set.of("Source", "Queue"), element.nodes.keySet());
assertEquals(10000, element.nodes.get("Source").numArrivals, 0.1);
assertEquals(10000, element.nodes.get("Queue").numArrivals, 0.1);
assertEquals(0.22, element.nodes.get("Source").throughput, 0.1);
assertEquals(0.22, element.nodes.get("Queue").throughput, 0.1);
seeds.add(element.seed);
}
assertEquals(list.size(), seeds.size());
}
@Test
@AfterAll
public void multiSimulation1() throws Exception {
new SimulationBuilder(net1)
.setCsv(csv1)
.setMaxRuns(1000)
.setSeed(2007539552L)
.setParallel(true)
.run();
}
@Test
@AfterAll
public void multiSimulation2() throws Exception {
new SimulationBuilder(net2)
.setCsv(csv2)
.setMaxRuns(1000)
.setSeed(2007539552L)
.setParallel(true)
.run();
}
@Test
@AfterAll
public void multiSimulation3() throws Exception {
new SimulationBuilder(net3)
.setCsv(csv3)
.setMaxRuns(1000)
.setSeed(2007539552L)
.setParallel(true)
.run();
}
}