add testPlanContext, and do it in a way of taskList, it's a MileStone to

complete
This commit is contained in:
coderfengyun 2013-08-13 17:42:22 +08:00
parent bef43f8952
commit f4ce7c5815
11 changed files with 181 additions and 113 deletions

View File

@ -1,15 +1,11 @@
package org.bench4q.master.api; package org.bench4q.master.api;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.bench4q.master.api.model.AgentBriefModel; 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.TestPlanModel;
import org.bench4q.master.api.model.TestPlanResponseModel; 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.communication.agent.RunScenarioResultModel;
import org.bench4q.master.service.ScriptService; import org.bench4q.master.service.ScriptService;
import org.bench4q.master.service.UserService; import org.bench4q.master.service.UserService;
@ -25,7 +21,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Controller @Controller
@RequestMapping("/testPlan") @RequestMapping("/testPlan")
public class TestPlanController extends BaseController { public class TestPlanController extends BaseController {
private ScriptService scriptService = new ScriptService(); public ScriptService scriptService = new ScriptService();
private TestPlanRunner testPlanRunner; private TestPlanRunner testPlanRunner;
@Autowired @Autowired
@ -47,28 +43,19 @@ public class TestPlanController extends BaseController {
public TestPlanResponseModel runTestPlanWithScriptId( public TestPlanResponseModel runTestPlanWithScriptId(
@RequestParam int scriptId, @RequestParam int requireLoad) { @RequestParam int scriptId, @RequestParam int requireLoad) {
// TODO:add the scriptId-requireLoad to taskList, and return a UUID // 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); "you don't have the powe to RunTestPlan", null);
} }
RunScenarioModel runScenarioModel = this.scriptService
.getRunSceniroModelByScriptId(scriptId); return buildTestPlanResponseModel(true, "",
if (runScenarioModel == null) { this.testPlanRunner._runTestPlanWithScriptId(scriptId,
return _setTestPlanResponseModel(false, requireLoad));
"RunScenarioModel's content is null", null);
}
try {
List<RunScenarioResultModel> list = this.testPlanRunner
.runTestWithRunScenarioModel(runScenarioModel, requireLoad);
return _setTestPlanResponseModel(true, "success", list);
} catch (JAXBException e) {
e.printStackTrace();
return _setTestPlanResponseModel(false, "Run test fails", null);
}
} }
private TestPlanResponseModel _setTestPlanResponseModel(boolean success, public static TestPlanResponseModel buildTestPlanResponseModel(
String failCause, List<RunScenarioResultModel> list) { boolean success, String failCause, List<RunScenarioResultModel> list) {
TestPlanResponseModel resultModel = new TestPlanResponseModel(); TestPlanResponseModel resultModel = new TestPlanResponseModel();
resultModel.setSuccess(success); resultModel.setSuccess(success);
resultModel.setFailCause(failCause); resultModel.setFailCause(failCause);
@ -89,20 +76,31 @@ public class TestPlanController extends BaseController {
@ResponseBody @ResponseBody
public TestPlanResponseModel runTestPlanWithTestPlanModel( public TestPlanResponseModel runTestPlanWithTestPlanModel(
@RequestBody TestPlanModel testPlanModel) { @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<ScriptIdRequireLoadModel> iterator = testPlanModel if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
.getScriptIdRequireLoads().iterator(); return _buildTestPlanResponseModel(false,
while (iterator.hasNext()) { "you don't have this power", null, null);
ScriptIdRequireLoadModel sModel = iterator.next();
this.runTestPlanWithScriptId(sModel.getScriptId(),
sModel.getRequireLoad());
} }
return _buildTestPlanResponseModel(true, "",
this.testPlanRunner.runTestPlanWithModel(testPlanModel), null);
}
private TestPlanResponseModel _buildTestPlanResponseModel(boolean success,
String failCause, UUID testPlanId,
List<RunScenarioResultModel> runScenarioResultModels) {
TestPlanResponseModel result = new TestPlanResponseModel();
result.setSuccess(success);
result.setFailCause(failCause);
result.setTestPlanId(testPlanId);
result.setRunScenarioResultModels(runScenarioResultModels);
return result; return result;
} }
@RequestMapping(value = "/getBriefStatus", method = RequestMethod.POST)
public TestPlanResponseModel getBriefStatusByTestPlanId(
@RequestParam UUID testPlanId) {
// this.testPlanRunner.get
// this.testPlanRunner.getBriefStatusFromAgent(runId)
return null;
}
} }

View File

@ -5,6 +5,9 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.springframework.stereotype.Component;
@Component
@XmlRootElement(name = "testPlanModel") @XmlRootElement(name = "testPlanModel")
public class TestPlanModel { public class TestPlanModel {
private List<ScriptIdRequireLoadModel> scriptIdRequireLoads; private List<ScriptIdRequireLoadModel> scriptIdRequireLoads;

View File

@ -1,6 +1,7 @@
package org.bench4q.master.api.model; package org.bench4q.master.api.model;
import java.util.List; import java.util.List;
import java.util.UUID;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlElementWrapper;
@ -12,8 +13,18 @@ import org.bench4q.master.communication.agent.RunScenarioResultModel;
public class TestPlanResponseModel { public class TestPlanResponseModel {
private boolean success; private boolean success;
private String failCause; private String failCause;
private UUID testPlanId;
private List<RunScenarioResultModel> runScenarioResultModels; private List<RunScenarioResultModel> runScenarioResultModels;
@XmlElement(name = "testPlanId")
public UUID getTestPlanId() {
return testPlanId;
}
public void setTestPlanId(UUID testPlanId) {
this.testPlanId = testPlanId;
}
@XmlElement(name = "success") @XmlElement(name = "success")
public boolean isSuccess() { public boolean isSuccess() {
return success; return success;
@ -33,7 +44,7 @@ public class TestPlanResponseModel {
} }
@XmlElementWrapper(name = "ScenarioRuningMessage") @XmlElementWrapper(name = "ScenarioRuningMessage")
@XmlElement(name = "hostName_runId") @XmlElement(name = "agentRunId")
public List<RunScenarioResultModel> getRunScenarioResultModels() { public List<RunScenarioResultModel> getRunScenarioResultModels() {
return runScenarioResultModels; return runScenarioResultModels;
} }

View File

@ -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<RunScenarioResultModel> runScenarioResultModels = new ArrayList<RunScenarioResultModel>();
private int ScriptId;
@XmlElementWrapper(name = "runId-hostNames")
@XmlElement
public List<RunScenarioResultModel> getRunScenarioResultModel() {
return runScenarioResultModels;
}
public void setRunScenarioResultModel(
List<RunScenarioResultModel> runScenarioResultModels) {
this.runScenarioResultModels = runScenarioResultModels;
}
@XmlElement
public int getScriptId() {
return ScriptId;
}
public void setScriptId(int scriptId) {
ScriptId = scriptId;
}
}

View File

@ -23,7 +23,7 @@ public class AgentStateService {
try { try {
HttpResponse httpResponse = this.getHttpRequester().sendGet( HttpResponse httpResponse = this.getHttpRequester().sendGet(
hostName + ":" + port + "/", null, null); hostName + ":" + port + "/", null, null);
if (httpResponse.getCode() == 404) { if (httpResponse == null || httpResponse.getCode() == 404) {
return false; return false;
} }
return true; return true;

View File

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -103,11 +104,17 @@ public class HttpRequester {
urlConnection.getOutputStream().close(); 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, private HttpResponse makeContent(String urlString,
HttpURLConnection urlConnection) { HttpURLConnection urlConnection) throws ConnectException {
HttpResponse httpResponser = new HttpResponse(); HttpResponse httpResponser = new HttpResponse();
try { try {
InputStream in = urlConnection.getInputStream(); InputStream in = urlConnection.getInputStream();

View File

@ -8,7 +8,6 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "runScenarioResult") @XmlRootElement(name = "runScenarioResult")
public class RunScenarioResultModel { public class RunScenarioResultModel {
private UUID runId; private UUID runId;
private String hostName;
@XmlElement @XmlElement
public UUID getRunId() { public UUID getRunId() {
@ -19,13 +18,4 @@ public class RunScenarioResultModel {
this.runId = runId; this.runId = runId;
} }
@XmlElement
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
} }

View File

@ -7,7 +7,7 @@ import java.util.UUID;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class AgentContext { public class RunningAgentInfo {
private Map<UUID, HostNameAndLoad> runIdHostLoadMap = new HashMap<UUID, HostNameAndLoad>(); private Map<UUID, HostNameAndLoad> runIdHostLoadMap = new HashMap<UUID, HostNameAndLoad>();
public Map<UUID, HostNameAndLoad> getRunIdAgentMap() { public Map<UUID, HostNameAndLoad> getRunIdAgentMap() {

View File

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

View File

@ -4,9 +4,13 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
@ -14,6 +18,7 @@ import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
import org.bench4q.master.api.model.AgentBriefModel; 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.TestPlanModel;
import org.bench4q.master.communication.AgentStateService; import org.bench4q.master.communication.AgentStateService;
import org.bench4q.master.communication.HttpRequester; 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.communication.agent.TestBriefStatusModel;
import org.bench4q.master.entity.db.Agent; import org.bench4q.master.entity.db.Agent;
import org.bench4q.master.service.AgentService; import org.bench4q.master.service.AgentService;
import org.bench4q.master.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -32,9 +38,11 @@ public class TestPlanRunner implements Runnable {
private AgentService agentService; private AgentService agentService;
private AgentStateService agentStateService; private AgentStateService agentStateService;
private HttpRequester httpRequester; private HttpRequester httpRequester;
private AgentContext agentContext; private RunningAgentInfo runningAgentInfo;
public ScriptService scriptService = new ScriptService();
private static final int port = 6565; private static final int port = 6565;
private List<TestPlanModel> taskList = new ArrayList<TestPlanModel>(); private Map<UUID, TestPlanModel> _tasks = new HashMap<UUID, TestPlanModel>();
private final int POOL_SIZE = 100;
public AgentService getAgentService() { public AgentService getAgentService() {
return agentService; return agentService;
@ -63,41 +71,45 @@ public class TestPlanRunner implements Runnable {
this.httpRequester = httpRequester; this.httpRequester = httpRequester;
} }
public AgentContext getAgentContext() { public RunningAgentInfo getrunningAgentInfo() {
return agentContext; return runningAgentInfo;
} }
@Autowired @Autowired
public void setAgentContext(AgentContext agentContext) { public void setAgentContext(RunningAgentInfo agentContext) {
this.agentContext = agentContext; this.runningAgentInfo = agentContext;
} }
public void setTaskList(List<TestPlanModel> taskList) { public void setTaskList(Map<UUID, TestPlanModel> tasks) {
this.taskList = taskList; this._tasks = tasks;
} }
public void addATask(TestPlanModel task) { public UUID addATask(UUID testPlanId, TestPlanModel task) {
synchronized (this.taskList) { synchronized (this._tasks) {
this.taskList.add(task); this._tasks.put(testPlanId, task);
this.taskList.notifyAll(); this._tasks.notifyAll();
} }
return testPlanId;
} }
public void removeATask(int index) { public void removeATask(UUID testPlanId) {
this.taskList.remove(index); this._tasks.remove(testPlanId);
} }
public void run() { public void run() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
Iterator<UUID> iterator = this._tasks.keySet().iterator();
UUID testPlanId;
try { try {
while (!Thread.interrupted()) { while (!Thread.interrupted()) {
synchronized (this.taskList) { synchronized (this._tasks) {
if (this.taskList.size() == 0) { if (this._tasks.size() == 0) {
this.taskList.wait(); this._tasks.wait();
} }
while (this.taskList.size() > 0) { while (this._tasks.size() > 0) {
removeATask(0); testPlanId = iterator.next();
removeATask(testPlanId);
} }
} }
} }
@ -137,16 +149,15 @@ public class TestPlanRunner implements Runnable {
runScenarioResultModel = this.sendScriptContentToAgent( runScenarioResultModel = this.sendScriptContentToAgent(
agent.getHostName(), agent.getPort(), agent.getHostName(), agent.getPort(),
this.makeRunScenarioModelToString(runScenarioModel)); this.makeRunScenarioModelToString(runScenarioModel));
runScenarioResultModel.setHostName(agent.getHostName());
HostNameAndLoad hostLoad = new HostNameAndLoad(); HostNameAndLoad hostLoad = new HostNameAndLoad();
hostLoad.setHostName(agent.getHostName()); hostLoad.setHostName(agent.getHostName());
hostLoad.setLoad(min); hostLoad.setLoad(min);
this.agentContext.addToRunIdHostLoadMap( this.runningAgentInfo.addToRunIdHostLoadMap(
runScenarioResultModel.getRunId(), hostLoad); runScenarioResultModel.getRunId(), hostLoad);
this.agentContext.addToRunIdHostLoadMap(runScenarioResultModel this.runningAgentInfo.addToRunIdHostLoadMap(runScenarioResultModel
.getRunId(), new HostNameAndLoad(agent.getHostName(), min)); .getRunId(), new HostNameAndLoad(agent.getHostName(), min));
resulList.add(runScenarioResultModel); resulList.add(runScenarioResultModel);
requireLoad -= min; requireLoad -= min;
@ -214,7 +225,7 @@ public class TestPlanRunner implements Runnable {
AgentBriefModel result = new AgentBriefModel(); AgentBriefModel result = new AgentBriefModel();
TestBriefStatusModel briefStatusModel = new TestBriefStatusModel(); TestBriefStatusModel briefStatusModel = new TestBriefStatusModel();
try { try {
HostNameAndLoad hostNameAndLoad = this.agentContext HostNameAndLoad hostNameAndLoad = this.runningAgentInfo
.getRunIdAgentMap().get(runId); .getRunIdAgentMap().get(runId);
if (hostNameAndLoad == null) { if (hostNameAndLoad == null) {
return null; return null;
@ -262,4 +273,54 @@ public class TestPlanRunner implements Runnable {
return result; 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<ScriptIdRequireLoadModel> iterator = testPlanModel
.getScriptIdRequireLoads().iterator();
while (iterator.hasNext()) {
ScriptIdRequireLoadModel sModel = iterator.next();
this._runTestPlanWithScriptId(sModel.getScriptId(),
sModel.getRequireLoad());
}
}
public List<RunScenarioResultModel> _runTestPlanWithScriptId(int scriptId,
int requireLoad) {
RunScenarioModel runScenarioModel = this.scriptService
.getRunSceniroModelByScriptId(scriptId);
if (runScenarioModel == null) {
return null;
}
try {
List<RunScenarioResultModel> list = runTestWithRunScenarioModel(
runScenarioModel, requireLoad);
return list;
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
} }

View File

@ -60,12 +60,17 @@ public class AgentPoolControllerTest extends TestBase {
System.out.println(agentResponseModel.isSuccess()); System.out.println(agentResponseModel.isSuccess());
} }
public void removeAgentToPool() throws IOException { public void removeAgentToPool() throws IOException, JAXBException {
String accesTocken = this.login();
Map<String, String> propertiesMap = new HashMap<String, String>();
propertiesMap.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER
+ accesTocken);
String urlString = this.URLSTRING + "/removeAgentFromPool"; String urlString = this.URLSTRING + "/removeAgentFromPool";
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
map.put("hostName", HOSTNAME); map.put("hostName", HOSTNAME);
HttpResponse httpResponse = this.httpRequester.sendGet(urlString, map, HttpResponse httpResponse = this.httpRequester.sendGet(urlString, map,
null); propertiesMap);
System.out.println(httpResponse.getContent()); System.out.println(httpResponse.getContent());
} }