From 0cd3074d2583aae43dfa5b29900c36836144554d Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Mon, 24 Feb 2014 21:48:13 +0800 Subject: [PATCH] refactor to remove testplanContext ing --- .../bench4q/master/domain/RunningScript.java | 21 +++++--- .../master/domain/RunningScriptInterface.java | 18 +++++++ ...{RunningAgent.java => RunningAgentDB.java} | 4 +- .../master/domain/entity/TestPlan.java | 9 ++++ .../master/domain/entity/TestPlanScript.java | 49 +++++++++++++++++-- .../master/testplan/LoadDistribute.java | 32 ++++++++---- .../transaction/script/ScriptLoadBase.java | 14 +++--- .../bench4q/master/config/hibernate.cfg.xml | 2 +- 8 files changed, 119 insertions(+), 30 deletions(-) create mode 100644 src/main/java/org/bench4q/master/domain/RunningScriptInterface.java rename src/main/java/org/bench4q/master/domain/entity/{RunningAgent.java => RunningAgentDB.java} (96%) diff --git a/src/main/java/org/bench4q/master/domain/RunningScript.java b/src/main/java/org/bench4q/master/domain/RunningScript.java index 5a9d0fe0..394db7bc 100644 --- a/src/main/java/org/bench4q/master/domain/RunningScript.java +++ b/src/main/java/org/bench4q/master/domain/RunningScript.java @@ -1,9 +1,12 @@ package org.bench4q.master.domain; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Observable; +import java.util.Set; import java.util.Timer; import java.util.TimerTask; import java.util.UUID; @@ -34,14 +37,14 @@ import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel; import org.bench4q.share.models.master.statistics.SampleModel; -public class RunningScript extends Observable { +public class RunningScript extends Observable implements RunningScriptInterface { private int scriptId; private UUID testPlanID; private int requireLoad; private boolean finished; private TestScriptConfig config; private RunScenarioModel scenario; - private List runningAgents = new ArrayList(); + private Set runningAgents = new HashSet(); private Timer timer; private List dataStatisticsList; @@ -91,11 +94,11 @@ public class RunningScript extends Observable { this.scenario = scenario; } - private List getRunningAgents() { + public Set getRunningAgents() { return this.runningAgents; } - private void setRunningAgents(List runningAgents) { + private void setRunningAgents(Set runningAgents) { this.runningAgents = runningAgents; } @@ -161,7 +164,7 @@ public class RunningScript extends Observable { scriptBriefResultModel.setAverageResponseTime(-1); this.setLatestScriptBriefModel(scriptBriefResultModel); - this.setRunningAgents(new ArrayList()); + this.setRunningAgents(new HashSet()); this.setDataStatisticsList(new ArrayList()); this.getDataStatisticsList().add(new ScriptBriefStatistics()); this.getDataStatisticsList().add(new BehaviorsBriefStatistics()); @@ -188,12 +191,12 @@ public class RunningScript extends Observable { this.getScriptId())); } - public static boolean notValidScript(RunningScript input) { + public static boolean notValidScript(RunningScriptInterface input) { return input == null || input.getRunningAgents() == null; } - public List queryRunningAgentsUnModifiable() { - return Collections.unmodifiableList(this.runningAgents); + public Collection queryRunningAgentsUnModifiable() { + return Collections.unmodifiableCollection(this.runningAgents); } private DataStatistics getScriptBriefStatistics() { @@ -311,6 +314,7 @@ public class RunningScript extends Observable { public void doAfterDistributeLoad(List agentsAfterDistribute) { this.getRunningAgents().addAll(agentsAfterDistribute); + doPeriodicBrief(); } public void doPeriodicBrief() { @@ -355,4 +359,5 @@ public class RunningScript extends Observable { this.getAgentService().backLoadToAgent(hostName, runningAgent.getLoadInUse()); } + } diff --git a/src/main/java/org/bench4q/master/domain/RunningScriptInterface.java b/src/main/java/org/bench4q/master/domain/RunningScriptInterface.java new file mode 100644 index 00000000..7d44cacd --- /dev/null +++ b/src/main/java/org/bench4q/master/domain/RunningScriptInterface.java @@ -0,0 +1,18 @@ +package org.bench4q.master.domain; + +import java.util.List; +import java.util.Set; + +import org.bench4q.share.models.agent.RunScenarioModel; + +public interface RunningScriptInterface { + public int getScriptId(); + + public RunScenarioModel getScenario(); + + public void doAfterDistributeLoad(List agentsAfterDistribute); + + public Set getRunningAgents(); + + public int getRequireLoad(); +} diff --git a/src/main/java/org/bench4q/master/domain/entity/RunningAgent.java b/src/main/java/org/bench4q/master/domain/entity/RunningAgentDB.java similarity index 96% rename from src/main/java/org/bench4q/master/domain/entity/RunningAgent.java rename to src/main/java/org/bench4q/master/domain/entity/RunningAgentDB.java index 36a64866..f943c2d8 100644 --- a/src/main/java/org/bench4q/master/domain/entity/RunningAgent.java +++ b/src/main/java/org/bench4q/master/domain/entity/RunningAgentDB.java @@ -13,7 +13,7 @@ import javax.persistence.Table; @Entity @Table(name = "runningAgent") -public class RunningAgent { +public class RunningAgentDB { private int id; private Agent agent; private int loadInUse; @@ -52,7 +52,7 @@ public class RunningAgent { } @ManyToOne - @JoinColumn() + @JoinColumn(name = "scriptId") public Script getScript() { return script; } diff --git a/src/main/java/org/bench4q/master/domain/entity/TestPlan.java b/src/main/java/org/bench4q/master/domain/entity/TestPlan.java index 7539e185..b5375179 100644 --- a/src/main/java/org/bench4q/master/domain/entity/TestPlan.java +++ b/src/main/java/org/bench4q/master/domain/entity/TestPlan.java @@ -136,6 +136,15 @@ public class TestPlan implements IAggregate { } public void run() { + this.getLoadDistribute().generateLoadForTestPlan(this); + } + @Transient + public int getTotalRequireLoad() { + int result = 0; + for (TestPlanScript testPlanScript : this.getTestPlanScripts()) { + result += testPlanScript.getRequireLoad(); + } + return result; } } diff --git a/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java b/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java index 978903b2..98e9e892 100644 --- a/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java +++ b/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java @@ -1,5 +1,6 @@ package org.bench4q.master.domain.entity; +import java.util.List; import java.util.Set; import javax.persistence.CascadeType; @@ -14,16 +15,24 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.Table; +import javax.persistence.Transient; + +import org.apache.log4j.Logger; +import org.bench4q.master.domain.RunningAgent; +import org.bench4q.master.domain.RunningScriptInterface; +import org.bench4q.master.exception.ExceptionLog; +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.agent.RunScenarioModel; @Entity @Table(name = "TestPlanScript") -public class TestPlanScript { +public class TestPlanScript implements RunningScriptInterface { private int id; private Script script; private int requireLoad; private PlanedConfig planedConfig; private TestPlan testPlan; - private Set runningAgents; + private Set runningAgents; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -76,12 +85,44 @@ public class TestPlanScript { } @OneToMany(mappedBy = "testPlanScript", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) - public Set getRunningAgents() { + public Set getRunningAgentsDB() { return runningAgents; } - public void setRunningAgents(Set runningAgents) { + public void setRunningAgentsDB(Set runningAgents) { this.runningAgents = runningAgents; } + @Transient + public int getScriptId() { + return this.getScript().getId(); + } + + @Transient + public RunScenarioModel getScenario() { + try { + return (RunScenarioModel) MarshalHelper + .unmarshal(RunScenarioModel.class, this.getScript() + .getScriptContent()); + } catch (Exception e) { + Logger.getLogger(TestPlanScript.class).error( + ExceptionLog.getStackTrace(e)); + return null; + } + } + + @Transient + public Set getRunningAgents() { + return null; + } + + public void doAfterDistributeLoad( + List agentsAfterDistribute) { + doBrief(); + } + + private void doBrief() { + // TODO Auto-generated method stub + } + } diff --git a/src/main/java/org/bench4q/master/testplan/LoadDistribute.java b/src/main/java/org/bench4q/master/testplan/LoadDistribute.java index 82e28107..46f8e9d9 100644 --- a/src/main/java/org/bench4q/master/testplan/LoadDistribute.java +++ b/src/main/java/org/bench4q/master/testplan/LoadDistribute.java @@ -7,6 +7,7 @@ import org.apache.log4j.Logger; import org.bench4q.master.domain.RunningAgent; import org.bench4q.master.domain.RunningScript; import org.bench4q.master.domain.TestPlanContext; +import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.testplan.highavailable.HighAvailablePool; import org.bench4q.master.testplan.transaction.script.ScriptLoadCommand; import org.springframework.beans.factory.annotation.Autowired; @@ -31,11 +32,11 @@ public class LoadDistribute { public boolean generateLoadForTestPlan(TestPlanContext testPlanContext, UUID testPlanId) { synchronized (this.highAvailableAgentPool.getPool()) { - if (!hasEnoughMaxLoad(testPlanContext)) { + if (!hasEnoughMaxLoad(testPlanContext.getTotalLoad())) { logger.error("There is no enough max load in the pool!"); return false; } - if (!hasEnoughCurrentLoad(testPlanContext)) { + if (!hasEnoughCurrentLoad(testPlanContext.getTotalLoad())) { logger.info("There is no enough current load in the pool!"); return false; } @@ -63,16 +64,29 @@ public class LoadDistribute { private void doAfterDistributeLoadSuccess(RunningScript scriptInput, List runningAgents) { scriptInput.doAfterDistributeLoad(runningAgents); - scriptInput.doPeriodicBrief(); + // scriptInput.doPeriodicBrief(); } - private boolean hasEnoughCurrentLoad(TestPlanContext testPlanContext) { - return this.highAvailableAgentPool.getCurrentAvailableLoad() >= testPlanContext - .getTotalLoad(); + private boolean hasEnoughCurrentLoad(int totalRequireLoad) { + return this.highAvailableAgentPool.getCurrentAvailableLoad() >= totalRequireLoad; } - private boolean hasEnoughMaxLoad(TestPlanContext testPlanContext) { - return this.getHighAvailableAgentPool().getMaxAvailableLoad() >= testPlanContext - .getTotalLoad(); + private boolean hasEnoughMaxLoad(int totalRequireLoad) { + return this.getHighAvailableAgentPool().getMaxAvailableLoad() >= totalRequireLoad; + } + + public boolean generateLoadForTestPlan(TestPlan testPlanInDomain) { + synchronized (this.highAvailableAgentPool.getPool()) { + if (!hasEnoughMaxLoad(testPlanInDomain.getTotalRequireLoad())) { + logger.error("There is no enough max load in the pool!"); + return false; + } + if (!hasEnoughCurrentLoad(testPlanInDomain.getTotalRequireLoad())) { + logger.info("There is no enough current load in the pool!"); + return false; + } + // TODO: add the algorithm of ditribute load + return true; + } } } diff --git a/src/main/java/org/bench4q/master/testplan/transaction/script/ScriptLoadBase.java b/src/main/java/org/bench4q/master/testplan/transaction/script/ScriptLoadBase.java index 1e733658..52309f9c 100644 --- a/src/main/java/org/bench4q/master/testplan/transaction/script/ScriptLoadBase.java +++ b/src/main/java/org/bench4q/master/testplan/transaction/script/ScriptLoadBase.java @@ -9,6 +9,7 @@ import javax.xml.bind.JAXBException; import org.apache.log4j.Logger; import org.bench4q.master.domain.RunningAgent; import org.bench4q.master.domain.RunningScript; +import org.bench4q.master.domain.RunningScriptInterface; import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.helper.ApplicationContextHelper; @@ -23,18 +24,18 @@ import org.bench4q.share.models.agent.RunScenarioResultModel; import org.bench4q.share.models.agent.StopTestModel; public abstract class ScriptLoadBase implements Transaction { - private RunningScript runningScript; + private RunningScriptInterface runningScript; private UUID testPlanRunID; private List agentListThisTime = new ArrayList(); private AgentMessenger runningAgentService; private HighAvailablePool highAvailableAgentPool; private static Logger logger = Logger.getLogger(ScriptLoadCommand.class); - protected RunningScript getRunningScript() { + protected RunningScriptInterface getRunningScript() { return runningScript; } - protected void setRunningScript(RunningScript runningScript) { + protected void setRunningScript(RunningScriptInterface runningScript) { this.runningScript = runningScript; } @@ -79,11 +80,12 @@ public abstract class ScriptLoadBase implements Transaction { ScriptLoadBase.logger = logger; } - public ScriptLoadBase(RunningScript runningScript, UUID testPlanID) { + public ScriptLoadBase(RunningScriptInterface runningScript, UUID testPlanID) { initialize(runningScript, testPlanID); } - private void initialize(RunningScript runningScript, UUID testPlanID) { + private void initialize(RunningScriptInterface runningScript, + UUID testPlanID) { this.setRunningScript(runningScript); this.setTestPlanRunID(testPlanID); this.setRunningAgentService(ApplicationContextHelper.getContext() @@ -110,7 +112,7 @@ public abstract class ScriptLoadBase implements Transaction { + this.getRunningScript().getScriptId()); return null; } - if (RunningScript.notValidScript(this.runningScript)) { + if (RunningScript.notValidScript(this.getRunningScript())) { logger.error("The running agents of runningScriptModel is null, whose scriptId is" + this.getRunningScript().getScriptId()); return null; diff --git a/src/main/resources/org/bench4q/master/config/hibernate.cfg.xml b/src/main/resources/org/bench4q/master/config/hibernate.cfg.xml index 50071b62..4e9d5300 100644 --- a/src/main/resources/org/bench4q/master/config/hibernate.cfg.xml +++ b/src/main/resources/org/bench4q/master/config/hibernate.cfg.xml @@ -25,7 +25,7 @@ - +