refactor to remove testplanContext ing

This commit is contained in:
coderfengyun 2014-02-24 21:48:13 +08:00
parent 8acf1f6041
commit 0cd3074d25
8 changed files with 119 additions and 30 deletions

View File

@ -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<RunningAgent> runningAgents = new ArrayList<RunningAgent>();
private Set<RunningAgent> runningAgents = new HashSet<RunningAgent>();
private Timer timer;
private List<DataStatistics> dataStatisticsList;
@ -91,11 +94,11 @@ public class RunningScript extends Observable {
this.scenario = scenario;
}
private List<RunningAgent> getRunningAgents() {
public Set<RunningAgent> getRunningAgents() {
return this.runningAgents;
}
private void setRunningAgents(List<RunningAgent> runningAgents) {
private void setRunningAgents(Set<RunningAgent> runningAgents) {
this.runningAgents = runningAgents;
}
@ -161,7 +164,7 @@ public class RunningScript extends Observable {
scriptBriefResultModel.setAverageResponseTime(-1);
this.setLatestScriptBriefModel(scriptBriefResultModel);
this.setRunningAgents(new ArrayList<RunningAgent>());
this.setRunningAgents(new HashSet<RunningAgent>());
this.setDataStatisticsList(new ArrayList<DataStatistics>());
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<RunningAgent> queryRunningAgentsUnModifiable() {
return Collections.unmodifiableList(this.runningAgents);
public Collection<RunningAgent> queryRunningAgentsUnModifiable() {
return Collections.unmodifiableCollection(this.runningAgents);
}
private DataStatistics getScriptBriefStatistics() {
@ -311,6 +314,7 @@ public class RunningScript extends Observable {
public void doAfterDistributeLoad(List<RunningAgent> agentsAfterDistribute) {
this.getRunningAgents().addAll(agentsAfterDistribute);
doPeriodicBrief();
}
public void doPeriodicBrief() {
@ -355,4 +359,5 @@ public class RunningScript extends Observable {
this.getAgentService().backLoadToAgent(hostName,
runningAgent.getLoadInUse());
}
}

View File

@ -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<RunningAgent> agentsAfterDistribute);
public Set<RunningAgent> getRunningAgents();
public int getRequireLoad();
}

View File

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

View File

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

View File

@ -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<RunningAgent> runningAgents;
private Set<RunningAgentDB> 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<RunningAgent> getRunningAgents() {
public Set<RunningAgentDB> getRunningAgentsDB() {
return runningAgents;
}
public void setRunningAgents(Set<RunningAgent> runningAgents) {
public void setRunningAgentsDB(Set<RunningAgentDB> 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<RunningAgent> getRunningAgents() {
return null;
}
public void doAfterDistributeLoad(
List<org.bench4q.master.domain.RunningAgent> agentsAfterDistribute) {
doBrief();
}
private void doBrief() {
// TODO Auto-generated method stub
}
}

View File

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

View File

@ -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<RunningAgent> agentListThisTime = new ArrayList<RunningAgent>();
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;

View File

@ -25,7 +25,7 @@
<mapping class="org.bench4q.master.domain.entity.Port" />
<mapping class="org.bench4q.master.domain.entity.TestPlan" />
<mapping class="org.bench4q.master.domain.entity.TestPlanScript" />
<mapping class="org.bench4q.master.domain.entity.RunningAgent" />
<mapping class="org.bench4q.master.domain.entity.RunningAgentDB" />
<mapping class="org.bench4q.master.domain.entity.TestPlanScriptResult" />
<mapping class="org.bench4q.master.domain.entity.Monitor" />
<mapping class="org.bench4q.master.domain.entity.MonitorResult" />