add things about controlling the test by timer

This commit is contained in:
coderfengyun 2013-08-21 17:40:33 +08:00
parent c505bc4edf
commit f073d37fe7
6 changed files with 68 additions and 21 deletions

View File

@ -38,7 +38,7 @@ public class HomeController {
getScenarioEngine().getRunningTests()); getScenarioEngine().getRunningTests());
for (UUID key : contexts.keySet()) { for (UUID key : contexts.keySet()) {
ScenarioContext value = contexts.get(key); ScenarioContext value = contexts.get(key);
if (value.getResults().size() == value.getTotalCount()) { if (value.isFinished()) {
serverStatusModel.getFinishedTests().add(key); serverStatusModel.getFinishedTests().add(key);
} else { } else {
serverStatusModel.getRunningTests().add(key); serverStatusModel.getRunningTests().add(key);

View File

@ -231,15 +231,19 @@ public class TestController {
return testBriefStatusModel; return testBriefStatusModel;
} }
@RequestMapping(value = "/stop/{runId}", method = RequestMethod.GET) @RequestMapping(value = "/stop/{runId}", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody @ResponseBody
public StopTestModel stop(@PathVariable UUID runId) { public StopTestModel stop(@PathVariable UUID runId) {
System.out.println("stop method");
ScenarioContext scenarioContext = this.getScenarioEngine() ScenarioContext scenarioContext = this.getScenarioEngine()
.getRunningTests().get(runId); .getRunningTests().get(runId);
if (scenarioContext == null) { if (scenarioContext == null) {
return null; return null;
} }
scenarioContext.getExecutorService().shutdownNow(); scenarioContext.getExecutorService().shutdownNow();
scenarioContext.setFinished(true);
StopTestModel stopTestModel = new StopTestModel(); StopTestModel stopTestModel = new StopTestModel();
stopTestModel.setSuccess(true); stopTestModel.setSuccess(true);
return stopTestModel; return stopTestModel;

View File

@ -13,7 +13,6 @@ public class TestBriefStatusModel {
private int failCount; private int failCount;
private int finishedCount; private int finishedCount;
private int totalCount; private int totalCount;
private int finishedScenarioCount;
private int scenarioBehaviorCount; private int scenarioBehaviorCount;
private double averageResponseTime; private double averageResponseTime;
@ -71,15 +70,6 @@ public class TestBriefStatusModel {
this.totalCount = totalCount; this.totalCount = totalCount;
} }
@XmlElement
public int getFinishedScenarioCount() {
return finishedScenarioCount;
}
public void setFinishedScenarioCount(int finishedScenarioCount) {
this.finishedScenarioCount = finishedScenarioCount;
}
@XmlElement @XmlElement
public double getAverageResponseTime() { public double getAverageResponseTime() {
return averageResponseTime; return averageResponseTime;

View File

@ -0,0 +1,49 @@
package org.bench4q.agent.scenario;
import java.util.List;
import java.util.UUID;
public class RunnableExecutor implements Runnable {
private ScenarioEngine scenarioEngine;
private UUID runId;
private List<BehaviorResult> behaviorResults;
private Scenario scenario;
private void setScenarioEngine(ScenarioEngine scenarioEngine) {
this.scenarioEngine = scenarioEngine;
}
private void setBehaviorResults(List<BehaviorResult> behaviorResults) {
this.behaviorResults = behaviorResults;
}
private void setRunId(UUID runId) {
this.runId = runId;
}
private void setScenario(Scenario scenario) {
this.scenario = scenario;
}
public RunnableExecutor(ScenarioEngine scenarioEngine, UUID runId,
Scenario scenario, List<BehaviorResult> behaviorResults) {
this.setScenarioEngine(scenarioEngine);
this.setRunId(runId);
this.setScenario(scenario);
this.setBehaviorResults(behaviorResults);
}
public void run() {
this.behaviorResults.addAll(this.scenarioEngine
.doRunScenario(this.scenario));
ScenarioContext scenarioContext = this.scenarioEngine.getRunningTests()
.get(this.runId);
if (scenarioContext.getExecutorService().isShutdown()) {
return;
}
scenarioContext.getExecutorService().execute(
new RunnableExecutor(this.scenarioEngine, this.runId,
this.scenario, this.behaviorResults));
}
}

View File

@ -12,6 +12,7 @@ public class ScenarioContext {
private ExecutorService executorService; private ExecutorService executorService;
private Scenario scenario; private Scenario scenario;
private List<BehaviorResult> results; private List<BehaviorResult> results;
private boolean finished;
public int getPoolSize() { public int getPoolSize() {
return poolSize; return poolSize;
@ -68,4 +69,13 @@ public class ScenarioContext {
public void setResults(List<BehaviorResult> results) { public void setResults(List<BehaviorResult> results) {
this.results = results; this.results = results;
} }
public boolean isFinished() {
return finished;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
} }

View File

@ -68,16 +68,10 @@ public class ScenarioEngine {
poolSize, totalCount, executorService, ret); poolSize, totalCount, executorService, ret);
int i; int i;
this.getRunningTests().put(runId, scenarioContext); this.getRunningTests().put(runId, scenarioContext);
Runnable runnable = new Runnable() { Runnable runnable = new RunnableExecutor(this, runId, scenario, ret);
public void run() { for (i = 0; i < poolSize; i++) {
ret.addAll(doRunScenario(scenario));
}
};
for (i = 0; i < totalCount; i++) {
scenarioContext.setFinishedCount(i - 1);
executorService.execute(runnable); executorService.execute(runnable);
} }
executorService.shutdown();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -97,7 +91,7 @@ public class ScenarioEngine {
return scenarioContext; return scenarioContext;
} }
private List<BehaviorResult> doRunScenario(Scenario scenario) { public List<BehaviorResult> doRunScenario(Scenario scenario) {
Map<String, Object> plugins = new HashMap<String, Object>(); Map<String, Object> plugins = new HashMap<String, Object>();
preparePlugins(scenario, plugins); preparePlugins(scenario, plugins);