diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java index b17053a5..a847f3fb 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java @@ -123,6 +123,10 @@ public class ScenarioContext implements Observer { * @param requiredLoad */ public void updatePopulation(int requiredLoad) { + if (requiredLoad == 0) { + requiredLoad = 1; + } + System.out.println(requiredLoad); this.getExecutor().setCorePoolSize(requiredLoad); this.getExecutor().setMaximumPoolSize(requiredLoad); } @@ -145,15 +149,20 @@ public class ScenarioContext implements Observer { @Override public void update(Observable o, Object arg) { - Schedule schedule = (Schedule) o; - if (schedule.hasReachEnd()) { - stop(); - } else { - this.updatePopulation((Integer) arg); + try { + Schedule schedule = (Schedule) o; + if (schedule.hasReachEnd()) { + stop(); + } else { + this.updatePopulation((Integer) arg); + } + } catch (Exception e) { + e.printStackTrace(); } } public void stop() { + this.getSchedule().stop(); this.setFinished(true); this.setEndDate(new Date()); this.getExecutor().shutdownNow(); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java index 6e7c92d2..0e1723b0 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java @@ -50,10 +50,12 @@ public class ScenarioEngine implements Observer { } } - public void submitScenario(final UUID runId, final Scenario scenario, final Schedule schedule, final long realStartTime) { + public void submitScenario(final UUID runId, final Scenario scenario, + final Schedule schedule, final long realStartTime) { try { ScenarioContext old = this.getRunningTests().get(runId); - this.getRunningTests().put(runId, old.addScenrio(scenario, schedule, realStartTime)); + this.getRunningTests().put(runId, + old.addScenrio(scenario, schedule, realStartTime)); } catch (Exception e) { e.printStackTrace(); } @@ -73,11 +75,9 @@ public class ScenarioEngine implements Observer { return false; } try { - int currentLoad = scenarioContext - .getSchedule() - .loadFor( - System.currentTimeMillis() - - scenarioContext.getStartDate().getTime()); + int currentLoad = scenarioContext.getSchedule().loadFor( + System.currentTimeMillis() + - scenarioContext.getStartDate().getTime()); scenarioContext.initTasks(currentLoad); new Supervisor(scenarioContext).start(); return true; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Schedule.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Schedule.java index 7aa36432..3b3c195b 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Schedule.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Schedule.java @@ -42,7 +42,6 @@ public class Schedule extends Observable { this.segments = segments; this.beginTime = System.currentTimeMillis(); this.reachEnd = false; - beginSchedul(); } public long getScheduleRange() { @@ -50,7 +49,7 @@ public class Schedule extends Observable { .getTime() - this.getSegments().get(0).start.getTime(); } - private void beginSchedul() { + public void begin() { timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { @@ -59,6 +58,7 @@ public class Schedule extends Observable { if (segment == null) { // exceed the range of execute, should let the context stop // the test + System.out.println("Execution Over!"); notifyObservers(0); return; } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Supervisor.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Supervisor.java index 9654c13d..bed640aa 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Supervisor.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Supervisor.java @@ -5,7 +5,6 @@ import java.util.Observable; import java.util.Timer; import java.util.TimerTask; - public class Supervisor extends Observable { private final ScenarioContext context; private final Timer timer = new Timer(); @@ -13,20 +12,21 @@ public class Supervisor extends Observable { public Supervisor(final ScenarioContext context) { this.context = context; } - - void start(){ + + void start() { + this.context.getSchedule().begin(); long time = context.getSchedule().getScheduleRange() + context.getStartDate().getTime(); this.timer.schedule(new TimerTask() { @Override public void run() { - context.getSchedule().stop(); + stop(); + notifyObservers(); } }, new Date(time)); } - - void stop(){ - this.context.getSchedule().stop(); + + void stop() { this.context.stop(); this.timer.cancel(); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java index 07e9b53a..c41cdb89 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java @@ -195,7 +195,7 @@ public class TestWithScriptFile extends TestBase { files.add(new File("Scripts" + System.getProperty("file.separator") + "testJD.xml")); HttpResponse httpResponse1 = this.getHttpRequester().sendPost( - url + "/bookTest/100", null, null); + url + "/bookTest/20", null, null); RunScenarioResultModel bookResponse = (RunScenarioResultModel) MarshalHelper .tryUnmarshal(RunScenarioResultModel.class, httpResponse1.getContent()); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java index 7ee75c38..64f4cbff 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java @@ -34,41 +34,15 @@ public class Test_ScenarioContext extends TestBase { assertEquals(20, context.getExecutor().getMaximumPoolSize()); } - // @Test - // public void testForParameterization() throws IOException { - // RunScenarioModel model = Test_HBasePlugin.buildScenario(10); - // String param_name = "param1"; - // UUID testId = UUID.randomUUID(); - // - // createParamFileAndFillParamContent(param_name, testId); - // - // model.getDefinedParameters().add( - // new DefinedParameterModel(param_name, "Para_Table", - // PickOrder.SEQUENTIAL.name(), - // UpdateStrategy.EACH_OCCURRENCE.name())); - // - // ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( - // testId, Scenario.scenarioBuilderWithCompile(model), 10); - // - // VUser vUser = new VUser(scenarioContext, 1); - // String firstValue = vUser.getParam(param_name); - // String secondValue = vUser.getParam(param_name); - // assertNotEquals(firstValue, secondValue); - // } - - // private void createParamFileAndFillParamContent(String param_name, - // UUID testId) throws IOException { - // File parmFile = new File( - // new Para_Table(testId, PickOrder.SEQUENTIAL, - // UpdateStrategy.EACH_ITERATION, param_name, -1) - // .getParamFileFullPath(param_name)); - // TestHelper.createFileIfNotExist(parmFile); - // FileUtils.writeStringToFile(parmFile, - // "row1;10;11~row2;20;21~row3,30,31~"); - // } - @Test public void test_AddScenario() { + ScenarioContext scenarioContext = getScenarioWithScenarioAndSchedule(); + assertNotNull(scenarioContext.getScenario()); + assertEquals(scenarioContext.getStartDate().getTime() + 60 * 1000, + scenarioContext.getEndDate().getTime()); + } + + private ScenarioContext getScenarioWithScenarioAndSchedule() { ScenarioContext scenarioContext = ScenarioContext .buildScenarioContextWithoutScenario(UUID.randomUUID(), 100, pluginManager); @@ -77,8 +51,21 @@ public class Test_ScenarioContext extends TestBase { Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith(new LinkedList())), Schedule.build(buildScheduleModel()), new Date() .getTime()); - assertNotNull(scenarioContext.getScenario()); - assertEquals(scenarioContext.getStartDate().getTime() + 60 * 1000, - scenarioContext.getEndDate().getTime()); + return scenarioContext; + } + + @Test + public void test_initTasksWithZero() { + ScenarioContext context = getScenarioWithScenarioAndSchedule(); + context.initTasks(0); + assertEquals(1, context.getExecutor().getCorePoolSize()); + assertEquals(1, context.getExecutor().getMaximumPoolSize()); + } + + @Test(expected = IllegalArgumentException.class) + public void test_updatePopulationWithZero() { + ScenarioContext context = getScenarioWithScenarioAndSchedule(); + context.initTasks(10); + context.updatePopulation(0); } }