From cf3cf083a184b86de6ea3975d6e3de9f9d949bf3 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Wed, 14 Aug 2013 13:37:50 +0800 Subject: [PATCH] add the support about muti-script testplan --- .../master/api/TestPlanController.java | 23 +++---- .../api/model/RunTestPlanResultModel.java | 18 +++--- .../master/api/model/RunningScriptModel.java | 43 +++++++++++++ .../api/model/ScriptIdRequireLoadModel.java | 29 --------- .../master/api/model/TestPlanModel.java | 6 +- .../bench4q/master/service/AgentService.java | 28 +++++++++ .../master/testPlan/TestPlanRunner.java | 63 +++++++++++-------- .../bench4q/master/test/TestPlanTester.java | 24 +++---- 8 files changed, 142 insertions(+), 92 deletions(-) create mode 100644 src/main/java/org/bench4q/master/api/model/RunningScriptModel.java delete mode 100644 src/main/java/org/bench4q/master/api/model/ScriptIdRequireLoadModel.java diff --git a/src/main/java/org/bench4q/master/api/TestPlanController.java b/src/main/java/org/bench4q/master/api/TestPlanController.java index ab067d79..33dbfdcd 100644 --- a/src/main/java/org/bench4q/master/api/TestPlanController.java +++ b/src/main/java/org/bench4q/master/api/TestPlanController.java @@ -1,12 +1,13 @@ package org.bench4q.master.api; +import java.util.ArrayList; import java.util.List; import java.util.UUID; import org.bench4q.master.api.model.AgentBriefModel; +import org.bench4q.master.api.model.RunningScriptModel; import org.bench4q.master.api.model.TestPlanModel; import org.bench4q.master.api.model.RunTestPlanResultModel; -import org.bench4q.master.communication.agent.RunScenarioResultModel; import org.bench4q.master.service.ScriptService; import org.bench4q.master.service.UserService; import org.bench4q.master.testPlan.TestPlanRunner; @@ -43,23 +44,25 @@ public class TestPlanController extends BaseController { public RunTestPlanResultModel runTestPlanWithScriptId( @RequestParam int scriptId, @RequestParam int requireLoad) { // TODO:add the scriptId-requireLoad to taskList, and return a UUID + List list = new ArrayList(); if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { return buildTestPlanResponseModel(false, "you don't have the powe to RunTestPlan", null); } - return buildTestPlanResponseModel(true, "", - this.testPlanRunner._runTestPlanWithScriptId(scriptId, - requireLoad)); + list.add(this.testPlanRunner._runTestPlanWithScriptId(scriptId, + requireLoad)); + + return buildTestPlanResponseModel(true, "", list); } public static RunTestPlanResultModel buildTestPlanResponseModel( - boolean success, String failCause, List list) { + boolean success, String failCause, List list) { RunTestPlanResultModel resultModel = new RunTestPlanResultModel(); resultModel.setSuccess(success); resultModel.setFailCause(failCause); - resultModel.setRunScenarioResultModels(list); + resultModel.setRunningScriptModels(list); return resultModel; } @@ -79,20 +82,18 @@ public class TestPlanController extends BaseController { if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { return _buildTestPlanResponseModel(false, - "you don't have this power", null, null); + "you don't have this power", null); } return _buildTestPlanResponseModel(true, "", - this.testPlanRunner.runTestPlanWithModel(testPlanModel), null); + this.testPlanRunner.runTestPlanWithModel(testPlanModel)); } private RunTestPlanResultModel _buildTestPlanResponseModel(boolean success, - String failCause, UUID testPlanId, - List runScenarioResultModels) { + String failCause, UUID testPlanId) { RunTestPlanResultModel result = new RunTestPlanResultModel(); result.setSuccess(success); result.setFailCause(failCause); result.setTestPlanId(testPlanId); - result.setRunScenarioResultModels(runScenarioResultModels); return result; } diff --git a/src/main/java/org/bench4q/master/api/model/RunTestPlanResultModel.java b/src/main/java/org/bench4q/master/api/model/RunTestPlanResultModel.java index ae4464f8..e58a4766 100644 --- a/src/main/java/org/bench4q/master/api/model/RunTestPlanResultModel.java +++ b/src/main/java/org/bench4q/master/api/model/RunTestPlanResultModel.java @@ -7,14 +7,12 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; -import org.bench4q.master.communication.agent.RunScenarioResultModel; - @XmlRootElement(name = "RunTestPlanResultModel") public class RunTestPlanResultModel { private boolean success; private String failCause; private UUID testPlanId; - private List runScenarioResultModels; + private List runningScriptContextModels; @XmlElement(name = "testPlanId") public UUID getTestPlanId() { @@ -43,15 +41,15 @@ public class RunTestPlanResultModel { this.failCause = failCause; } - @XmlElementWrapper(name = "ScenarioRuningMessage") - @XmlElement(name = "agentRunId") - public List getRunScenarioResultModels() { - return runScenarioResultModels; + @XmlElementWrapper(name = "ContentList") + @XmlElement(name = "RunningScriptContextModel") + public List getRunningScriptContextModels() { + return runningScriptContextModels; } - public void setRunScenarioResultModels( - List runScenarioResultModels) { - this.runScenarioResultModels = runScenarioResultModels; + public void setRunningScriptModels( + List runningScriptContextModels) { + this.runningScriptContextModels = runningScriptContextModels; } } diff --git a/src/main/java/org/bench4q/master/api/model/RunningScriptModel.java b/src/main/java/org/bench4q/master/api/model/RunningScriptModel.java new file mode 100644 index 00000000..65079251 --- /dev/null +++ b/src/main/java/org/bench4q/master/api/model/RunningScriptModel.java @@ -0,0 +1,43 @@ +package org.bench4q.master.api.model; + +import java.util.List; +import java.util.UUID; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "RunningScripModel") +public class RunningScriptModel { + private int scriptId; + private int requireLoad; + private List agentRunIds; + + @XmlElement + public int getScriptId() { + return scriptId; + } + + public void setScriptId(int scriptId) { + this.scriptId = scriptId; + } + + @XmlElement + public int getRequireLoad() { + return requireLoad; + } + + public void setRequireLoad(int requireLoad) { + this.requireLoad = requireLoad; + } + + @XmlElementWrapper(name = "AgentRunIdList") + @XmlElement(name = "agentRunId") + public List getAgentRunIds() { + return agentRunIds; + } + + public void setAgentRunIds(List agentRunIds) { + this.agentRunIds = agentRunIds; + } +} diff --git a/src/main/java/org/bench4q/master/api/model/ScriptIdRequireLoadModel.java b/src/main/java/org/bench4q/master/api/model/ScriptIdRequireLoadModel.java deleted file mode 100644 index bb81a7da..00000000 --- a/src/main/java/org/bench4q/master/api/model/ScriptIdRequireLoadModel.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.bench4q.master.api.model; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement(name = "ScriptIdRequireLoad") -public class ScriptIdRequireLoadModel { - private int scriptId; - private int requireLoad; - - @XmlElement - public int getScriptId() { - return scriptId; - } - - public void setScriptId(int scriptId) { - this.scriptId = scriptId; - } - - @XmlElement - public int getRequireLoad() { - return requireLoad; - } - - public void setRequireLoad(int requireLoad) { - this.requireLoad = requireLoad; - } - -} diff --git a/src/main/java/org/bench4q/master/api/model/TestPlanModel.java b/src/main/java/org/bench4q/master/api/model/TestPlanModel.java index 557f451d..33b94620 100644 --- a/src/main/java/org/bench4q/master/api/model/TestPlanModel.java +++ b/src/main/java/org/bench4q/master/api/model/TestPlanModel.java @@ -10,16 +10,16 @@ import org.springframework.stereotype.Component; @Component @XmlRootElement(name = "testPlanModel") public class TestPlanModel { - private List scriptIdRequireLoads; + private List scriptIdRequireLoads; @XmlElementWrapper(name = "scriptIdRequireLoads") @XmlElement - public List getScriptIdRequireLoads() { + public List getScriptIdRequireLoads() { return scriptIdRequireLoads; } public void setScriptIdRequireLoads( - List scriptIdRequireLoads) { + List scriptIdRequireLoads) { this.scriptIdRequireLoads = scriptIdRequireLoads; } diff --git a/src/main/java/org/bench4q/master/service/AgentService.java b/src/main/java/org/bench4q/master/service/AgentService.java index 1512000a..c1347a87 100644 --- a/src/main/java/org/bench4q/master/service/AgentService.java +++ b/src/main/java/org/bench4q/master/service/AgentService.java @@ -1,5 +1,6 @@ package org.bench4q.master.service; +import java.util.Iterator; import java.util.List; import org.bench4q.master.entity.db.Agent; import org.bench4q.master.helper.SessionHelper; @@ -167,4 +168,31 @@ public class AgentService { } } } + + public void resetAgents() { + Session session = this.getSessionHelper().openSession(); + Transaction transaction = sessionHelper.openSession() + .beginTransaction(); + try { + @SuppressWarnings("unchecked") + List agents = session.createCriteria(Agent.class).list(); + if (agents.size() == 0) { + return; + } + Iterator iterator = agents.iterator(); + Agent agent; + while (iterator.hasNext()) { + agent = iterator.next(); + agent.setRemainLoad(agent.getMaxLoad()); + } + transaction.commit(); + } catch (Exception e) { + e.printStackTrace(); + transaction.rollback(); + } finally { + if (session != null) { + session.close(); + } + } + } } diff --git a/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java b/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java index 649e85cc..20169051 100644 --- a/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java +++ b/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java @@ -18,7 +18,7 @@ import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import org.bench4q.master.api.model.AgentBriefModel; -import org.bench4q.master.api.model.ScriptIdRequireLoadModel; +import org.bench4q.master.api.model.RunningScriptModel; import org.bench4q.master.api.model.TestPlanModel; import org.bench4q.master.communication.AgentStateService; import org.bench4q.master.communication.HttpRequester; @@ -118,14 +118,14 @@ public class TestPlanRunner implements Runnable { } } - public List runTestWithRunScenarioModel( - RunScenarioModel runScenarioModel, int requireLoad) + public RunningScriptModel runTestWithRunScenarioModel( + RunScenarioModel runScenarioModel, int requireLoad, int scriptId) throws JAXBException { // TODO: is it needed to be synchronized, but i think it is; Iterator iterator; Agent agent; - List resulList = new ArrayList(); RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel(); + List runIdList = new ArrayList(); int min; // TODO:i think this should be done by HA and Ballancer, now i just do // the ballance in the testPlanRunner by monopolized @@ -136,11 +136,9 @@ public class TestPlanRunner implements Runnable { if (!_isAlive(agent)) { continue; } - if (_isMonopolized(agent)) { continue; } - min = _getMin(requireLoad, agent.getRemainLoad()); runScenarioModel.setPoolSize(min); @@ -148,21 +146,26 @@ public class TestPlanRunner implements Runnable { this.getAgentService().getLoadFromAgent(agent.getHostName(), min); runScenarioResultModel = this.sendScriptContentToAgent( agent.getHostName(), agent.getPort(), - this.makeRunScenarioModelToString(runScenarioModel)); - - HostNameAndLoad hostLoad = new HostNameAndLoad(); - hostLoad.setHostName(agent.getHostName()); - hostLoad.setLoad(min); - - this.runningAgentInfo.addToRunIdHostLoadMap( - runScenarioResultModel.getRunId(), hostLoad); + this.marshalRunScenarioModelToString(runScenarioModel)); this.runningAgentInfo.addToRunIdHostLoadMap(runScenarioResultModel .getRunId(), new HostNameAndLoad(agent.getHostName(), min)); - resulList.add(runScenarioResultModel); + runIdList.add(runScenarioResultModel.getRunId()); requireLoad -= min; } - return resulList; + if (requireLoad > 0) { + return null; + } + return _buildRunningScriptModel(runIdList, scriptId); + + } + + private RunningScriptModel _buildRunningScriptModel(List agentRunIds, + int scriptId) { + RunningScriptModel result = new RunningScriptModel(); + result.setAgentRunIds(agentRunIds); + result.setScriptId(scriptId); + return result; } private boolean _isAlive(Agent agent) { @@ -179,8 +182,8 @@ public class TestPlanRunner implements Runnable { : remainLoadByStart; } - public String makeRunScenarioModelToString(RunScenarioModel runScenarioModel) - throws JAXBException { + public String marshalRunScenarioModelToString( + RunScenarioModel runScenarioModel) throws JAXBException { Marshaller marshaller; marshaller = JAXBContext.newInstance(runScenarioModel.getClass()) .createMarshaller(); @@ -295,18 +298,18 @@ public class TestPlanRunner implements Runnable { } private void doRunTestPlan(TestPlanModel testPlanModel) { - - Iterator iterator = testPlanModel + List result = new ArrayList(); + Iterator iterator = testPlanModel .getScriptIdRequireLoads().iterator(); while (iterator.hasNext()) { - ScriptIdRequireLoadModel sModel = iterator.next(); - this._runTestPlanWithScriptId(sModel.getScriptId(), - sModel.getRequireLoad()); + RunningScriptModel sModel = iterator.next(); + result.add(this._runTestPlanWithScriptId(sModel.getScriptId(), + sModel.getRequireLoad())); } } - public List _runTestPlanWithScriptId(int scriptId, + public RunningScriptModel _runTestPlanWithScriptId(int scriptId, int requireLoad) { RunScenarioModel runScenarioModel = this.scriptService .getRunSceniroModelByScriptId(scriptId); @@ -314,9 +317,15 @@ public class TestPlanRunner implements Runnable { return null; } try { - List list = runTestWithRunScenarioModel( - runScenarioModel, requireLoad); - return list; + RunningScriptModel result = this.runTestWithRunScenarioModel( + runScenarioModel, requireLoad, scriptId); + if (result == null) { + result = new RunningScriptModel(); + result.setAgentRunIds(null); + result.setScriptId(-1); + } + result.setRequireLoad(requireLoad); + return result; } catch (JAXBException e) { e.printStackTrace(); return null; diff --git a/src/test/java/org/bench4q/master/test/TestPlanTester.java b/src/test/java/org/bench4q/master/test/TestPlanTester.java index 9d53f7c3..11624e1e 100644 --- a/src/test/java/org/bench4q/master/test/TestPlanTester.java +++ b/src/test/java/org/bench4q/master/test/TestPlanTester.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.UUID; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -16,13 +17,12 @@ import javax.xml.bind.Unmarshaller; import org.bench4q.master.api.TestPlanController; import org.bench4q.master.api.model.AgentBriefModel; -import org.bench4q.master.api.model.ScriptIdRequireLoadModel; +import org.bench4q.master.api.model.RunningScriptModel; import org.bench4q.master.api.model.TestPlanModel; import org.bench4q.master.api.model.RunTestPlanResultModel; import org.bench4q.master.communication.HttpRequester.HttpResponse; import org.bench4q.master.communication.agent.ParameterModel; import org.bench4q.master.communication.agent.RunScenarioModel; -import org.bench4q.master.communication.agent.RunScenarioResultModel; import org.bench4q.master.communication.agent.UsePluginModel; import org.bench4q.master.communication.agent.UserBehaviorModel; import org.bench4q.master.service.AgentService; @@ -62,8 +62,8 @@ public class TestPlanTester extends TestBase { } private void _createATestPlan() { - List list = new ArrayList(); - ScriptIdRequireLoadModel model = new ScriptIdRequireLoadModel(); + List list = new ArrayList(); + RunningScriptModel model = new RunningScriptModel(); model.setScriptId(8); model.setRequireLoad(200); list.add(model); @@ -150,16 +150,16 @@ public class TestPlanTester extends TestBase { marshaller.marshal(runScenarioModel, stringWriter); // String content = stringWriter.toString(); - List map = testPlanController - .getTestPlanRunner().runTestWithRunScenarioModel( - runScenarioModel, runScenarioModel.getTotalCount()); - Iterator iterator = map.iterator(); - RunScenarioResultModel runResultModel; + RunningScriptModel map = testPlanController.getTestPlanRunner() + .runTestWithRunScenarioModel(runScenarioModel, + runScenarioModel.getTotalCount(), 1); + Iterator iterator = map.getAgentRunIds().iterator(); + UUID agentRunId; while (iterator.hasNext()) { - runResultModel = iterator.next(); + agentRunId = iterator.next(); AgentBriefModel agentBriefModel = testPlanController - .getTestPlanRunner().getBriefStatusFromAgent( - runResultModel.getRunId()); + .getTestPlanRunner().getBriefStatusFromAgent(agentRunId); + System.out.println(agentBriefModel.getTestStatusModel() .getAverageResponseTime()); }