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;
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<RunScenarioResultModel> 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<RunScenarioResultModel> list) {
public static TestPlanResponseModel buildTestPlanResponseModel(
boolean success, String failCause, List<RunScenarioResultModel> 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<ScriptIdRequireLoadModel> 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<RunScenarioResultModel> 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;
}
}

View File

@ -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<ScriptIdRequireLoadModel> scriptIdRequireLoads;

View File

@ -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<RunScenarioResultModel> 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<RunScenarioResultModel> getRunScenarioResultModels() {
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 {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
hostName + ":" + port + "/", null, null);
if (httpResponse.getCode() == 404) {
if (httpResponse == null || httpResponse.getCode() == 404) {
return false;
}
return true;

View File

@ -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();

View File

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

View File

@ -7,7 +7,7 @@ import java.util.UUID;
import org.springframework.stereotype.Component;
@Component
public class AgentContext {
public class RunningAgentInfo {
private Map<UUID, HostNameAndLoad> runIdHostLoadMap = new HashMap<UUID, HostNameAndLoad>();
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.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<TestPlanModel> taskList = new ArrayList<TestPlanModel>();
private Map<UUID, TestPlanModel> _tasks = new HashMap<UUID, TestPlanModel>();
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<TestPlanModel> taskList) {
this.taskList = taskList;
public void setTaskList(Map<UUID, TestPlanModel> 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<UUID> 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<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());
}
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";
Map<String, String> map = new HashMap<String, String>();
map.put("hostName", HOSTNAME);
HttpResponse httpResponse = this.httpRequester.sendGet(urlString, map,
null);
propertiesMap);
System.out.println(httpResponse.getContent());
}