refactor code and remove some bug

refactor code and remove some bug
This commit is contained in:
coderfengyun 2014-09-02 16:30:01 +08:00
parent 01a0e56f6e
commit 2a69bc8884
6 changed files with 54 additions and 58 deletions

View File

@ -123,6 +123,10 @@ public class ScenarioContext implements Observer {
* @param requiredLoad * @param requiredLoad
*/ */
public void updatePopulation(int requiredLoad) { public void updatePopulation(int requiredLoad) {
if (requiredLoad == 0) {
requiredLoad = 1;
}
System.out.println(requiredLoad);
this.getExecutor().setCorePoolSize(requiredLoad); this.getExecutor().setCorePoolSize(requiredLoad);
this.getExecutor().setMaximumPoolSize(requiredLoad); this.getExecutor().setMaximumPoolSize(requiredLoad);
} }
@ -145,15 +149,20 @@ public class ScenarioContext implements Observer {
@Override @Override
public void update(Observable o, Object arg) { public void update(Observable o, Object arg) {
Schedule schedule = (Schedule) o; try {
if (schedule.hasReachEnd()) { Schedule schedule = (Schedule) o;
stop(); if (schedule.hasReachEnd()) {
} else { stop();
this.updatePopulation((Integer) arg); } else {
this.updatePopulation((Integer) arg);
}
} catch (Exception e) {
e.printStackTrace();
} }
} }
public void stop() { public void stop() {
this.getSchedule().stop();
this.setFinished(true); this.setFinished(true);
this.setEndDate(new Date()); this.setEndDate(new Date());
this.getExecutor().shutdownNow(); this.getExecutor().shutdownNow();

View File

@ -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 { try {
ScenarioContext old = this.getRunningTests().get(runId); 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -73,11 +75,9 @@ public class ScenarioEngine implements Observer {
return false; return false;
} }
try { try {
int currentLoad = scenarioContext int currentLoad = scenarioContext.getSchedule().loadFor(
.getSchedule() System.currentTimeMillis()
.loadFor( - scenarioContext.getStartDate().getTime());
System.currentTimeMillis()
- scenarioContext.getStartDate().getTime());
scenarioContext.initTasks(currentLoad); scenarioContext.initTasks(currentLoad);
new Supervisor(scenarioContext).start(); new Supervisor(scenarioContext).start();
return true; return true;

View File

@ -42,7 +42,6 @@ public class Schedule extends Observable {
this.segments = segments; this.segments = segments;
this.beginTime = System.currentTimeMillis(); this.beginTime = System.currentTimeMillis();
this.reachEnd = false; this.reachEnd = false;
beginSchedul();
} }
public long getScheduleRange() { public long getScheduleRange() {
@ -50,7 +49,7 @@ public class Schedule extends Observable {
.getTime() - this.getSegments().get(0).start.getTime(); .getTime() - this.getSegments().get(0).start.getTime();
} }
private void beginSchedul() { public void begin() {
timer.scheduleAtFixedRate(new TimerTask() { timer.scheduleAtFixedRate(new TimerTask() {
@Override @Override
public void run() { public void run() {
@ -59,6 +58,7 @@ public class Schedule extends Observable {
if (segment == null) { if (segment == null) {
// exceed the range of execute, should let the context stop // exceed the range of execute, should let the context stop
// the test // the test
System.out.println("Execution Over!");
notifyObservers(0); notifyObservers(0);
return; return;
} }

View File

@ -5,7 +5,6 @@ import java.util.Observable;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
public class Supervisor extends Observable { public class Supervisor extends Observable {
private final ScenarioContext context; private final ScenarioContext context;
private final Timer timer = new Timer(); private final Timer timer = new Timer();
@ -13,20 +12,21 @@ public class Supervisor extends Observable {
public Supervisor(final ScenarioContext context) { public Supervisor(final ScenarioContext context) {
this.context = context; this.context = context;
} }
void start(){ void start() {
this.context.getSchedule().begin();
long time = context.getSchedule().getScheduleRange() long time = context.getSchedule().getScheduleRange()
+ context.getStartDate().getTime(); + context.getStartDate().getTime();
this.timer.schedule(new TimerTask() { this.timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
context.getSchedule().stop(); stop();
notifyObservers();
} }
}, new Date(time)); }, new Date(time));
} }
void stop(){ void stop() {
this.context.getSchedule().stop();
this.context.stop(); this.context.stop();
this.timer.cancel(); this.timer.cancel();
} }

View File

@ -195,7 +195,7 @@ public class TestWithScriptFile extends TestBase {
files.add(new File("Scripts" + System.getProperty("file.separator") files.add(new File("Scripts" + System.getProperty("file.separator")
+ "testJD.xml")); + "testJD.xml"));
HttpResponse httpResponse1 = this.getHttpRequester().sendPost( HttpResponse httpResponse1 = this.getHttpRequester().sendPost(
url + "/bookTest/100", null, null); url + "/bookTest/20", null, null);
RunScenarioResultModel bookResponse = (RunScenarioResultModel) MarshalHelper RunScenarioResultModel bookResponse = (RunScenarioResultModel) MarshalHelper
.tryUnmarshal(RunScenarioResultModel.class, .tryUnmarshal(RunScenarioResultModel.class,
httpResponse1.getContent()); httpResponse1.getContent());

View File

@ -34,41 +34,15 @@ public class Test_ScenarioContext extends TestBase {
assertEquals(20, context.getExecutor().getMaximumPoolSize()); 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 @Test
public void test_AddScenario() { 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 ScenarioContext scenarioContext = ScenarioContext
.buildScenarioContextWithoutScenario(UUID.randomUUID(), 100, .buildScenarioContextWithoutScenario(UUID.randomUUID(), 100,
pluginManager); pluginManager);
@ -77,8 +51,21 @@ public class Test_ScenarioContext extends TestBase {
Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith(new LinkedList<UsePluginModel>())), Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith(new LinkedList<UsePluginModel>())),
Schedule.build(buildScheduleModel()), new Date() Schedule.build(buildScheduleModel()), new Date()
.getTime()); .getTime());
assertNotNull(scenarioContext.getScenario()); return scenarioContext;
assertEquals(scenarioContext.getStartDate().getTime() + 60 * 1000, }
scenarioContext.getEndDate().getTime());
@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);
} }
} }