Commit 5e54ea41 by Michael Schmid

Last changes and unit tests

parent 6bc244f2
...@@ -12,7 +12,7 @@ import mvd.jester.priority.RateMonotonic; ...@@ -12,7 +12,7 @@ import mvd.jester.priority.RateMonotonic;
public class App { public class App {
public static void main(String[] args) { public static void main(String[] args) {
SystemSetup.Builder builder = new SystemSetup.Builder().setNumberOfProcessors(8); SystemSetup.Builder builder = new SystemSetup.Builder().setNumberOfProcessors(8);
TestEnvironment te = new TestEnvironment(builder, 4000); TestEnvironment te = new TestEnvironment(builder, 40000);
te.registerSchedulingAlgorithm(new RateMonotonic()); te.registerSchedulingAlgorithm(new RateMonotonic());
te.registerSchedulingAlgorithm(new EarliestDeadlineFirst()); te.registerSchedulingAlgorithm(new EarliestDeadlineFirst());
...@@ -24,44 +24,5 @@ public class App { ...@@ -24,44 +24,5 @@ public class App {
mvd.jester.simulator.SchmidMottok.class); mvd.jester.simulator.SchmidMottok.class);
te.runTests(); te.runTests();
// boolean mbWorked = false;
// boolean smWorked = false;
// SystemSetup thisOne = null;
// EventPrinter.enablePrinter();
// do {
// SystemSetup.Builder builder = new SystemSetup.Builder().setNumberOfProcessors(8);
// SystemSetup systemSetup = builder.build();
// SchmidMottok sm = new SchmidMottok(systemSetup);
// MaiaBertogna mb = new MaiaBertogna(systemSetup);
// smWorked = sm.runSimulation();
// mbWorked = mb.runSimulation();
// thisOne = systemSetup;
// } while (smWorked == mbWorked);
// try (PrintWriter out = new PrintWriter("./results/manCheck.txt")) {
// out.println(thisOne);
// } catch (Exception e) {
// System.out.println("Ähm something went horribly wrong!");
// }
// SchmidMottok sm = new SchmidMottok(thisOne);
// MaiaBertogna mb = new MaiaBertogna(thisOne);
// mb.runSimulation();
// sm.runSimulation();
// SystemSetup systemSetup = SystemSetup.fromFile("results/manCheck.txt");
// EventPrinter.enablePrinter();
// SchmidMottok sm = new SchmidMottok(systemSetup);
// sm.runSimulation();
} }
} }
...@@ -6,6 +6,7 @@ import java.util.HashMap; ...@@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table; import com.google.common.collect.Table;
import mvd.jester.model.SystemSetup; import mvd.jester.model.SystemSetup;
import mvd.jester.priority.PriorityManager; import mvd.jester.priority.PriorityManager;
...@@ -81,11 +82,11 @@ public class TestEnvironment { ...@@ -81,11 +82,11 @@ public class TestEnvironment {
Set<AbstractTest> abstractTestInstances = new HashSet<>(); Set<AbstractTest> abstractTestInstances = new HashSet<>();
Set<AbstractSimulator> abstractSimulatorInstances = new HashSet<>(); Set<AbstractSimulator> abstractSimulatorInstances = new HashSet<>();
Map<Long, Map<AbstractTest, Long>> testResults = new HashMap<>(); Map<Long, Long> totalNumberOfTasks = new HashMap<>();
Map<Long, Map<AbstractSimulator, Long>> simulatorResults = new HashMap<>();
// Map<Long, Table<PriorityManager, AbstractTest, Long>> testResults = new HashMap<>(); Map<Long, Table<PriorityManager, AbstractTest, Long>> testResults = new HashMap<>();
// Map<Long, Table<PriorityManager, AbstractSimulator, Long>> simulatorResults = Map<Long, Table<PriorityManager, AbstractSimulator, Long>> simulatorResults =
// new HashMap<>(); new HashMap<>();
for (Constructor<? extends AbstractTest> c : abstractTests) { for (Constructor<? extends AbstractTest> c : abstractTests) {
try { try {
...@@ -120,24 +121,34 @@ public class TestEnvironment { ...@@ -120,24 +121,34 @@ public class TestEnvironment {
long roundedUtilization = (long) (utilization * 10); long roundedUtilization = (long) (utilization * 10);
totalNumberOfTasks.compute(roundedUtilization, (k, v) -> v == null ? 1 : v + 1);
for (PriorityManager priorityManager : schedulingAlgorithms) { for (PriorityManager priorityManager : schedulingAlgorithms) {
for (AbstractTest test : abstractTestInstances) { for (AbstractTest test : abstractTestInstances) {
Table<PriorityManager, AbstractTest, Long> table = testResults
.computeIfAbsent(roundedUtilization, k -> HashBasedTable.create());
if (priorityManager.hasTest(test) if (priorityManager.hasTest(test)
&& test.runSchedulabilityCheck(priorityManager)) { && test.runSchedulabilityCheck(priorityManager)) {
testResults Long val = table.get(priorityManager, test);
.computeIfAbsent(roundedUtilization, if (val == null) {
k -> new HashMap<AbstractTest, Long>()) table.put(priorityManager, test, (long) 1);
.compute(test, (k, v) -> (v == null) ? 1 : v + 1); } else {
table.put(priorityManager, test, val + 1);
}
} }
} }
for (AbstractSimulator simulator : abstractSimulatorInstances) { for (AbstractSimulator simulator : abstractSimulatorInstances) {
Table<PriorityManager, AbstractSimulator, Long> table = simulatorResults
.computeIfAbsent(roundedUtilization, k -> HashBasedTable.create());
if (priorityManager.hasSimulator(simulator) if (priorityManager.hasSimulator(simulator)
&& simulator.runSimulation(priorityManager)) { && simulator.runSimulation(priorityManager)) {
simulatorResults Long val = table.get(priorityManager, simulator);
.computeIfAbsent(roundedUtilization, if (val == null) {
k -> new HashMap<AbstractSimulator, Long>()) table.put(priorityManager, simulator, (long) 1);
.compute(simulator, (k, v) -> (v == null) ? 1 : v + 1); } else {
table.put(priorityManager, simulator, val + 1);
}
} }
} }
...@@ -148,66 +159,84 @@ public class TestEnvironment { ...@@ -148,66 +159,84 @@ public class TestEnvironment {
} }
} }
logTestResults(abstractTestInstances, testResults); logTestResults(testResults, totalNumberOfTasks);
logSimulationResults(abstractSimulatorInstances, simulatorResults); logSimulationResults(simulatorResults, totalNumberOfTasks);
} }
private void logTestResults(Set<AbstractTest> testCases, private void logTestResults(Map<Long, Table<PriorityManager, AbstractTest, Long>> results,
Map<Long, Map<AbstractTest, Long>> results) { Map<Long, Long> totalNumberOfTasks) {
LocalTime date = LocalTime.now(); LocalTime date = LocalTime.now();
Logger log = new Logger("./results/results_" + systemSetup.getNumberOfProcessors() + "_" Logger log = new Logger("./results/results_" + systemSetup.getNumberOfProcessors() + "_"
+ date.getHour() + ":" + date.getMinute() + ".txt"); + date.getHour() + ":" + date.getMinute() + ".txt");
String firstLine = new String("Utilization"); String firstLine = new String("Utilization");
for (AbstractTest t : testCases) { if (!results.isEmpty()) {
firstLine = firstLine + "\t" + t.getName(); Long first = results.keySet().iterator().next();
for (PriorityManager p : results.get(first).rowKeySet()) {
for (AbstractTest t : results.get(first).columnKeySet()) {
firstLine = firstLine + "\t" + t.getName() + "_" + p.getName();
}
} }
log.log(firstLine); log.log(firstLine);
for (Long util : results.keySet()) { for (Long util : results.keySet()) {
String line = String.valueOf((double) util / 10); String line = String.valueOf((double) util / 10);
Map<AbstractTest, Long> tests = results.get(util); Table<PriorityManager, AbstractTest, Long> tests = results.get(util);
for (AbstractTest t : testCases) { for (PriorityManager p : tests.rowKeySet()) {
if (tests.containsKey(t)) { for (AbstractTest t : tests.columnKeySet()) {
line += "\t" + tests.get(t); if (tests.contains(p, t)) {
line += "\t" + tests.get(p, t);
} else { } else {
line += "\t" + "0"; line += "\t" + "0";
} }
} }
}
log.log(line); log.log(line);
} }
}
log.finalize(); log.finalize();
} }
private void logSimulationResults(Set<AbstractSimulator> testCases, private void logSimulationResults(
Map<Long, Map<AbstractSimulator, Long>> results) { Map<Long, Table<PriorityManager, AbstractSimulator, Long>> results,
Map<Long, Long> totalNumberOfTasks) {
LocalTime date = LocalTime.now(); LocalTime date = LocalTime.now();
Logger log = new Logger("./results/results_sim_" + systemSetup.getNumberOfProcessors() + "_" Logger log = new Logger("./results/results_sim_" + systemSetup.getNumberOfProcessors() + "_"
+ date.getHour() + ":" + date.getMinute() + ".txt"); + date.getHour() + ":" + date.getMinute() + ".txt");
String firstLine = new String("Utilization"); String firstLine = new String("Utilization\tTotal");
if (!results.isEmpty()) {
Long first = results.keySet().iterator().next();
for (AbstractSimulator t : testCases) { for (PriorityManager p : results.get(first).rowKeySet()) {
firstLine = firstLine + "\t" + t.getName(); for (AbstractSimulator s : results.get(first).columnKeySet()) {
firstLine = firstLine + "\t" + s.getName() + "_" + p.getName();
}
} }
log.log(firstLine); log.log(firstLine);
for (Long util : results.keySet()) { for (Long util : totalNumberOfTasks.keySet()) {
String line = String.valueOf((double) util / 10); String line = String.valueOf((double) util / 10);
Map<AbstractSimulator, Long> tests = results.get(util); line += "\t" + totalNumberOfTasks.get(util);
Table<PriorityManager, AbstractSimulator, Long> tests = results.get(util);
for (AbstractSimulator t : testCases) { for (PriorityManager p : tests.rowKeySet()) {
if (tests.containsKey(t)) { for (AbstractSimulator s : tests.columnKeySet()) {
line += "\t" + tests.get(t); if (tests.contains(p, s)) {
line += "\t" + tests.get(p, s);
} else { } else {
line += "\t" + "0"; line += "\t" + "0";
} }
} }
}
log.log(line); log.log(line);
} }
}
log.finalize(); log.finalize();
} }
......
...@@ -6,6 +6,7 @@ import java.util.Set; ...@@ -6,6 +6,7 @@ import java.util.Set;
import mvd.jester.model.Task; import mvd.jester.model.Task;
import mvd.jester.simulator.AbstractSimulator; import mvd.jester.simulator.AbstractSimulator;
import mvd.jester.simulator.MaiaBertogna; import mvd.jester.simulator.MaiaBertogna;
import mvd.jester.simulator.SchmidMottok;
import mvd.jester.simulator.internals.TaskContextInterface; import mvd.jester.simulator.internals.TaskContextInterface;
import mvd.jester.tests.AbstractTest; import mvd.jester.tests.AbstractTest;
...@@ -16,7 +17,7 @@ public class EarliestDeadlineFirst implements PriorityManager { ...@@ -16,7 +17,7 @@ public class EarliestDeadlineFirst implements PriorityManager {
final static Set<Class<? extends AbstractTest>> abstractTests = new HashSet<>(); final static Set<Class<? extends AbstractTest>> abstractTests = new HashSet<>();
final static Set<Class<? extends AbstractSimulator>> abstractSimulators = final static Set<Class<? extends AbstractSimulator>> abstractSimulators =
new HashSet<>(Arrays.asList(MaiaBertogna.class)); new HashSet<>(Arrays.asList(MaiaBertogna.class, SchmidMottok.class));
/** /**
* Compare the priority of two tasks according to the Rate Monotonic policy * Compare the priority of two tasks according to the Rate Monotonic policy
......
...@@ -91,7 +91,7 @@ public abstract class AbstractSimulator implements SimulatorInterface { ...@@ -91,7 +91,7 @@ public abstract class AbstractSimulator implements SimulatorInterface {
private long getHyperPeriod() { private long getHyperPeriod() {
return systemSetup.getTasks().stream().max(Comparator.comparing(Task::getPeriod)).get() return systemSetup.getTasks().stream().max(Comparator.comparing(Task::getPeriod)).get()
.getPeriod() * 2; /* * 10 */ .getPeriod() * 10;
} }
private class ProcessorComparator implements Comparator<ProcessorContext> { private class ProcessorComparator implements Comparator<ProcessorContext> {
......
...@@ -30,7 +30,7 @@ public class SegmentContext { ...@@ -30,7 +30,7 @@ public class SegmentContext {
return segment; return segment;
} }
Optional<JobContextInterface> getNextJob() { public Optional<JobContextInterface> getNextJob() {
return jobs.stream() return jobs.stream()
.filter(j -> !j.getCurrentProcessor().isPresent() && j.checkExecutionTime()) .filter(j -> !j.getCurrentProcessor().isPresent() && j.checkExecutionTime())
.findFirst(); .findFirst();
......
...@@ -64,10 +64,7 @@ public class TaskContext implements TaskContextInterface { ...@@ -64,10 +64,7 @@ public class TaskContext implements TaskContextInterface {
public Optional<JobContextInterface> getNextJob() { public Optional<JobContextInterface> getNextJob() {
if (currentSegment < segments.size()) { if (currentSegment < segments.size()) {
Optional<JobContextInterface> optionalJob = segments.get(currentSegment).getNextJob(); return segments.get(currentSegment).getNextJob();
if (optionalJob.isPresent()) {
return optionalJob;
}
} }
return Optional.empty(); return Optional.empty();
......
...@@ -43,7 +43,7 @@ public class SegmentContext { ...@@ -43,7 +43,7 @@ public class SegmentContext {
return jobs.size(); return jobs.size();
} }
Optional<JobContextInterface> getNextJob() { public Optional<JobContextInterface> getNextJob() {
boolean taskletAvailable = tasklets.stream() boolean taskletAvailable = tasklets.stream()
.anyMatch(t -> !t.getCurrentJob().isPresent() && t.checkExecutionTime()); .anyMatch(t -> !t.getCurrentJob().isPresent() && t.checkExecutionTime());
...@@ -63,7 +63,7 @@ public class SegmentContext { ...@@ -63,7 +63,7 @@ public class SegmentContext {
} }
} }
Optional<TaskletContext> getNextTasklet() { public Optional<TaskletContext> getNextTasklet() {
return tasklets.stream() return tasklets.stream()
.filter(t -> !t.getCurrentJob().isPresent() && t.checkExecutionTime()).findFirst(); .filter(t -> !t.getCurrentJob().isPresent() && t.checkExecutionTime()).findFirst();
} }
......
...@@ -64,10 +64,7 @@ public class TaskContext implements TaskContextInterface { ...@@ -64,10 +64,7 @@ public class TaskContext implements TaskContextInterface {
public Optional<JobContextInterface> getNextJob() { public Optional<JobContextInterface> getNextJob() {
if (currentSegment < segments.size()) { if (currentSegment < segments.size()) {
Optional<JobContextInterface> optionalJob = segments.get(currentSegment).getNextJob(); return segments.get(currentSegment).getNextJob();
if (optionalJob.isPresent()) {
return optionalJob;
}
} }
return Optional.empty(); return Optional.empty();
......
package mvd.jester.model;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.common.math.LongMath;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* SegmentTest
*/
public class TestSegment {
@Test
@DisplayName("Test if dynamic segments are constructed correctly.")
public void testSegmentConstruction() {
Segment s1 = new Segment(100, 10);
Segment s2 = new Segment(9, 10);
assertTrue(s1.getJobWcet() == 100);
assertTrue(s1.getNumberOfJobs() == 10);
assertTrue(s1.getNumberOfTasklets() == 100);
assertTrue(s1.getTaskletWcet() == 10);
assertTrue(s2.getJobWcet() == 9);
assertTrue(s2.getNumberOfJobs() == 10);
assertTrue(s2.getNumberOfTasklets() == 9 * 10 / LongMath.gcd(9, 10));
assertTrue(s2.getTaskletWcet() == LongMath.gcd(9, 10));
}
}
...@@ -5,7 +5,7 @@ import org.junit.jupiter.api.DisplayName; ...@@ -5,7 +5,7 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class TaskSetTest { public class TestSystemSetup {
private static final int NUMBER_OF_SETS = 1000; private static final int NUMBER_OF_SETS = 1000;
...@@ -14,7 +14,8 @@ public class TaskSetTest { ...@@ -14,7 +14,8 @@ public class TaskSetTest {
public void testRandomTaskSetGeneration() { public void testRandomTaskSetGeneration() {
for (int i = 0; i < NUMBER_OF_SETS; ++i) { for (int i = 0; i < NUMBER_OF_SETS; ++i) {
SystemSetup taskSet = new SystemSetup.Builder().build(); SystemSetup taskSet = new SystemSetup.Builder().setNumberOfSegments(1, 7)
.setNumberOfJobs(2, 10).setPeriods(100, 1000).build();
for (Task t : taskSet.getTasks()) { for (Task t : taskSet.getTasks()) {
assertTrue(t.getPeriod() >= 100); assertTrue(t.getPeriod() >= 100);
...@@ -29,11 +30,11 @@ public class TaskSetTest { ...@@ -29,11 +30,11 @@ public class TaskSetTest {
assertTrue(s.getJobWcet() >= 1); assertTrue(s.getJobWcet() >= 1);
assertTrue(s.getJobWcet() <= maxJobWcet); assertTrue(s.getJobWcet() <= maxJobWcet);
assertTrue(s.getNumberOfJobs() >= 1); assertTrue(s.getNumberOfJobs() >= 1);
assertTrue(s.getNumberOfJobs() <= 6); assertTrue(s.getNumberOfJobs() <= 10);
} }
assertTrue(t.getSegments().size() >= 1); assertTrue(t.getSegments().size() >= 1);
assertTrue(t.getSegments().size() <= 5); assertTrue(t.getSegments().size() <= 7);
} }
} }
......
package mvd.jester.priority;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.HashSet;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Task;
import mvd.jester.simulator.internals.maiabertogna.TaskContext;
/**
* TestEarliestDeadlineFirst
*/
public class TestEarliestDeadlineFirst {
@Test
@DisplayName("Test if priority manager returns the correct priority.")
public void testPriority() {
EarliestDeadlineFirst edf = new EarliestDeadlineFirst();
Task t1 = new Task(100, new HashSet<>());
Task t2 = new Task(200, new HashSet<>());
TaskContext tc1 = new TaskContext(t1, 0);
TaskContext tc2 = new TaskContext(t2, 0);
assertThrows(RuntimeException.class, () -> edf.compare(t1, t2));
assertTrue(edf.compare(tc1, tc2) < 0);
}
}
package mvd.jester.priority;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.HashSet;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Segment;
import mvd.jester.model.Task;
import mvd.jester.simulator.internals.maiabertogna.TaskContext;
/**
* TestRateMonotonic
*/
public class TestRateMonotonic {
@Test
@DisplayName("Test if priority manager returns the correct priority.")
public void testPriority() {
RateMonotonic rm = new RateMonotonic();
Task t1 = new Task(100, new HashSet<Segment>());
Task t2 = new Task(200, new HashSet<Segment>());
TaskContext tc1 = new TaskContext(t1, 0);
TaskContext tc2 = new TaskContext(t2, 0);
assertTrue(rm.compare(t1, t2) < 0);
assertTrue(rm.compare(tc1, tc2) < 0);
}
}
package mvd.jester.simulator.maiabertogna;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Segment;
import mvd.jester.simulator.internals.ProcessorContext;
import mvd.jester.simulator.internals.TaskContextInterface;
import mvd.jester.simulator.internals.maiabertogna.JobContext;
import mvd.jester.simulator.internals.maiabertogna.SegmentContext;
import mvd.jester.simulator.internals.maiabertogna.TaskContext;
/**
* TestJobContext
*/
public class TestJobContext {
@Test
@DisplayName("Check if execution of Job is updated correctly.")
public void testUpdateExecution() {
for (int run = 0; run < 100; ++run) {
long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
long jobWcet = ThreadLocalRandom.current().nextLong(20, 50);
Segment s = new Segment(jobWcet, numberOfJobs);
SegmentContext sc = mock(SegmentContext.class);
when(sc.getSegment()).thenReturn(s);
TaskContext tc = mock(TaskContext.class);
when(tc.acceptNotification(anyLong())).thenReturn(Optional.of(tc));
JobContext job = new JobContext(tc, sc);
ProcessorContext p = new ProcessorContext(0);
job.setCurrentProcessor(p);
p.setJob(job);
assertTrue(job.checkExecutionTime());
for (int i = 0; i < jobWcet - 1; ++i) {
assertFalse(job.updateExecution(i).isPresent());
}
Optional<TaskContextInterface> otc = job.updateExecution(0);
assertTrue(otc.isPresent());
assertTrue(tc.equals(otc.get()));
assertFalse(p.getJob().isPresent());
assertFalse(job.getCurrentProcessor().isPresent());
assertFalse(job.checkExecutionTime());
}
}
}
package mvd.jester.simulator.maiabertogna;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Segment;
import mvd.jester.simulator.internals.JobContextInterface;
import mvd.jester.simulator.internals.maiabertogna.SegmentContext;
import mvd.jester.simulator.internals.maiabertogna.TaskContext;
import mvd.jester.simulator.internals.ProcessorContext;
/**
* TestSegmentContext
*/
public class TestSegmentContext {
@Test
@DisplayName("Check if segment returns the jobs correctly.")
void testGetNextJob() {
for (int run = 0; run < 100; ++run) {
long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
long jobWcet = ThreadLocalRandom.current().nextLong(20, 50);
Segment s = new Segment(jobWcet, numberOfJobs);
TaskContext tc = mock(TaskContext.class);
when(tc.acceptNotification(jobWcet - 1)).thenReturn(Optional.empty());
SegmentContext sc = new SegmentContext(tc, s);
Optional<JobContextInterface> job = Optional.empty();
for (int i = 0; i < numberOfJobs; ++i) {
job = sc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(i));
}
assertFalse(sc.getNextJob().isPresent());
sc = new SegmentContext(tc, s);
for (int i = 0; i < numberOfJobs; ++i) {
job = sc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(i));
for (int j = 0; j < jobWcet; ++j) {
job.get().updateExecution(i);
}
}
assertFalse(sc.getNextJob().isPresent());
}
}
}
package mvd.jester.simulator.maiabertogna;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Segment;
import mvd.jester.model.Task;
import mvd.jester.simulator.internals.JobContextInterface;
import mvd.jester.simulator.internals.ProcessorContext;
import mvd.jester.simulator.internals.TaskContextInterface;
import mvd.jester.simulator.internals.maiabertogna.TaskContext;
/**
* TestTaskContext
*/
public class TestTaskContext {
@Test
@DisplayName("Check if TaskContext accepts its notification correctly.")
public void testAcceptNotification() {
for (int run = 0; run < 100; ++run) {
long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
long numberOfSegments = ThreadLocalRandom.current().nextLong(3, 10);
ArrayList<Segment> segments = new ArrayList<>();
for (int i = 0; i < numberOfSegments; ++i) {
long numJobs = i % 2 == 0 ? 1 : numberOfJobs;
segments.add(new Segment(10, numJobs));
}
Task t = new Task(100, new LinkedHashSet<>(segments));
TaskContext tc = new TaskContext(t, 0);
for (int i = 0; i < segments.size() - 1; ++i) {
Segment s = segments.get(i);
for (int j = 0; j < s.getNumberOfJobs(); ++j) {
assertFalse(tc.acceptNotification(0).isPresent());
}
}
for (int i = 0; i < segments.get(segments.size() - 1).getNumberOfJobs() - 1; ++i) {
assertFalse(tc.acceptNotification(0).isPresent());
}
Optional<TaskContextInterface> tci = tc.acceptNotification(0);
assertTrue(tci.isPresent());
assertTrue(tci.get().equals(tc));
}
}
@Test
@DisplayName("Check if the next job is returned correctly.")
public void testGetNextJob() {
for (int run = 0; run < 100; ++run) {
// long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
// long numberOfSegments = ThreadLocalRandom.current().nextLong(3, 10);
Set<Segment> segments = new LinkedHashSet<>();
segments.add(new Segment(5, 1));
segments.add(new Segment(10, 10));
segments.add(new Segment(15, 1));
Task t = new Task(100, segments);
TaskContext tc = new TaskContext(t, 0);
Optional<JobContextInterface> job = tc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(0));
assertFalse(tc.getNextJob().isPresent());
tc.acceptNotification(0);
for (int i = 0; i < 9; ++i) {
job = tc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(i));
tc.acceptNotification(0);
}
job = tc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(20));
assertFalse(tc.getNextJob().isPresent());
tc.acceptNotification(0);
assertTrue(tc.getNextJob().isPresent());
}
}
}
package mvd.jester.simulator.schmidmottok;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* TestJobContext
*/
public class TestJobContext {
@Test
@DisplayName("Check if the job is prepared correctly.")
public void testPrepareJob() {
assertTrue(true);
}
}
package mvd.jester.simulator.schmidmottok;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Segment;
import mvd.jester.simulator.internals.JobContextInterface;
import mvd.jester.simulator.internals.ProcessorContext;
import mvd.jester.simulator.internals.schmidmottok.JobContext;
import mvd.jester.simulator.internals.schmidmottok.SegmentContext;
import mvd.jester.simulator.internals.schmidmottok.TaskContext;
import mvd.jester.simulator.internals.schmidmottok.TaskletContext;
/**
* TestSegmentContext
*/
public class TestSegmentContext {
@Test
@DisplayName("Check if segment returns the jobs correctly.")
void testGetNextJob() {
for (int run = 0; run < 100; ++run) {
long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
long jobWcet = ThreadLocalRandom.current().nextLong(20, 50);
Segment s = mock(Segment.class);
when(s.getNumberOfJobs()).thenReturn(numberOfJobs);
when(s.getJobWcet()).thenReturn(jobWcet);
when(s.getNumberOfTasklets()).thenReturn(jobWcet);
when(s.getTaskletWcet()).thenReturn(numberOfJobs);
TaskContext tc = mock(TaskContext.class);
SegmentContext sc = new SegmentContext(tc, s, 4);
Optional<JobContextInterface> job = Optional.empty();
when(tc.acceptNotification(jobWcet - 1)).thenReturn(Optional.empty());
long numJobs = numberOfJobs > 4 ? 4 : numberOfJobs;
for (int i = 0; i < numJobs; ++i) {
job = sc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(i));
}
assertFalse(sc.getNextJob().isPresent());
sc = new SegmentContext(tc, s, 4);
job = sc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(0));
for (int i = 0; i < jobWcet * numberOfJobs; ++i) {
job.get().updateExecution(i);
}
assertFalse(sc.getNextJob().isPresent());
}
}
@Test
@DisplayName("Check if SegmentContext returns the correct amount of tasklets.")
void testGetNextTasklet() {
for (int run = 0; run < 100; ++run) {
long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
long jobWcet = ThreadLocalRandom.current().nextLong(20, 50);
Segment s = mock(Segment.class);
when(s.getNumberOfJobs()).thenReturn(numberOfJobs);
when(s.getJobWcet()).thenReturn(jobWcet);
when(s.getNumberOfTasklets()).thenReturn(jobWcet);
when(s.getTaskletWcet()).thenReturn(numberOfJobs);
TaskContext tc = mock(TaskContext.class);
when(tc.acceptNotification(jobWcet - 1)).thenReturn(Optional.empty());
SegmentContext sc = new SegmentContext(tc, s, 4);
for (int i = 0; i < jobWcet; ++i) {
Optional<TaskletContext> tci = sc.getNextTasklet();
assertTrue(tci.isPresent());
tci.get().setCurrentJob(new JobContext(tc, sc));
}
assertFalse(sc.getNextTasklet().isPresent());
sc = new SegmentContext(tc, s, 4);
for (int i = 0; i < jobWcet; ++i) {
Optional<TaskletContext> tasklet = sc.getNextTasklet();
assertTrue(tasklet.isPresent());
tasklet.get().setCurrentJob(new JobContext(tc, sc));
for (int j = 0; j < numberOfJobs; ++j) {
tasklet.get().updateExecution(j);
}
}
assertFalse(sc.getNextTasklet().isPresent());
}
}
}
package mvd.jester.simulator.schmidmottok;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Segment;
import mvd.jester.model.Task;
import mvd.jester.simulator.internals.JobContextInterface;
import mvd.jester.simulator.internals.ProcessorContext;
import mvd.jester.simulator.internals.TaskContextInterface;
import mvd.jester.simulator.internals.schmidmottok.TaskContext;
/**
* TestTaskContext
*/
public class TestTaskContext {
@Test
@DisplayName("Check if TaskContext accepts its notification correctly.")
public void testAcceptNotification() {
for (int run = 0; run < 100; ++run) {
long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
long numberOfSegments = ThreadLocalRandom.current().nextLong(3, 10);
ArrayList<Segment> segments = new ArrayList<>();
for (int i = 0; i < numberOfSegments; ++i) {
long numJobs = i % 2 == 0 ? 1 : numberOfJobs;
segments.add(new Segment(10, numJobs));
}
Task t = new Task(100, new LinkedHashSet<>(segments));
TaskContext tc = new TaskContext(t, 1, 0);
for (int i = 0; i < segments.size() - 1; ++i) {
Segment s = segments.get(i);
for (int j = 0; j < s.getNumberOfTasklets(); ++j) {
assertFalse(tc.acceptNotification(0).isPresent());
}
}
for (int i = 0; i < segments.get(segments.size() - 1).getNumberOfTasklets() - 1; ++i) {
assertFalse(tc.acceptNotification(0).isPresent());
}
Optional<TaskContextInterface> tci = tc.acceptNotification(0);
assertTrue(tci.isPresent());
assertTrue(tci.get().equals(tc));
}
}
@Test
@DisplayName("Check if the next job is returned correctly.")
public void testGetNextJob() {
for (int run = 0; run < 100; ++run) {
// long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
// long numberOfSegments = ThreadLocalRandom.current().nextLong(3, 10);
Set<Segment> segments = new LinkedHashSet<>();
segments.add(new Segment(5, 1));
segments.add(new Segment(10, 10));
segments.add(new Segment(15, 1));
Task t = new Task(100, segments);
TaskContext tc = new TaskContext(t, 1, 0);
Optional<JobContextInterface> job = tc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(0));
assertFalse(tc.getNextJob().isPresent());
tc.acceptNotification(0);
for (int i = 0; i < 9; ++i) {
job = tc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(i));
tc.acceptNotification(0);
}
job = tc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(20));
assertFalse(tc.getNextJob().isPresent());
tc.acceptNotification(0);
assertTrue(tc.getNextJob().isPresent());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment