Commit 6fe892dd authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add ScenarioAppender to testSimulationRunner

parent 483aaa41
package org.vadere.simulator.control; package org.vadere.simulator.control;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.simulator.projects.Scenario;
import org.vadere.simulator.projects.ScenarioRun;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
...@@ -8,8 +13,11 @@ import java.util.concurrent.ThreadFactory; ...@@ -8,8 +13,11 @@ import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class ScenarioExecutorService extends ThreadPoolExecutor { public class ScenarioExecutorService extends ThreadPoolExecutor {
private static Logger log = LogManager.getLogger(ScenarioExecutorService.class);
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ScenarioExecutorService(nThreads, nThreads, return new ScenarioExecutorService(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS, 0L, TimeUnit.MILLISECONDS,
...@@ -38,10 +46,10 @@ public class ScenarioExecutorService extends ThreadPoolExecutor { ...@@ -38,10 +46,10 @@ public class ScenarioExecutorService extends ThreadPoolExecutor {
protected void afterExecute(Runnable r, Throwable t) { protected void afterExecute(Runnable r, Throwable t) {
if (t != null) { if (t != null) {
// todo Throwable von scenario run abolen // todo Throwable von scenario run abolen
System.out.println("Error in execution found..."); log.info("Error in execution found...");
t.printStackTrace(); t.printStackTrace();
} else { } else {
System.out.println("Execution worked"); log.info("Execution worked");
} }
super.afterExecute(r, t); super.afterExecute(r, t);
} }
......
package org.vadere.simulator.projects.migration; package org.vadere.simulator.projects.migration;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.Test; import org.junit.Test;
import org.vadere.simulator.control.ScenarioExecutorService; import org.vadere.simulator.control.ScenarioExecutorService;
import org.vadere.simulator.entrypoints.ScenarioFactory; import org.vadere.simulator.entrypoints.ScenarioFactory;
...@@ -18,12 +20,13 @@ import java.util.Comparator; ...@@ -18,12 +20,13 @@ import java.util.Comparator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class TestSimulationRunner { public class TestSimulationRunner {
private static Logger logger = LogManager.getLogger(TestSimulationRunner.class);
private final String outputPath = "target/TestRuns/output"; private final String outputPath = "target/TestRuns/output";
...@@ -31,31 +34,31 @@ public class TestSimulationRunner { ...@@ -31,31 +34,31 @@ public class TestSimulationRunner {
public void RunAllSimulations() { public void RunAllSimulations() {
// remove/clean dir from target folder and create new empty tree (used for output) // remove/clean dir from target folder and create new empty tree (used for output)
try { try {
if (Paths.get(outputPath).toFile().exists()) {
Files.walk(Paths.get(outputPath).getParent()) Files.walk(Paths.get(outputPath).getParent())
.sorted(Comparator.reverseOrder()) .sorted(Comparator.reverseOrder())
.map(Path::toFile) .map(Path::toFile)
.forEach(File::deleteOnExit); .forEach(File::delete);
}
Files.createDirectories(Paths.get(outputPath)); Files.createDirectories(Paths.get(outputPath));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
ThreadFactory threadFactory = new ThreadFactory() { ThreadFactory threadFactory = r -> {
@Override
public Thread newThread(Runnable r) {
// System.out.println("creating pooled thread");
final Thread thread = new Thread(r); final Thread thread = new Thread(r);
//todo LogManager neuer Appender
// thread.setUncaughtExceptionHandler(exceptionHandler);
return thread; return thread;
}
}; };
ExecutorService threadPool = ScenarioExecutorService.newFixedThreadPool(4, threadFactory); ExecutorService threadPool = ScenarioExecutorService.newFixedThreadPool(4, threadFactory);
ScenarioAppender scenarioAppender = new ScenarioAppender(Paths.get(outputPath));
LogManager.getRootLogger().addAppender(scenarioAppender);
List<Path> scenarios = getScenarioFiles(Paths.get("../VadereModelTests/TestOSM")); List<Path> scenarios = getScenarioFiles(Paths.get("../VadereModelTests/TestOSM"));
List<Path> chicken = scenarios.stream().filter(f -> f.getFileName().endsWith("basic_1_chicken_osm1.scenario")).collect(Collectors.toList());
List<Path> subset = scenarios.subList(0, 3); List<Path> subset = scenarios.subList(0, 3);
subset.addAll(chicken);
System.out.println(subset.size()); System.out.println(subset.size());
for (Path scenarioPath : subset) { for (Path scenarioPath : subset) {
Scenario s = null; Scenario s = null;
...@@ -65,18 +68,20 @@ public class TestSimulationRunner { ...@@ -65,18 +68,20 @@ public class TestSimulationRunner {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("#####Start scenario: " + scenarioPath.getFileName()); logger.info("#####Start scenario: " + scenarioPath.getFileName());
threadPool.submit(new ScenarioRun(s, outputPath, (listener) -> System.out.println("done"))); threadPool.submit(new ScenarioRun(s, outputPath, (listener) -> System.out.println("done")));
} }
threadPool.shutdown(); threadPool.shutdown();
if (!threadPool.isTerminated()) { if (!threadPool.isTerminated()) {
try { try {
System.out.println("waiting 60 sec...."); logger.info("waiting 60 sec....");
threadPool.awaitTermination(60, TimeUnit.SECONDS); threadPool.awaitTermination(60, TimeUnit.SECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
threadPool.shutdownNow(); threadPool.shutdownNow();
} finally {
LogManager.getRootLogger().removeAppender(scenarioAppender);
} }
} }
......
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 to comment