From f4ce7c581511478cbf1dbc8dcfc1b34c1be46d70 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Tue, 13 Aug 2013 17:42:22 +0800 Subject: [PATCH] add testPlanContext, and do it in a way of taskList, it's a MileStone to complete --- .../master/api/TestPlanController.java | 68 ++++++----- .../master/api/model/TestPlanModel.java | 3 + .../api/model/TestPlanResponseModel.java | 13 ++- .../api/model/TestResponseChildModel.java | 37 ------ .../communication/AgentStateService.java | 2 +- .../master/communication/HttpRequester.java | 11 +- .../agent/RunScenarioResultModel.java | 10 -- ...gentContext.java => RunningAgentInfo.java} | 2 +- .../master/testPlan/TestPlanContext.java | 30 +++++ .../master/testPlan/TestPlanRunner.java | 109 ++++++++++++++---- .../master/test/AgentPoolControllerTest.java | 9 +- 11 files changed, 181 insertions(+), 113 deletions(-) delete mode 100644 src/main/java/org/bench4q/master/api/model/TestResponseChildModel.java rename src/main/java/org/bench4q/master/testPlan/{AgentContext.java => RunningAgentInfo.java} (92%) create mode 100644 src/main/java/org/bench4q/master/testPlan/TestPlanContext.java diff --git a/src/main/java/org/bench4q/master/api/TestPlanController.java b/src/main/java/org/bench4q/master/api/TestPlanController.java index e35ad656..f6e6e8f0 100644 --- a/src/main/java/org/bench4q/master/api/TestPlanController.java +++ b/src/main/java/org/bench4q/master/api/TestPlanController.java @@ -1,15 +1,11 @@ package org.bench4q.master.api; -import java.util.Iterator; import java.util.List; import java.util.UUID; -import javax.xml.bind.JAXBException; 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.agent.RunScenarioModel; import org.bench4q.master.communication.agent.RunScenarioResultModel; import org.bench4q.master.service.ScriptService; import org.bench4q.master.service.UserService; @@ -25,7 +21,7 @@ import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/testPlan") public class TestPlanController extends BaseController { - private ScriptService scriptService = new ScriptService(); + public ScriptService scriptService = new ScriptService(); private TestPlanRunner testPlanRunner; @Autowired @@ -47,28 +43,19 @@ public class TestPlanController extends BaseController { public TestPlanResponseModel runTestPlanWithScriptId( @RequestParam int scriptId, @RequestParam int requireLoad) { // TODO:add the scriptId-requireLoad to taskList, and return a UUID - if (this.checkScope(UserService.NORAML_AUTHENTICATION)) { - return _setTestPlanResponseModel(false, + + if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { + return buildTestPlanResponseModel(false, "you don't have the powe to RunTestPlan", null); } - RunScenarioModel runScenarioModel = this.scriptService - .getRunSceniroModelByScriptId(scriptId); - if (runScenarioModel == null) { - return _setTestPlanResponseModel(false, - "RunScenarioModel's content is null", null); - } - try { - List list = this.testPlanRunner - .runTestWithRunScenarioModel(runScenarioModel, requireLoad); - return _setTestPlanResponseModel(true, "success", list); - } catch (JAXBException e) { - e.printStackTrace(); - return _setTestPlanResponseModel(false, "Run test fails", null); - } + + return buildTestPlanResponseModel(true, "", + this.testPlanRunner._runTestPlanWithScriptId(scriptId, + requireLoad)); } - private TestPlanResponseModel _setTestPlanResponseModel(boolean success, - String failCause, List list) { + public static TestPlanResponseModel buildTestPlanResponseModel( + boolean success, String failCause, List list) { TestPlanResponseModel resultModel = new TestPlanResponseModel(); resultModel.setSuccess(success); resultModel.setFailCause(failCause); @@ -89,20 +76,31 @@ public class TestPlanController extends BaseController { @ResponseBody public TestPlanResponseModel runTestPlanWithTestPlanModel( @RequestBody TestPlanModel testPlanModel) { - TestPlanResponseModel result = new TestPlanResponseModel(); - if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { - result.setSuccess(false); - result.setFailCause("you don't have the power to runTest"); - return result; - } - Iterator iterator = testPlanModel - .getScriptIdRequireLoads().iterator(); - while (iterator.hasNext()) { - ScriptIdRequireLoadModel sModel = iterator.next(); - this.runTestPlanWithScriptId(sModel.getScriptId(), - sModel.getRequireLoad()); + if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { + return _buildTestPlanResponseModel(false, + "you don't have this power", null, null); } + return _buildTestPlanResponseModel(true, "", + this.testPlanRunner.runTestPlanWithModel(testPlanModel), null); + } + + private TestPlanResponseModel _buildTestPlanResponseModel(boolean success, + String failCause, UUID testPlanId, + List runScenarioResultModels) { + TestPlanResponseModel result = new TestPlanResponseModel(); + result.setSuccess(success); + result.setFailCause(failCause); + result.setTestPlanId(testPlanId); + result.setRunScenarioResultModels(runScenarioResultModels); return result; } + + @RequestMapping(value = "/getBriefStatus", method = RequestMethod.POST) + public TestPlanResponseModel getBriefStatusByTestPlanId( + @RequestParam UUID testPlanId) { + // this.testPlanRunner.get + // this.testPlanRunner.getBriefStatusFromAgent(runId) + return null; + } } 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 3471f8fa..557f451d 100644 --- a/src/main/java/org/bench4q/master/api/model/TestPlanModel.java +++ b/src/main/java/org/bench4q/master/api/model/TestPlanModel.java @@ -5,6 +5,9 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; +import org.springframework.stereotype.Component; + +@Component @XmlRootElement(name = "testPlanModel") public class TestPlanModel { private List scriptIdRequireLoads; diff --git a/src/main/java/org/bench4q/master/api/model/TestPlanResponseModel.java b/src/main/java/org/bench4q/master/api/model/TestPlanResponseModel.java index cd1db748..c3287e28 100644 --- a/src/main/java/org/bench4q/master/api/model/TestPlanResponseModel.java +++ b/src/main/java/org/bench4q/master/api/model/TestPlanResponseModel.java @@ -1,6 +1,7 @@ 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; @@ -12,8 +13,18 @@ import org.bench4q.master.communication.agent.RunScenarioResultModel; public class TestPlanResponseModel { private boolean success; private String failCause; + private UUID testPlanId; private List runScenarioResultModels; + @XmlElement(name = "testPlanId") + public UUID getTestPlanId() { + return testPlanId; + } + + public void setTestPlanId(UUID testPlanId) { + this.testPlanId = testPlanId; + } + @XmlElement(name = "success") public boolean isSuccess() { return success; @@ -33,7 +44,7 @@ public class TestPlanResponseModel { } @XmlElementWrapper(name = "ScenarioRuningMessage") - @XmlElement(name = "hostName_runId") + @XmlElement(name = "agentRunId") public List getRunScenarioResultModels() { return runScenarioResultModels; } diff --git a/src/main/java/org/bench4q/master/api/model/TestResponseChildModel.java b/src/main/java/org/bench4q/master/api/model/TestResponseChildModel.java deleted file mode 100644 index 501a48b3..00000000 --- a/src/main/java/org/bench4q/master/api/model/TestResponseChildModel.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.bench4q.master.api.model; - -import java.util.ArrayList; -import java.util.List; - -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 = "scriptID-hostName-runId") -public class TestResponseChildModel { - private List runScenarioResultModels = new ArrayList(); - private int ScriptId; - - @XmlElementWrapper(name = "runId-hostNames") - @XmlElement - public List getRunScenarioResultModel() { - return runScenarioResultModels; - } - - public void setRunScenarioResultModel( - List runScenarioResultModels) { - this.runScenarioResultModels = runScenarioResultModels; - } - - @XmlElement - public int getScriptId() { - return ScriptId; - } - - public void setScriptId(int scriptId) { - ScriptId = scriptId; - } - -} diff --git a/src/main/java/org/bench4q/master/communication/AgentStateService.java b/src/main/java/org/bench4q/master/communication/AgentStateService.java index c5a6eb7a..e18045a7 100644 --- a/src/main/java/org/bench4q/master/communication/AgentStateService.java +++ b/src/main/java/org/bench4q/master/communication/AgentStateService.java @@ -23,7 +23,7 @@ public class AgentStateService { try { HttpResponse httpResponse = this.getHttpRequester().sendGet( hostName + ":" + port + "/", null, null); - if (httpResponse.getCode() == 404) { + if (httpResponse == null || httpResponse.getCode() == 404) { return false; } return true; diff --git a/src/main/java/org/bench4q/master/communication/HttpRequester.java b/src/main/java/org/bench4q/master/communication/HttpRequester.java index 0321bec1..701d8d50 100644 --- a/src/main/java/org/bench4q/master/communication/HttpRequester.java +++ b/src/main/java/org/bench4q/master/communication/HttpRequester.java @@ -4,6 +4,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; @@ -103,11 +104,17 @@ public class HttpRequester { urlConnection.getOutputStream().close(); } - return this.makeContent(urlString, urlConnection); + try { + HttpResponse result = this.makeContent(urlString, urlConnection); + return result; + } catch (ConnectException e) { + return null; + } + } private HttpResponse makeContent(String urlString, - HttpURLConnection urlConnection) { + HttpURLConnection urlConnection) throws ConnectException { HttpResponse httpResponser = new HttpResponse(); try { InputStream in = urlConnection.getInputStream(); diff --git a/src/main/java/org/bench4q/master/communication/agent/RunScenarioResultModel.java b/src/main/java/org/bench4q/master/communication/agent/RunScenarioResultModel.java index 434d8ed4..eaf98e98 100644 --- a/src/main/java/org/bench4q/master/communication/agent/RunScenarioResultModel.java +++ b/src/main/java/org/bench4q/master/communication/agent/RunScenarioResultModel.java @@ -8,7 +8,6 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "runScenarioResult") public class RunScenarioResultModel { private UUID runId; - private String hostName; @XmlElement public UUID getRunId() { @@ -19,13 +18,4 @@ public class RunScenarioResultModel { this.runId = runId; } - @XmlElement - public String getHostName() { - return hostName; - } - - public void setHostName(String hostName) { - this.hostName = hostName; - } - } diff --git a/src/main/java/org/bench4q/master/testPlan/AgentContext.java b/src/main/java/org/bench4q/master/testPlan/RunningAgentInfo.java similarity index 92% rename from src/main/java/org/bench4q/master/testPlan/AgentContext.java rename to src/main/java/org/bench4q/master/testPlan/RunningAgentInfo.java index 6d0f03be..61f8cbd6 100644 --- a/src/main/java/org/bench4q/master/testPlan/AgentContext.java +++ b/src/main/java/org/bench4q/master/testPlan/RunningAgentInfo.java @@ -7,7 +7,7 @@ import java.util.UUID; import org.springframework.stereotype.Component; @Component -public class AgentContext { +public class RunningAgentInfo { private Map runIdHostLoadMap = new HashMap(); public Map getRunIdAgentMap() { diff --git a/src/main/java/org/bench4q/master/testPlan/TestPlanContext.java b/src/main/java/org/bench4q/master/testPlan/TestPlanContext.java new file mode 100644 index 00000000..938fe6e1 --- /dev/null +++ b/src/main/java/org/bench4q/master/testPlan/TestPlanContext.java @@ -0,0 +1,30 @@ +package org.bench4q.master.testPlan; + +import org.bench4q.master.api.model.TestPlanModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class TestPlanContext { + private TestPlanModel testPlanModel; + private RunningAgentInfo runningAgentInfo; + + public TestPlanModel getTestPlanModel() { + return testPlanModel; + } + + @Autowired + public void setTestPlanModel(TestPlanModel testPlanModel) { + this.testPlanModel = testPlanModel; + } + + public RunningAgentInfo getRunningAgentInfo() { + return runningAgentInfo; + } + + @Autowired + public void setRunningAgentInfo(RunningAgentInfo runningAgentInfo) { + this.runningAgentInfo = runningAgentInfo; + } + +} diff --git a/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java b/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java index 6cfe57ab..30c342ab 100644 --- a/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java +++ b/src/main/java/org/bench4q/master/testPlan/TestPlanRunner.java @@ -4,9 +4,13 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -14,6 +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.TestPlanModel; import org.bench4q.master.communication.AgentStateService; import org.bench4q.master.communication.HttpRequester; @@ -23,6 +28,7 @@ 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.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -32,9 +38,11 @@ public class TestPlanRunner implements Runnable { private AgentService agentService; private AgentStateService agentStateService; private HttpRequester httpRequester; - private AgentContext agentContext; + private RunningAgentInfo runningAgentInfo; + public ScriptService scriptService = new ScriptService(); private static final int port = 6565; - private List taskList = new ArrayList(); + private Map _tasks = new HashMap(); + private final int POOL_SIZE = 100; public AgentService getAgentService() { return agentService; @@ -63,41 +71,45 @@ public class TestPlanRunner implements Runnable { this.httpRequester = httpRequester; } - public AgentContext getAgentContext() { - return agentContext; + public RunningAgentInfo getrunningAgentInfo() { + return runningAgentInfo; } @Autowired - public void setAgentContext(AgentContext agentContext) { - this.agentContext = agentContext; + public void setAgentContext(RunningAgentInfo agentContext) { + this.runningAgentInfo = agentContext; } - public void setTaskList(List taskList) { - this.taskList = taskList; + public void setTaskList(Map tasks) { + this._tasks = tasks; } - public void addATask(TestPlanModel task) { - synchronized (this.taskList) { - this.taskList.add(task); - this.taskList.notifyAll(); + public UUID addATask(UUID testPlanId, TestPlanModel task) { + synchronized (this._tasks) { + this._tasks.put(testPlanId, task); + this._tasks.notifyAll(); } - + return testPlanId; } - public void removeATask(int index) { - this.taskList.remove(index); + public void removeATask(UUID testPlanId) { + this._tasks.remove(testPlanId); } public void run() { // TODO Auto-generated method stub + Iterator iterator = this._tasks.keySet().iterator(); + UUID testPlanId; try { while (!Thread.interrupted()) { - synchronized (this.taskList) { - if (this.taskList.size() == 0) { - this.taskList.wait(); + synchronized (this._tasks) { + if (this._tasks.size() == 0) { + this._tasks.wait(); } - while (this.taskList.size() > 0) { - removeATask(0); + while (this._tasks.size() > 0) { + testPlanId = iterator.next(); + removeATask(testPlanId); + } } } @@ -137,16 +149,15 @@ public class TestPlanRunner implements Runnable { runScenarioResultModel = this.sendScriptContentToAgent( agent.getHostName(), agent.getPort(), this.makeRunScenarioModelToString(runScenarioModel)); - runScenarioResultModel.setHostName(agent.getHostName()); HostNameAndLoad hostLoad = new HostNameAndLoad(); hostLoad.setHostName(agent.getHostName()); hostLoad.setLoad(min); - this.agentContext.addToRunIdHostLoadMap( + this.runningAgentInfo.addToRunIdHostLoadMap( runScenarioResultModel.getRunId(), hostLoad); - this.agentContext.addToRunIdHostLoadMap(runScenarioResultModel + this.runningAgentInfo.addToRunIdHostLoadMap(runScenarioResultModel .getRunId(), new HostNameAndLoad(agent.getHostName(), min)); resulList.add(runScenarioResultModel); requireLoad -= min; @@ -214,7 +225,7 @@ public class TestPlanRunner implements Runnable { AgentBriefModel result = new AgentBriefModel(); TestBriefStatusModel briefStatusModel = new TestBriefStatusModel(); try { - HostNameAndLoad hostNameAndLoad = this.agentContext + HostNameAndLoad hostNameAndLoad = this.runningAgentInfo .getRunIdAgentMap().get(runId); if (hostNameAndLoad == null) { return null; @@ -262,4 +273,54 @@ public class TestPlanRunner implements Runnable { return result; } + public UUID runTestPlanWithModel(final TestPlanModel testPlanModel) { + + RunningAgentInfo agentInfo = new RunningAgentInfo(); + ExecutorService executorService = Executors + .newFixedThreadPool(this.POOL_SIZE); + UUID testPlanId = UUID.randomUUID(); + TestPlanContext testPlanContext = new TestPlanContext(); + testPlanContext.setTestPlanModel(testPlanModel); + testPlanContext.setRunningAgentInfo(agentInfo); + + addATask(testPlanId, testPlanModel); + Runnable runnable = new Runnable() { + public void run() { + doRunTestPlan(testPlanModel); + } + }; + executorService.execute(runnable); + executorService.shutdown(); + return testPlanId; + } + + private void doRunTestPlan(TestPlanModel testPlanModel) { + + Iterator iterator = testPlanModel + .getScriptIdRequireLoads().iterator(); + while (iterator.hasNext()) { + ScriptIdRequireLoadModel sModel = iterator.next(); + this._runTestPlanWithScriptId(sModel.getScriptId(), + sModel.getRequireLoad()); + } + + } + + public List _runTestPlanWithScriptId(int scriptId, + int requireLoad) { + RunScenarioModel runScenarioModel = this.scriptService + .getRunSceniroModelByScriptId(scriptId); + if (runScenarioModel == null) { + return null; + } + try { + List list = runTestWithRunScenarioModel( + runScenarioModel, requireLoad); + return list; + } catch (JAXBException e) { + e.printStackTrace(); + return null; + } + } + } diff --git a/src/test/java/org/bench4q/master/test/AgentPoolControllerTest.java b/src/test/java/org/bench4q/master/test/AgentPoolControllerTest.java index 280f4621..435fab1e 100644 --- a/src/test/java/org/bench4q/master/test/AgentPoolControllerTest.java +++ b/src/test/java/org/bench4q/master/test/AgentPoolControllerTest.java @@ -60,12 +60,17 @@ public class AgentPoolControllerTest extends TestBase { System.out.println(agentResponseModel.isSuccess()); } - public void removeAgentToPool() throws IOException { + public void removeAgentToPool() throws IOException, JAXBException { + String accesTocken = this.login(); + Map propertiesMap = new HashMap(); + propertiesMap.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER + + accesTocken); + String urlString = this.URLSTRING + "/removeAgentFromPool"; Map map = new HashMap(); map.put("hostName", HOSTNAME); HttpResponse httpResponse = this.httpRequester.sendGet(urlString, map, - null); + propertiesMap); System.out.println(httpResponse.getContent()); }