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
*/
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();

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 {
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;

View File

@ -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;
}

View File

@ -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();
}

View File

@ -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());

View File

@ -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<UsePluginModel>())),
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);
}
}