diff --git a/src/main/java/org/bench4q/master/api/TestPlanController.java b/src/main/java/org/bench4q/master/api/TestPlanController.java index b0639c18..f1fa1c59 100644 --- a/src/main/java/org/bench4q/master/api/TestPlanController.java +++ b/src/main/java/org/bench4q/master/api/TestPlanController.java @@ -2,18 +2,17 @@ package org.bench4q.master.api; import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.StringWriter; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.UUID; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; -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.TestPlanModel; import org.bench4q.master.api.model.TestPlanResponseModel; import org.bench4q.master.communication.AgentStateService; import org.bench4q.master.communication.HttpRequester; @@ -21,9 +20,9 @@ import org.bench4q.master.communication.HttpRequester.HttpResponse; import org.bench4q.master.communication.agent.RunScenarioModel; import org.bench4q.master.communication.agent.RunScenarioResultModel; import org.bench4q.master.communication.agent.TestBriefStatusModel; -import org.bench4q.master.entity.db.Agent; import org.bench4q.master.service.AgentService; import org.bench4q.master.service.ScriptService; +import org.bench4q.master.testPlan.TestPlanRunner; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @@ -38,6 +37,7 @@ public class TestPlanController extends BaseController { private AgentService agentPoolService = new AgentService(); private AgentStateService agentStateService = new AgentStateService(); private HttpRequester httpRequester = new HttpRequester(); + private TestPlanRunner testPlanRunner; public ScriptService getScriptService() { return scriptService; @@ -75,11 +75,20 @@ public class TestPlanController extends BaseController { this.httpRequester = httpRequester; } + public TestPlanRunner getTestPlanRunner() { + return testPlanRunner; + } + + @Autowired + public void setTestPlanRunner(TestPlanRunner testPlanRunner) { + this.testPlanRunner = testPlanRunner; + } + @RequestMapping(value = "/runTestPlanWithScriptId", method = RequestMethod.GET) @ResponseBody public TestPlanResponseModel runTestPlanWithScriptId( @RequestParam int scriptId, @RequestParam int requireLoad) { - // TODO:let it can give me a script list + // TODO:add the scriptId-requireLoad to taskList, and return a UUID RunScenarioModel runScenarioModel = this.getScriptService() .getRunSceniroModelByScriptId(scriptId); if (runScenarioModel == null) { @@ -87,7 +96,7 @@ public class TestPlanController extends BaseController { "RunScenarioModel's content is null", null); } try { - List list = this + List list = this.getTestPlanRunner() .runTestWithRunScenarioModel(runScenarioModel, requireLoad); return setTestPlanResponseModel(true, "success", list); } catch (JAXBException e) { @@ -102,107 +111,6 @@ public class TestPlanController extends BaseController { } - public List runTestWithRunScenarioModel( - RunScenarioModel runScenarioModel, int requireLoad) - 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(); - - // TODO:i think this should be done by HA and Ballancer, now i just do - // the ballance - // by solo - for (iterator = this.getAgentPoolService().loadAgentPoolFromDB() - .iterator(); iterator.hasNext() && requireLoad > 0;) { - if (requireLoad <= 0) { - break; - } - agent = iterator.next(); - - if (!this.getAgentStateService().askLiving(agent.getHostName(), - agent.getPort())) { - continue; - } - - if (agent.getRemainLoad() < agent.getMaxLoad()) { - continue; - } - - int remainLoadByStart = agent.getRemainLoad(); - - if (remainLoadByStart >= requireLoad) { - - runScenarioModel.setPoolSize(requireLoad); - runScenarioModel.setTotalCount(requireLoad); - this.getAgentPoolService().getLoadFromAgent( - agent.getHostName(), requireLoad); - - runScenarioResultModel = this.sendScriptContentToAgent( - agent.getHostName(), agent.getPort(), - this.marShallRunScenarioModel(runScenarioModel)); - - // TODO:for now, i just think it's stable - resulList.add(runScenarioResultModel); - requireLoad = 0; - } else { - runScenarioModel.setPoolSize(agent.getRemainLoad()); - runScenarioModel.setPoolSize(agent.getRemainLoad()); - this.getAgentPoolService().getLoadFromAgent( - agent.getHostName(), agent.getRemainLoad()); - runScenarioResultModel = this.sendScriptContentToAgent( - agent.getHostName(), agent.getPort(), - this.marShallRunScenarioModel(runScenarioModel)); - - resulList.add(runScenarioResultModel); - requireLoad -= remainLoadByStart; - } - } - return resulList; - } - - public String marShallRunScenarioModel(RunScenarioModel runScenarioModel) - throws JAXBException { - Marshaller marshaller; - marshaller = JAXBContext.newInstance(runScenarioModel.getClass()) - .createMarshaller(); - StringWriter stringWriter = new StringWriter(); - marshaller.marshal(runScenarioModel, stringWriter); - return stringWriter.toString(); - } - - public RunScenarioResultModel sendScriptContentToAgent( - String hostNameString, int port, String scriptContent) { - RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel(); - try { - HttpResponse httpResponse = this.getHttpRequester().sendPostXml( - hostNameString + ":" + port + "/test/run", scriptContent); - - runScenarioResultModel = extractRunSenarioResultModel(httpResponse - .getContent()); - - return runScenarioResultModel; - } catch (IOException e) { - e.printStackTrace(); - return runScenarioResultModel; - } catch (JAXBException e) { - e.printStackTrace(); - return runScenarioResultModel; - } - } - - private RunScenarioResultModel extractRunSenarioResultModel( - String responseContent) throws JAXBException { - RunScenarioResultModel resultModel = new RunScenarioResultModel(); - - Unmarshaller unmarshaller = JAXBContext.newInstance( - resultModel.getClass()).createUnmarshaller(); - resultModel = (RunScenarioResultModel) unmarshaller - .unmarshal(new ByteArrayInputStream(responseContent.getBytes())); - return resultModel; - } - private TestPlanResponseModel setTestPlanResponseModel(boolean success, String failCause, List list) { TestPlanResponseModel resultModel = new TestPlanResponseModel(); @@ -241,8 +149,7 @@ public class TestPlanController extends BaseController { private void giveBackTheLoad(String hostName, AgentBriefModel result) { // TODO:judge have i back the load already? if (result.isFinish()) { - int backLoad = result.getTestStatusModel().getTotalCount() - / result.getTestStatusModel().getScenarioBehaviorCount(); + int backLoad = result.getTestStatusModel().getTotalCount() / 2; this.getAgentPoolService().backLoadToAgent(hostName, backLoad); } } @@ -265,4 +172,19 @@ public class TestPlanController extends BaseController { .getTotalCount()); return result; } + + @RequestMapping(value = "/runTestPlanWithTestPlanModel", method = RequestMethod.GET) + @ResponseBody + public TestPlanResponseModel runTestPlanWithTestPlanModel( + @RequestParam TestPlanModel testPlanModel) { + TestPlanResponseModel result = new TestPlanResponseModel(); + Iterator iterator = testPlanModel + .getScriptIdRequireLoads().iterator(); + while (iterator.hasNext()) { + ScriptIdRequireLoadModel sModel = iterator.next(); + this.runTestPlanWithScriptId(sModel.getScriptId(), + sModel.getRequireLoad()); + } + return result; + } } diff --git a/src/main/java/org/bench4q/master/api/model/ScriptIdRequireLoadModel.java b/src/main/java/org/bench4q/master/api/model/ScriptIdRequireLoadModel.java new file mode 100644 index 00000000..bb81a7da --- /dev/null +++ b/src/main/java/org/bench4q/master/api/model/ScriptIdRequireLoadModel.java @@ -0,0 +1,29 @@ +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 new file mode 100644 index 00000000..112989de --- /dev/null +++ b/src/main/java/org/bench4q/master/api/model/TestPlanModel.java @@ -0,0 +1,22 @@ +package org.bench4q.master.api.model; + +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; + +public class TestPlanModel { + private List scriptIdRequireLoads; + + @XmlElementWrapper(name = "scriptIdRequireLoads") + @XmlElement + public List getScriptIdRequireLoads() { + return scriptIdRequireLoads; + } + + public void setScriptIdRequireLoads( + List scriptIdRequireLoads) { + this.scriptIdRequireLoads = scriptIdRequireLoads; + } + +} diff --git a/src/main/java/org/bench4q/master/api/model/TestResponseChildModel.java b/src/main/java/org/bench4q/master/api/model/TestResponseChildModel.java new file mode 100644 index 00000000..d6e67d9c --- /dev/null +++ b/src/main/java/org/bench4q/master/api/model/TestResponseChildModel.java @@ -0,0 +1,32 @@ +package org.bench4q.master.api.model; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import org.bench4q.master.communication.agent.RunScenarioResultModel; + +@XmlRootElement(name = "scriptID-hostName-runId") +public class TestResponseChildModel { + private RunScenarioResultModel runScenarioResultModel; + private int ScriptId; + + @XmlElement + public RunScenarioResultModel getRunScenarioResultModel() { + return runScenarioResultModel; + } + + public void setRunScenarioResultModel( + RunScenarioResultModel runScenarioResultModel) { + this.runScenarioResultModel = runScenarioResultModel; + } + + @XmlElement + public int getScriptId() { + return ScriptId; + } + + public void setScriptId(int scriptId) { + ScriptId = scriptId; + } + +} diff --git a/src/main/java/org/bench4q/master/testPlan/HighAvailable.java b/src/main/java/org/bench4q/master/testPlan/HighAvailable.java new file mode 100644 index 00000000..91662a3e --- /dev/null +++ b/src/main/java/org/bench4q/master/testPlan/HighAvailable.java @@ -0,0 +1,5 @@ +package org.bench4q.master.testPlan; + +public class HighAvailable { + +} diff --git a/src/main/java/org/bench4q/master/testPlan/LoadBallancerForAgent.java b/src/main/java/org/bench4q/master/testPlan/LoadBallancerForAgent.java new file mode 100644 index 00000000..d68a07f4 --- /dev/null +++ b/src/main/java/org/bench4q/master/testPlan/LoadBallancerForAgent.java @@ -0,0 +1,5 @@ +package org.bench4q.master.testPlan; + +public class LoadBallancerForAgent { + +} diff --git a/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java b/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java new file mode 100644 index 00000000..20769564 --- /dev/null +++ b/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java @@ -0,0 +1,159 @@ +package org.bench4q.master.testPlan; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.bench4q.master.communication.AgentStateService; +import org.bench4q.master.communication.HttpRequester; +import org.bench4q.master.communication.HttpRequester.HttpResponse; +import org.bench4q.master.communication.agent.RunScenarioModel; +import org.bench4q.master.communication.agent.RunScenarioResultModel; +import org.bench4q.master.entity.db.Agent; +import org.bench4q.master.service.AgentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +@Component +public class TestPlanRunner { + + private AgentService agentService; + private AgentStateService agentStateService; + private HttpRequester httpRequester; + + public AgentService getAgentService() { + return agentService; + } + + @Autowired + public void setAgentService(AgentService agentService) { + this.agentService = agentService; + } + + public AgentStateService getAgentStateService() { + return agentStateService; + } + + @Autowired + public void setAgentStateService(AgentStateService agentStateService) { + this.agentStateService = agentStateService; + } + + public HttpRequester getHttpRequester() { + return httpRequester; + } + + @Autowired + public void setHttpRequester(HttpRequester httpRequester) { + this.httpRequester = httpRequester; + } + + public List runTestWithRunScenarioModel( + RunScenarioModel runScenarioModel, int requireLoad) + 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(); + + // TODO:i think this should be done by HA and Ballancer, now i just do + // the ballance + // by solo + for (iterator = this.getAgentService().loadAgentPoolFromDB().iterator(); iterator + .hasNext() && requireLoad > 0;) { + if (requireLoad <= 0) { + break; + } + agent = iterator.next(); + + if (!this.getAgentStateService().askLiving(agent.getHostName(), + agent.getPort())) { + continue; + } + + if (agent.getRemainLoad() < agent.getMaxLoad()) { + continue; + } + + int remainLoadByStart = agent.getRemainLoad(); + + if (remainLoadByStart >= requireLoad) { + + runScenarioModel.setPoolSize(requireLoad); + runScenarioModel.setTotalCount(requireLoad); + this.getAgentService().getLoadFromAgent(agent.getHostName(), + requireLoad); + + runScenarioResultModel = this.sendScriptContentToAgent( + agent.getHostName(), agent.getPort(), + this.makeRunScenarioModelString(runScenarioModel)); + + // TODO:for now, i just think it's stable + resulList.add(runScenarioResultModel); + requireLoad = 0; + } else { + runScenarioModel.setPoolSize(agent.getRemainLoad()); + runScenarioModel.setPoolSize(agent.getRemainLoad()); + this.getAgentService().getLoadFromAgent(agent.getHostName(), + agent.getRemainLoad()); + runScenarioResultModel = this.sendScriptContentToAgent( + agent.getHostName(), agent.getPort(), + this.makeRunScenarioModelString(runScenarioModel)); + + resulList.add(runScenarioResultModel); + requireLoad -= remainLoadByStart; + } + } + return resulList; + } + + public String makeRunScenarioModelString(RunScenarioModel runScenarioModel) + throws JAXBException { + Marshaller marshaller; + marshaller = JAXBContext.newInstance(runScenarioModel.getClass()) + .createMarshaller(); + StringWriter stringWriter = new StringWriter(); + marshaller.marshal(runScenarioModel, stringWriter); + return stringWriter.toString(); + } + + public RunScenarioResultModel sendScriptContentToAgent( + String hostNameString, int port, String scriptContent) { + RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel(); + try { + HttpResponse httpResponse = this.getHttpRequester().sendPostXml( + hostNameString + ":" + port + "/test/run", scriptContent); + + runScenarioResultModel = extractRunSenarioResultModel(httpResponse + .getContent()); + + return runScenarioResultModel; + } catch (IOException e) { + e.printStackTrace(); + return runScenarioResultModel; + } catch (JAXBException e) { + e.printStackTrace(); + return runScenarioResultModel; + } + } + + private RunScenarioResultModel extractRunSenarioResultModel( + String responseContent) throws JAXBException { + RunScenarioResultModel resultModel = new RunScenarioResultModel(); + + Unmarshaller unmarshaller = JAXBContext.newInstance( + resultModel.getClass()).createUnmarshaller(); + resultModel = (RunScenarioResultModel) unmarshaller + .unmarshal(new ByteArrayInputStream(responseContent.getBytes())); + return resultModel; + } + +} diff --git a/src/test/java/org/bench4q/master/test/TestPlanTester.java b/src/test/java/org/bench4q/master/test/TestPlanTester.java index d36cfd4a..5e05af94 100644 --- a/src/test/java/org/bench4q/master/test/TestPlanTester.java +++ b/src/test/java/org/bench4q/master/test/TestPlanTester.java @@ -79,8 +79,8 @@ public class TestPlanTester { // String content = stringWriter.toString(); List map = testPlanController - .runTestWithRunScenarioModel(runScenarioModel, - runScenarioModel.getTotalCount()); + .getTestPlanRunner().runTestWithRunScenarioModel( + runScenarioModel, runScenarioModel.getTotalCount()); Iterator iterator = map.iterator(); RunScenarioResultModel runResultModel; while (iterator.hasNext()) {