Skip to content

Commit

Permalink
Benchmarker test should also cover unintialized, non-feasible results…
Browse files Browse the repository at this point in the history
… (needed to verify initScore refactor)
  • Loading branch information
ge0ffrey committed Jun 8, 2016
1 parent 3ef93d0 commit 2c1421d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 39 deletions.
Expand Up @@ -33,7 +33,7 @@
import org.optaplanner.benchmark.api.PlannerBenchmark;
import org.optaplanner.benchmark.config.blueprint.SolverBenchmarkBluePrintConfig;
import org.optaplanner.benchmark.config.report.BenchmarkReportConfig;
import org.optaplanner.benchmark.impl.PlannerBenchmarkRunner;
import org.optaplanner.benchmark.impl.DefaultPlannerBenchmark;
import org.optaplanner.benchmark.impl.result.PlannerBenchmarkResult;
import org.optaplanner.core.config.SolverConfigContext;
import org.optaplanner.core.config.util.ConfigUtils;
Expand Down Expand Up @@ -195,21 +195,21 @@ public PlannerBenchmark buildPlannerBenchmark(SolverConfigContext solverConfigCo
PlannerBenchmarkResult plannerBenchmarkResult = new PlannerBenchmarkResult();
plannerBenchmarkResult.setName(name);
plannerBenchmarkResult.setAggregation(false);
PlannerBenchmarkRunner plannerBenchmarkRunner = new PlannerBenchmarkRunner(plannerBenchmarkResult, solverConfigContext);
plannerBenchmarkRunner.setBenchmarkDirectory(benchmarkDirectory);
DefaultPlannerBenchmark plannerBenchmark = new DefaultPlannerBenchmark(plannerBenchmarkResult, solverConfigContext);
plannerBenchmark.setBenchmarkDirectory(benchmarkDirectory);
plannerBenchmarkResult.setParallelBenchmarkCount(resolveParallelBenchmarkCount());
plannerBenchmarkResult.setWarmUpTimeMillisSpentLimit(calculateWarmUpTimeMillisSpentLimit());
BenchmarkReportConfig benchmarkReportConfig_ = benchmarkReportConfig == null ? new BenchmarkReportConfig()
: benchmarkReportConfig;
plannerBenchmarkRunner.setBenchmarkReport(benchmarkReportConfig_.buildBenchmarkReport(plannerBenchmarkResult));
plannerBenchmark.setBenchmarkReport(benchmarkReportConfig_.buildBenchmarkReport(plannerBenchmarkResult));

plannerBenchmarkResult.setUnifiedProblemBenchmarkResultList(new ArrayList<>());
plannerBenchmarkResult.setSolverBenchmarkResultList(new ArrayList<>(
effectiveSolverBenchmarkConfigList.size()));
for (SolverBenchmarkConfig solverBenchmarkConfig : effectiveSolverBenchmarkConfigList) {
solverBenchmarkConfig.buildSolverBenchmark(plannerBenchmarkResult);
}
return plannerBenchmarkRunner;
return plannerBenchmark;
}

protected void validate() {
Expand Down
Expand Up @@ -47,7 +47,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PlannerBenchmarkRunner implements PlannerBenchmark {
public class DefaultPlannerBenchmark implements PlannerBenchmark {

protected final transient Logger logger = LoggerFactory.getLogger(getClass());
protected final transient Logger singleBenchmarkRunnerExceptionLogger = LoggerFactory.getLogger(
Expand All @@ -67,11 +67,11 @@ public class PlannerBenchmarkRunner implements PlannerBenchmark {
private long startingSystemTimeMillis = -1L;
private SubSingleBenchmarkRunner firstFailureSubSingleBenchmarkRunner = null;

public PlannerBenchmarkRunner(PlannerBenchmarkResult plannerBenchmarkResult) {
public DefaultPlannerBenchmark(PlannerBenchmarkResult plannerBenchmarkResult) {
this(plannerBenchmarkResult, new SolverConfigContext());
}

public PlannerBenchmarkRunner(PlannerBenchmarkResult plannerBenchmarkResult,
public DefaultPlannerBenchmark(PlannerBenchmarkResult plannerBenchmarkResult,
SolverConfigContext solverConfigContext) {
this.plannerBenchmarkResult = plannerBenchmarkResult;
this.solverConfigContext = solverConfigContext;
Expand Down
Expand Up @@ -18,10 +18,13 @@

import java.io.File;
import java.io.IOException;
import java.util.Collections;

import org.junit.BeforeClass;
import org.junit.Test;
import org.optaplanner.core.api.solver.DivertingClassLoader;
import org.optaplanner.core.config.phase.custom.CustomPhaseConfig;
import org.optaplanner.core.impl.phase.custom.DummyCustomPhaseCommand;

import static org.junit.Assert.*;

Expand All @@ -36,33 +39,38 @@ public static void setup() throws IOException {
}

@Test
public void testdataPlannerBenchmarkConfig() {
public void benchmarkConfig() {
PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromXmlResource(
"org/optaplanner/benchmark/api/testdataPlannerBenchmarkConfig.xml");
PlannerBenchmark plannerBenchmark = plannerBenchmarkFactory.buildPlannerBenchmark();
assertNotNull(plannerBenchmark);
plannerBenchmark.benchmark();
}

@Test
public void testdataPlannerBenchmarkConfigBenchmark() {
@Test(expected = IllegalArgumentException.class)
public void nonExistingBenchmarkConfig() {
PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromXmlResource(
"org/optaplanner/benchmark/api/testdataPlannerBenchmarkConfig.xml");
"org/optaplanner/benchmark/api/nonExistingPlannerBenchmarkConfig.xml");
PlannerBenchmark plannerBenchmark = plannerBenchmarkFactory.buildPlannerBenchmark();
assertNotNull(plannerBenchmark);
plannerBenchmark.benchmark();
}

@Test(expected = IllegalArgumentException.class)
public void nonExistingPlannerBenchmarkConfig() {
@Test
public void uninitializedBenchmarkResult() {
PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromXmlResource(
"org/optaplanner/benchmark/api/nonExistingPlannerBenchmarkConfig.xml");
"org/optaplanner/benchmark/api/testdataPlannerBenchmarkConfig.xml");
CustomPhaseConfig phaseConfig = new CustomPhaseConfig();
phaseConfig.setCustomPhaseCommandClassList(Collections.singletonList(DummyCustomPhaseCommand.class));
plannerBenchmarkFactory.getPlannerBenchmarkConfig().getSolverBenchmarkConfigList().get(0).getSolverConfig()
.setPhaseConfigList(Collections.singletonList(phaseConfig));
PlannerBenchmark plannerBenchmark = plannerBenchmarkFactory.buildPlannerBenchmark();
assertNotNull(plannerBenchmark);
plannerBenchmark.benchmark();
}

@Test
public void testdataPlannerBenchmarkConfigWithClassLoader() {
public void benchmarkConfigWithClassLoader() {
// Mocking loadClass doesn't work well enough, because the className still differs from class.getName()
ClassLoader classLoader = new DivertingClassLoader(getClass().getClassLoader());
PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromXmlResource(
Expand All @@ -73,15 +81,7 @@ public void testdataPlannerBenchmarkConfigWithClassLoader() {
}

@Test
public void testdataPlannerBenchmarkConfigTemplate() {
PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromFreemarkerXmlResource(
"org/optaplanner/benchmark/api/testdataPlannerBenchmarkConfigTemplate.xml.ftl");
PlannerBenchmark plannerBenchmark = plannerBenchmarkFactory.buildPlannerBenchmark();
assertNotNull(plannerBenchmark);
}

@Test
public void testdataPlannerBenchmarkConfigBenchmarkTemplate() {
public void template() {
PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromFreemarkerXmlResource(
"org/optaplanner/benchmark/api/testdataPlannerBenchmarkConfigTemplate.xml.ftl");
PlannerBenchmark plannerBenchmark = plannerBenchmarkFactory.buildPlannerBenchmark();
Expand All @@ -90,7 +90,7 @@ public void testdataPlannerBenchmarkConfigBenchmarkTemplate() {
}

@Test(expected = IllegalArgumentException.class)
public void nonExistingPlannerBenchmarkConfigTemplate() {
public void nonExistingTemplate() {
PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromFreemarkerXmlResource(
"org/optaplanner/benchmark/api/nonExistingPlannerBenchmarkConfigTemplate.xml.ftl");
PlannerBenchmark plannerBenchmark = plannerBenchmarkFactory.buildPlannerBenchmark();
Expand All @@ -99,7 +99,7 @@ public void nonExistingPlannerBenchmarkConfigTemplate() {
}

@Test
public void testdataPlannerBenchmarkConfigTemplateWithClassLoader() {
public void templateWithClassLoader() {
// Mocking loadClass doesn't work well enough, because the className still differs from class.getName()
ClassLoader classLoader = new DivertingClassLoader(getClass().getClassLoader());
PlannerBenchmarkFactory plannerBenchmarkFactory = PlannerBenchmarkFactory.createFromFreemarkerXmlResource(
Expand Down
Expand Up @@ -42,13 +42,13 @@ public void solve() {
TestdataSolution.class, TestdataEntity.class);
Solver<TestdataSolution> solver = solverFactory.buildSolver();


TestdataSolution solution = new TestdataSolution("s1");
solution.setValueList(Arrays.asList(new TestdataValue("v1"), new TestdataValue("v2")));
solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2")));

solution = solver.solve(solution);
assertNotNull(solution);
assertEquals(true, solution.getScore().isSolutionInitialized());
}

@Test
Expand All @@ -57,7 +57,6 @@ public void solveLegacy() {
TestdataLegacySolution.class, TestdataEntity.class);
Solver<TestdataLegacySolution> solver = solverFactory.buildSolver();


TestdataLegacySolution solution = new TestdataLegacySolution("s1");
solution.setValueList(Arrays.asList(new TestdataValue("v1"), new TestdataValue("v2")));
solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2")));
Expand Down
Expand Up @@ -25,7 +25,7 @@
import org.junit.runners.Parameterized;
import org.optaplanner.benchmark.api.PlannerBenchmark;
import org.optaplanner.benchmark.api.PlannerBenchmarkFactory;
import org.optaplanner.benchmark.impl.PlannerBenchmarkRunner;
import org.optaplanner.benchmark.impl.DefaultPlannerBenchmark;
import org.optaplanner.benchmark.impl.result.PlannerBenchmarkResult;
import org.optaplanner.benchmark.impl.result.SolverBenchmarkResult;
import org.optaplanner.core.config.SolverConfigContext;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void buildPlannerBenchmark() {

protected void buildEverySolver(PlannerBenchmark plannerBenchmark) {
SolverConfigContext configContext = new SolverConfigContext();
PlannerBenchmarkResult plannerBenchmarkResult = ((PlannerBenchmarkRunner) plannerBenchmark).getPlannerBenchmarkResult();
PlannerBenchmarkResult plannerBenchmarkResult = ((DefaultPlannerBenchmark) plannerBenchmark).getPlannerBenchmarkResult();
for (SolverBenchmarkResult solverBenchmarkResult : plannerBenchmarkResult.getSolverBenchmarkResultList()) {
SolverConfig solverConfig = solverBenchmarkResult.getSolverConfig();
solverConfig.buildSolver(configContext);
Expand Down
Expand Up @@ -25,7 +25,7 @@
import org.optaplanner.benchmark.api.PlannerBenchmarkFactory;
import org.optaplanner.benchmark.config.statistic.ProblemStatisticType;
import org.optaplanner.benchmark.config.statistic.SingleStatisticType;
import org.optaplanner.benchmark.impl.PlannerBenchmarkRunner;
import org.optaplanner.benchmark.impl.DefaultPlannerBenchmark;
import org.optaplanner.examples.common.app.PlannerBenchmarkTest;

public class NQueensBenchmarkTest extends PlannerBenchmarkTest {
Expand Down Expand Up @@ -70,10 +70,10 @@ public void benchmark64queensSingleThread() {
@Test
public void benchmarkDirectoryNameDuplication() {
PlannerBenchmarkFactory plannerBenchmarkFactory = buildPlannerBenchmarkFactory(new File("data/nqueens/unsolved/4queens.xml"));
PlannerBenchmarkRunner plannerBenchmarkRunner = (PlannerBenchmarkRunner) plannerBenchmarkFactory.buildPlannerBenchmark();
plannerBenchmarkRunner.benchmarkingStarted();
plannerBenchmarkRunner.getPlannerBenchmarkResult().initBenchmarkReportDirectory(plannerBenchmarkFactory.getPlannerBenchmarkConfig().getBenchmarkDirectory());
plannerBenchmarkRunner.getPlannerBenchmarkResult().initBenchmarkReportDirectory(plannerBenchmarkFactory.getPlannerBenchmarkConfig().getBenchmarkDirectory());
DefaultPlannerBenchmark plannerBenchmark = (DefaultPlannerBenchmark) plannerBenchmarkFactory.buildPlannerBenchmark();
plannerBenchmark.benchmarkingStarted();
plannerBenchmark.getPlannerBenchmarkResult().initBenchmarkReportDirectory(plannerBenchmarkFactory.getPlannerBenchmarkConfig().getBenchmarkDirectory());
plannerBenchmark.getPlannerBenchmarkResult().initBenchmarkReportDirectory(plannerBenchmarkFactory.getPlannerBenchmarkConfig().getBenchmarkDirectory());
}

}
2 changes: 1 addition & 1 deletion optaplanner-examples/src/test/resources/logback-test.xml
Expand Up @@ -11,7 +11,7 @@

<logger name="org.optaplanner" level="info"/>
<!-- Don't pollute the test log with a stacktrace -->
<logger name="org.optaplanner.benchmark.impl.PlannerBenchmarkRunner.singleBenchmarkRunnerException" level="error"/>
<logger name="org.optaplanner.benchmark.impl.DefaultPlannerBenchmark.singleBenchmarkRunnerException" level="error"/>

<root level="warn">
<appender-ref ref="consoleAppender" />
Expand Down
Expand Up @@ -38,8 +38,9 @@ public String getOutputFileExtension() {
@Override
public TestdataSolution read(File inputSolutionFile) {
TestdataSolution solution = new TestdataSolution("s1");
solution.setValueList(Arrays.asList(new TestdataValue("v1")));
solution.setEntityList(Arrays.asList(new TestdataEntity("e1")));
solution.setValueList(Arrays.asList(new TestdataValue("v1"), new TestdataValue("v2")));
solution.setEntityList(Arrays.asList(
new TestdataEntity("e1"), new TestdataEntity("e2"), new TestdataEntity("e3")));
return solution;
}

Expand Down

0 comments on commit 2c1421d

Please sign in to comment.