refactor, and move the datastatistics to runningScript

This commit is contained in:
coderfengyun 2013-12-25 13:58:26 +08:00
parent 4483838010
commit df43f8734c
10 changed files with 114 additions and 49 deletions

View File

@ -13,7 +13,6 @@ import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.bench4q.master.api.modelfactory.BusinessModelMapFactory;
import org.bench4q.master.datastatistics.BehaviorsBriefStatistics;
import org.bench4q.master.datastatistics.PageBriefStatistics;
import org.bench4q.master.datastatistics.ScriptBriefStatistics;
import org.bench4q.master.entity.db.TestPlanDB;
import org.bench4q.master.exception.Bench4QException;
@ -188,8 +187,7 @@ public class TestPlanController extends BaseController {
this.getLogger().info("testPlanContext exists!");
List<RunningScriptModel> runningScriptModels = new ArrayList<RunningScriptModel>();
for (RunningScript runningScript : testPlanContext
.getRunningScriptMap().values()) {
for (RunningScript runningScript : testPlanContext.getAllScript()) {
runningScriptModels.add(BusinessModelMapFactory
.toModel(runningScript));
}
@ -227,13 +225,12 @@ public class TestPlanController extends BaseController {
ret.setFinished(true);
return ret;
}
RunningScript runningScript = context.getRunningScriptMap().get(
new Integer(scriptId));
RunningScript runningScript = context.queryRunningScript(scriptId);
if (runningScript == null || runningScript.isFinished()) {
ret.setFinished(true);
return ret;
}
ret = this.getRunningScriptService().getRunningScriptBrief(testPlanId,
ret = this.getRunningScriptService().getScriptBrief(testPlanId,
runningScript, new ScriptBriefStatistics());
ret.setPlanedRunningTime(runningScript.getConfig().getExecuteRange());
return ret;
@ -337,13 +334,11 @@ public class TestPlanController extends BaseController {
ScriptBehaviorsBriefModel ret = new ScriptBehaviorsBriefModel();
TestPlanContext context = this.getTestPlanContainer()
.getRunningTestPlans().get(testPlanRunID);
if (context.isFinish()
|| !context.getRunningScriptMap().containsKey(scriptId)) {
if (context.isFinish() || !context.isContains(scriptId)) {
ret.setFinished(true);
return ret;
}
RunningScript runningScript = context.getRunningScriptMap().get(
scriptId);
RunningScript runningScript = context.queryRunningScript(scriptId);
if (runningScript == null || runningScript.isFinished()) {
ret.setFinished(true);
return ret;
@ -372,9 +367,8 @@ public class TestPlanController extends BaseController {
guardForTestPlan(testPlanRunId);
TestPlanContext context = this.getTestPlanContainer()
.getRunningTestPlans().get(testPlanRunId);
RunningScript runningScript = context.getRunningScriptMap().get(
scriptId);
RunningScript runningScript = context.queryRunningScript(scriptId);
return this.getRunningScriptService().getPageBriefModel(testPlanRunId,
runningScript, pageId, new PageBriefStatistics());
runningScript, pageId);
}
}

View File

@ -66,20 +66,20 @@ public class BusinessModelMapFactory {
}
public static RunningScript toBusiness(RunningScriptModel runningScriptModel) {
RunningScript runningScript = new RunningScript();
runningScript.setConfig(runningScriptModel.getConfig());
runningScript.setFinished(runningScriptModel.isFinished());
runningScript.setRequireLoad(runningScriptModel.getRequireLoad());
RunningScript result = new RunningScript();
result.setConfig(runningScriptModel.getConfig());
result.setFinished(runningScriptModel.isFinished());
result.setRequireLoad(runningScriptModel.getRequireLoad());
List<RunningAgent> runningAgents = new ArrayList<RunningAgent>();
for (RunningAgentModel runningAgentModel : runningScriptModel
.getRunningAgentModels()) {
runningAgents.add(BusinessModelMapFactory
.toBusiness(runningAgentModel));
}
runningScript.setRunningAgents(runningAgents);
runningScript.setScriptId(runningScriptModel.getScriptId());
runningScript.setTestPlanID(runningScriptModel.getTestPlanID());
return runningScript;
result.setRunningAgents(runningAgents);
result.setScriptId(runningScriptModel.getScriptId());
result.setTestPlanID(runningScriptModel.getTestPlanID());
return result;
}
public static RunningScriptModel toModel(RunningScript runningScript) {

View File

@ -6,4 +6,14 @@ public interface DataStatistics {
public void add(DataStatisticsModel dataUnit);
public Object getStatistics();
public class DefaultStatistics implements DataStatistics {
public void add(DataStatisticsModel dataUnit) {
}
public Object getStatistics() {
return null;
}
}
}

View File

@ -38,7 +38,7 @@ public class RunningScriptService {
this.testPlanScriptService = testPlanScriptService;
}
public ScriptBriefResultModel getRunningScriptBrief(UUID testPlanId,
public ScriptBriefResultModel getScriptBrief(UUID testPlanId,
RunningScript runningScript, DataStatistics briefStatistics) {
if (RunningScript.notValidScript(runningScript)) {
return null;
@ -82,19 +82,19 @@ public class RunningScriptService {
}
public ScriptPageBriefModel getPageBriefModel(UUID testPlanID,
RunningScript runningScript, int pageId,
DataStatistics dataStatistics) {
RunningScript runningScript, int pageId) {
if (RunningScript.notValidScript(runningScript)) {
return null;
}
for (RunningAgent runningAgent : runningScript.getRunningAgents()) {
dataStatistics.add(this.getRunningAgentService().pageBrief(
runningAgent.getAgent(), runningAgent.getAgentRunId(),
pageId));
runningScript.getPageBriefStatistics().add(
this.getRunningAgentService().pageBrief(
runningAgent.getAgent(),
runningAgent.getAgentRunId(), pageId));
}
ScriptPageBriefModel result = (ScriptPageBriefModel) dataStatistics
.getStatistics();
ScriptPageBriefModel result = (ScriptPageBriefModel) runningScript
.getPageBriefStatistics().getStatistics();
result.setFinished(runningScript.isFinished());
// TODO:save it in the database
return result;

View File

@ -38,8 +38,7 @@ public class LoadDistribute {
return false;
}
try {
for (RunningScript scriptInput : testPlanContext
.getRunningScriptMap().values()) {
for (RunningScript scriptInput : testPlanContext.getAllScript()) {
Transaction scriptLoadCommand = new ScriptLoadCommand(
scriptInput, testPlanId);
RunningScript scriptOutput = (RunningScript) scriptLoadCommand

View File

@ -43,15 +43,13 @@ public class TestPlanContainer {
public boolean isTestPlanFinished(UUID testPlanId) {
TestPlanContext testPlanContext = this.queryTestPlanContext(testPlanId);
boolean finish = true;
if (testPlanContext == null
|| testPlanContext.getRunningScriptMap().size() == 0) {
if (testPlanContext == null || testPlanContext.getScriptCount() == 0) {
logger.error("testPlanContext is null or scriptMap size is 0, where the testPlanID is"
+ testPlanId);
return true;
}
try {
for (RunningScript runningScript : testPlanContext
.getRunningScriptMap().values()) {
for (RunningScript runningScript : testPlanContext.getAllScript()) {
if (!runningScript.isFinished()) {
finish = false;
}

View File

@ -1,5 +1,7 @@
package org.bench4q.master.testplan;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -21,7 +23,7 @@ public class TestPlanContext {
this.testPlan = testPlan;
}
public Map<Integer, RunningScript> getRunningScriptMap() {
private Map<Integer, RunningScript> getRunningScriptMap() {
return runningScriptMap;
}
@ -69,9 +71,22 @@ public class TestPlanContext {
return this.runningScriptMap.get(new Integer(scriptId));
}
public Collection<RunningScript> getAllScript() {
return Collections.unmodifiableCollection(this.getRunningScriptMap()
.values());
}
public int getScriptCount() {
return this.getRunningScriptMap().size();
}
public boolean isContains(int scriptId) {
return this.getRunningScriptMap().containsKey(scriptId);
}
int getTotalLoad() {
int result = 0;
for (RunningScript runningScript : this.getRunningScriptMap().values()) {
for (RunningScript runningScript : getAllScript()) {
result += runningScript.getRequireLoad();
}
return result;

View File

@ -75,7 +75,7 @@ public class TestPlanEngine implements TaskCompleteCallback,
final TestPlanContext testPlanContext = new TestPlanContext(testPlan,
testPlanId);
if (!this.testPlanService.saveTestPlanToDB(testPlan, user, testPlanId,
testPlanContext.getRunningScriptMap().values())) {
testPlanContext.getAllScript())) {
return null;
}
@ -105,9 +105,7 @@ public class TestPlanEngine implements TaskCompleteCallback,
private boolean schedulePlanConfig(TestPlanContext testPlanContext,
UUID testPlanId) {
for (RunningScript runningScript : testPlanContext
.getRunningScriptMap().values()) {
for (RunningScript runningScript : testPlanContext.getAllScript()) {
scheduleScriptConfig(runningScript, testPlanId);
}
return true;
@ -154,8 +152,7 @@ public class TestPlanEngine implements TaskCompleteCallback,
+ testPlanID.toString() + " is null!");
return;
}
for (RunningScript runningScript : context.getRunningScriptMap()
.values()) {
for (RunningScript runningScript : context.getAllScript()) {
for (RunningAgent runningAgent : runningScript.getRunningAgents()) {
this.getHaPool().getAgentRunBlotters()
.remove(runningAgent.getAgentRunId());

View File

@ -4,15 +4,20 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.bench4q.master.datastatistics.BehaviorsBriefStatistics;
import org.bench4q.master.datastatistics.DataStatistics;
import org.bench4q.master.datastatistics.PageBriefStatistics;
import org.bench4q.master.datastatistics.ScriptBriefStatistics;
import org.bench4q.share.models.master.TestScriptConfig;
public class RunningScript {
private int scriptId;
private int requireLoad;
private TestScriptConfig config;
private boolean finished;
private List<RunningAgent> runningAgents = new ArrayList<RunningAgent>();
private UUID testPlanID;
private int requireLoad;
private boolean finished;
private TestScriptConfig config;
private List<DataStatistics> dataStatisticsList;
private List<RunningAgent> runningAgents = new ArrayList<RunningAgent>();
public int getScriptId() {
return scriptId;
@ -62,6 +67,53 @@ public class RunningScript {
this.testPlanID = testPlanID;
}
private List<DataStatistics> getDataStatisticsList() {
return dataStatisticsList;
}
private void setDataStatisticsList(List<DataStatistics> dataStatisticsList) {
this.dataStatisticsList = dataStatisticsList;
}
public RunningScript() {
this.setRunningAgents(new ArrayList<RunningAgent>());
this.setDataStatisticsList(new ArrayList<DataStatistics>());
this.getDataStatisticsList().add(new ScriptBriefStatistics());
this.getDataStatisticsList().add(new PageBriefStatistics());
this.getDataStatisticsList().add(new BehaviorsBriefStatistics());
}
public DataStatistics getScriptBriefStatistics() {
return getSpecificStaitstics(ScriptBriefStatistics.class);
}
public DataStatistics getPageBriefStatistics() {
return getSpecificStaitstics(PageBriefStatistics.class);
}
public DataStatistics getBehaviorsBriefStatistics() {
return getSpecificStaitstics(BehaviorsBriefStatistics.class);
}
private DataStatistics getSpecificStaitstics(
Class<?> specificStatisticsClass) {
for (DataStatistics dataStatistics : this.getDataStatisticsList()) {
if (specificStatisticsClass.equals(ScriptBriefStatistics.class)
&& (dataStatistics instanceof ScriptBriefStatistics)) {
return dataStatistics;
}
if (specificStatisticsClass.equals(BehaviorsBriefStatistics.class)
&& (dataStatistics instanceof BehaviorsBriefStatistics)) {
return dataStatistics;
}
if (specificStatisticsClass.equals(PageBriefStatistics.class)
&& (dataStatistics instanceof PageBriefStatistics)) {
return dataStatistics;
}
}
return new DataStatistics.DefaultStatistics();
}
public static boolean notValidScript(RunningScript input) {
return input == null || input.getRunningAgents() == null;
}

View File

@ -170,8 +170,8 @@ public class HighAvailablePool extends CurrentLoadSubject {
logger.info("The testPlan has finished or not started, so no need to substitute!");
return;
}
RunningScript runningScript = context.getRunningScriptMap().get(
new Integer(deadAgentBlotter.getRunningAgent().getScriptId()));
RunningScript runningScript = context.queryRunningScript(new Integer(
deadAgentBlotter.getRunningAgent().getScriptId()));
ScriptLoadBase loadCommand = new ScriptLoadSubstitute(runningScript,
deadAgentBlotter);
loadCommand.execute();