refactoring

This commit is contained in:
coderfengyun 2013-07-18 15:36:24 +08:00
parent 5ab657f721
commit 765f0c13bb
8 changed files with 117 additions and 113 deletions

View File

@ -13,10 +13,8 @@ import org.bench4q.master.api.model.AgentResponseModel;
import org.bench4q.master.communication.AgentStateService; import org.bench4q.master.communication.AgentStateService;
import org.bench4q.master.communication.HttpRequester; import org.bench4q.master.communication.HttpRequester;
import org.bench4q.master.communication.HttpRequester.HttpResponse; import org.bench4q.master.communication.HttpRequester.HttpResponse;
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.SyncAgent; import org.bench4q.master.entity.SyncAgent;
import org.bench4q.master.service.AgentHelper;
import org.bench4q.master.service.AgentService; import org.bench4q.master.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -28,7 +26,6 @@ import org.springframework.web.bind.annotation.RequestParam;
@RequestMapping("/agentManage") @RequestMapping("/agentManage")
public class AgentController extends BaseController { public class AgentController extends BaseController {
private AgentService agentPoolService = new AgentService(); private AgentService agentPoolService = new AgentService();
private AgentHelper agentHelper = new AgentHelper();
private HttpRequester httpRequester = new HttpRequester(); private HttpRequester httpRequester = new HttpRequester();
private AgentStateService agentStateService = new AgentStateService(); private AgentStateService agentStateService = new AgentStateService();
private static final Object AGENT_LOCK = new Object(); private static final Object AGENT_LOCK = new Object();
@ -60,15 +57,6 @@ public class AgentController extends BaseController {
this.httpRequester = httpRequester; this.httpRequester = httpRequester;
} }
public AgentHelper getAgentHelper() {
return agentHelper;
}
@Autowired
public void setAgentHelper(AgentHelper agentHelper) {
this.agentHelper = agentHelper;
}
@RequestMapping(value = "/AddAgentToPool", method = RequestMethod.POST) @RequestMapping(value = "/AddAgentToPool", method = RequestMethod.POST)
public AgentResponseModel addAgentToPool(@RequestParam SyncAgent syncAgent) { public AgentResponseModel addAgentToPool(@RequestParam SyncAgent syncAgent) {
synchronized (AGENT_LOCK) { synchronized (AGENT_LOCK) {
@ -138,12 +126,6 @@ public class AgentController extends BaseController {
} }
} }
public RunScenarioResultModel sendScriptContentToAgent(
String hostNameString, int port, String scriptContent) {
return this.getAgentHelper().sendScriptContentToAgent(hostNameString,
port, scriptContent);
}
public int getLivingAgentCount() { public int getLivingAgentCount() {
int livingCount = 0; int livingCount = 0;
if (this.getAgentPoolService().getAgentPool().isEmpty()) { if (this.getAgentPoolService().getAgentPool().isEmpty()) {

View File

@ -2,6 +2,8 @@ package org.bench4q.master.api;
import org.bench4q.master.api.model.OrganizeRecordPortResponseModel; import org.bench4q.master.api.model.OrganizeRecordPortResponseModel;
import org.bench4q.master.entity.Constant; import org.bench4q.master.entity.Constant;
import org.bench4q.master.service.PortPoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
@ -11,6 +13,18 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Controller @Controller
@RequestMapping("/RecordPort") @RequestMapping("/RecordPort")
public class RecordPortController extends BaseController { public class RecordPortController extends BaseController {
private PortPoolService portPoolService = new PortPoolService();
public PortPoolService getPortPoolService() {
return portPoolService;
}
@Autowired
private void setPortPoolService(PortPoolService portPoolService) {
this.portPoolService = portPoolService;
}
@RequestMapping(value = "/AddPortToPortPool", method = RequestMethod.GET) @RequestMapping(value = "/AddPortToPortPool", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public OrganizeRecordPortResponseModel addPortToPortPool( public OrganizeRecordPortResponseModel addPortToPortPool(

View File

@ -1,14 +1,21 @@
package org.bench4q.master.api; package org.bench4q.master.api;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.bench4q.master.communication.AgentStateService; 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.RunScenarioResultModel; import org.bench4q.master.communication.agent.RunScenarioResultModel;
import org.bench4q.master.entity.SyncAgent; import org.bench4q.master.entity.SyncAgent;
import org.bench4q.master.service.AgentHelper;
import org.bench4q.master.service.AgentService; import org.bench4q.master.service.AgentService;
import org.bench4q.master.service.ScriptService; import org.bench4q.master.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -22,8 +29,8 @@ import org.springframework.web.bind.annotation.RequestParam;
public class TestPlanController extends BaseController { public class TestPlanController extends BaseController {
private ScriptService scriptService = new ScriptService(); private ScriptService scriptService = new ScriptService();
private AgentService agentPoolService = new AgentService(); private AgentService agentPoolService = new AgentService();
private AgentHelper agentHelper = new AgentHelper();
private AgentStateService agentStateService = new AgentStateService(); private AgentStateService agentStateService = new AgentStateService();
private HttpRequester httpRequester = new HttpRequester();
public ScriptService getScriptService() { public ScriptService getScriptService() {
return scriptService; return scriptService;
@ -38,15 +45,6 @@ public class TestPlanController extends BaseController {
return agentPoolService; return agentPoolService;
} }
public AgentHelper getAgentHelper() {
return agentHelper;
}
@Autowired
public void setAgentHelper(AgentHelper agentHelper) {
this.agentHelper = agentHelper;
}
@Autowired @Autowired
public void setAgentPoolService(AgentService agentPoolService) { public void setAgentPoolService(AgentService agentPoolService) {
this.agentPoolService = agentPoolService; this.agentPoolService = agentPoolService;
@ -61,6 +59,15 @@ public class TestPlanController extends BaseController {
this.agentStateService = agentStateService; this.agentStateService = agentStateService;
} }
public HttpRequester getHttpRequester() {
return httpRequester;
}
@Autowired
public void setHttpRequester(HttpRequester httpRequester) {
this.httpRequester = httpRequester;
}
@RequestMapping(value = "/runTestPlanWithScriptId", method = RequestMethod.GET) @RequestMapping(value = "/runTestPlanWithScriptId", method = RequestMethod.GET)
public void runTestPlanWithScriptId(@RequestParam int scriptId, public void runTestPlanWithScriptId(@RequestParam int scriptId,
@RequestParam int requireLoad) { @RequestParam int requireLoad) {
@ -88,9 +95,8 @@ public class TestPlanController extends BaseController {
} }
if (syncAgent.getRemainLoad() > requireLoad) { if (syncAgent.getRemainLoad() > requireLoad) {
RunScenarioResultModel runScenarioResultModel = this RunScenarioResultModel runScenarioResultModel = this
.getAgentHelper().sendScriptContentToAgent( .sendScriptContentToAgent(syncAgent.getHostName(),
syncAgent.getHostName(), syncAgent.getPort(), syncAgent.getPort(), scriptContentString);
scriptContentString);
if (!runScenarioResultModel.getRunId().equals(null)) { if (!runScenarioResultModel.getRunId().equals(null)) {
map.put(syncAgent.getHostName(), map.put(syncAgent.getHostName(),
runScenarioResultModel.getRunId()); runScenarioResultModel.getRunId());
@ -100,4 +106,35 @@ public class TestPlanController extends BaseController {
} }
return map; return map;
} }
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;
}
} }

View File

@ -0,0 +1,31 @@
package org.bench4q.master.entity.db;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "port")
public class Port {
private int id;
private int port;
@Column(name = "id", nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "port", nullable = false)
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}

View File

@ -1,59 +0,0 @@
package org.bench4q.master.service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.bench4q.master.communication.HttpRequester;
import org.bench4q.master.communication.HttpRequester.HttpResponse;
import org.bench4q.master.communication.agent.RunScenarioResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AgentHelper {
private HttpRequester httpRequester = new HttpRequester();
public HttpRequester getHttpRequester() {
return httpRequester;
}
@Autowired
public void setHttpRequester(HttpRequester httpRequester) {
this.httpRequester = httpRequester;
}
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;
}
}

View File

@ -16,20 +16,15 @@ import org.springframework.stereotype.Component;
@Component @Component
public class AgentService { public class AgentService {
private SessionHelper sessionHelper = new SessionHelper(); private SessionHelper sessionHelper = new SessionHelper();
private Map<String, SyncAgent> agentPool = new HashMap<String, SyncAgent>(); private static Map<String, SyncAgent> agentPool = new HashMap<String, SyncAgent>();
public AgentService() { public AgentService() {
this.setAgentPool(new HashMap<String, SyncAgent>());
} }
public Map<String, SyncAgent> getAgentPool() { public Map<String, SyncAgent> getAgentPool() {
return agentPool; return agentPool;
} }
private void setAgentPool(Map<String, SyncAgent> agentPool) {
this.agentPool = agentPool;
}
private SessionHelper getSessionHelper() { private SessionHelper getSessionHelper() {
return sessionHelper; return sessionHelper;
} }

View File

@ -2,30 +2,34 @@ package org.bench4q.master.service;
import java.util.Vector; import java.util.Vector;
import org.springframework.stereotype.Component;
@Component
public class PortPoolService { public class PortPoolService {
private Vector<Integer> ScriptPortPool = new Vector<Integer>(); private static Vector<Integer> ScriptPortPool = new Vector<Integer>();
private static Object syncObject = new Object();
public PortPoolService() { public static Object getSyncObject() {
this.setScriptPortPool(new Vector<Integer>()); return syncObject;
} }
public Vector<Integer> getScriptPortPool() { public Vector<Integer> getScriptPortPool() {
return ScriptPortPool; return PortPoolService.ScriptPortPool;
} }
private void setScriptPortPool(Vector<Integer> scriptPortPool) { public boolean addPortToPool(int port) {
ScriptPortPool = scriptPortPool; synchronized (PortPoolService.syncObject) {
if (this.getScriptPortPool().contains(port)) {
return false;
}
this.getScriptPortPool().add(port);
}
return true;
} }
public void addPortToPool(int port) public void removePortFromPool(int port) {
{ if (this.getScriptPortPool().contains(port)) {
this.getScriptPortPool().add(port);
}
public void removePortFromPool(int port)
{
if(this.getScriptPortPool().contains(port)){
} }
} }

View File

@ -84,7 +84,7 @@ public class TestPlanTester {
hostNameString = iterator.next(); hostNameString = iterator.next();
TestBriefStatusModel testBriefStatusModel = (new AgentController()).getBriefStatusModelFromAgent( TestBriefStatusModel testBriefStatusModel = (new AgentController()).getBriefStatusModelFromAgent(
hostNameString, 6565, vector.get(hostNameString)); hostNameString, 6565, vector.get(hostNameString));
System.out.println(testBriefStatusModel); System.out.println(testBriefStatusModel.getAverageResponseTime());
} }
} }