refactor and remove some bugs

refactor and remove some bugs
This commit is contained in:
coderfengyun 2014-04-23 20:27:13 +08:00
parent 7288b9d9fa
commit e1b37bb4a0
30 changed files with 234 additions and 470 deletions

View File

@ -1,6 +1,5 @@
package org.bench4q.agent.api;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -30,7 +29,6 @@ import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@ -44,10 +42,6 @@ public class TestController {
private ParameterFileCollector paramFileCollector;
private Logger logger = Logger.getLogger(TestController.class);
private Logger getLogger() {
return logger;
}
private ScenarioEngine getScenarioEngine() {
return scenarioEngine;
}
@ -66,21 +60,37 @@ public class TestController {
this.paramFileCollector = paramFileCollector;
}
@RequestMapping(value = "/submitScenarioWithParams", method = RequestMethod.POST)
@RequestMapping(value = "/bookTest/{poolSize}", method = {
RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public String submitParams(
@RequestParam("files[]") List<MultipartFile> files,
@RequestParam("scenarioModel") String scenarioModel) {
public RunScenarioResultModel bookTest(@PathVariable int poolSize) {
try {
UUID runId = UUID.randomUUID();
this.getScenarioEngine().addRunningTestWithoutScenario(runId,
poolSize);
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
runScenarioResultModel.setRunId(runId);
return runScenarioResultModel;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@RequestMapping(value = "/submitScenarioWithParams/{runId}", method = RequestMethod.POST)
@ResponseBody
public String submitParams(
@PathVariable UUID runId,
@RequestParam(value = "files[]", required = false) List<MultipartFile> files,
@RequestParam("scenarioModel") String scenarioModel) {
try {
this.getParamFileCollector().collectParamFiles(files, runId);
System.out.println(scenarioModel);
RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper
.unmarshal(RunScenarioModel.class, scenarioModel);
this.getScenarioEngine().submitScenario(runId,
Scenario.scenarioBuilderWithCompile(runScenarioModel),
runScenarioModel.getPoolSize());
Scenario.scenarioBuilderWithCompile(runScenarioModel));
return MarshalHelper.tryMarshal(buildWith(runId));
} catch (Exception e) {
logger.error("/submitScenarioWithParams", e);
@ -101,29 +111,6 @@ public class TestController {
: null;
}
@RequestMapping(value = "/runWithoutParams", method = RequestMethod.POST)
@ResponseBody
public RunScenarioResultModel run(
@RequestBody RunScenarioModel runScenarioModel)
throws UnknownHostException {
Scenario scenario = Scenario
.scenarioBuilderWithCompile(runScenarioModel);
UUID runId = UUID.randomUUID();
System.out.println(runScenarioModel.getPoolSize());
this.getLogger().info(MarshalHelper.tryMarshal(runScenarioModel));
if (runScenarioModel.getPoolSize() <= 0) {
logger.info("This RunScenarioModel's pool size is L.E zero, so throw out");
return null;
}
this.getScenarioEngine().submitScenario(runId, scenario,
runScenarioModel.getPoolSize());
this.getScenarioEngine().runWith(runId);
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
runScenarioResultModel.setRunId(runId);
return runScenarioResultModel;
}
@RequestMapping(value = "/briefAll/{runId}", method = RequestMethod.GET)
@ResponseBody
public TestBriefStatusModel briefAll(@PathVariable UUID runId) {

View File

@ -76,7 +76,7 @@ public class Scenario {
}
}
public static Scenario scenarioBuilder(String scenarioContent) {
public static Scenario scenarioBuilderWithCompile(String scenarioContent) {
return scenarioBuilderWithCompile((RunScenarioModel) MarshalHelper
.tryUnmarshal(RunScenarioModel.class, scenarioContent));
}

View File

@ -94,6 +94,25 @@ public class ScenarioContext {
return scenarioContext;
}
public static ScenarioContext buildScenarioContextWithoutScenario(
UUID testId, int poolSize) {
ScenarioContext scenarioContext = new ScenarioContext();
scenarioContext.setTestId(testId);
final SynchronousQueue<Runnable> workQueue = new SynchronousQueue<Runnable>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize,
poolSize, keepAliveTime, TimeUnit.MINUTES, workQueue,
new DiscardPolicy());
scenarioContext.setStartDate(new Date(System.currentTimeMillis()));
scenarioContext.setExecutorService(executor);
scenarioContext.setDataStatistics(new ScenarioResultCollector(testId));
return scenarioContext;
}
public ScenarioContext addScenrio(Scenario scenario) {
this.setScenario(scenario);
return this;
}
/**
* Now, I tolerate that if the requiredLoad <
* this.getExecutor.getCorePoolSize(), then the excess threads will be

View File

@ -25,13 +25,24 @@ public class ScenarioEngine {
this.runningTests = runningTests;
}
public void submitScenario(UUID runId, final Scenario scenario, int poolSize) {
public void addRunningTestWithoutScenario(UUID runId, int poolSize) {
try {
final ScenarioContext scenarioContext = ScenarioContext
.buildScenarioContext(runId, scenario, poolSize);
.buildScenarioContextWithoutScenario(runId, poolSize);
this.getRunningTests().put(runId, scenarioContext);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(poolSize);
public void submitScenario(UUID runId, final Scenario scenario) {
try {
this.getRunningTests().get(runId).addScenrio(scenario);
// final ScenarioContext scenarioContext = ScenarioContext
// .buildScenarioContext(runId, scenario, poolSize);
// this.getRunningTests().put(runId, scenarioContext);
// System.out.println(poolSize);
} catch (Exception e) {
e.printStackTrace();
@ -69,6 +80,6 @@ public class ScenarioEngine {
public void updatePopulation(UUID testId, int requiredLoad) {
ScenarioContext context = this.getRunningTests().get(testId);
context.updatePopulation(requiredLoad);
context.updatePopulation(requiredLoad);
}
}

View File

@ -1,2 +1 @@
123,234,566
167,567,789
key=set;value=nv

View File

@ -0,0 +1 @@
everyThing ok = ok

View File

@ -39,16 +39,13 @@ public class AgentController extends BaseController {
"you don't have the power to add agent to pool!",
new ArrayList<AgentModel>());
}
synchronized (this.getAgentService().getAgentLock()) {
if (!this.getAgentService().addAgentToPool(
BusinessModelMapFactory.toBusiness(agent))) {
return setAgentResponseModel(false,
"add agent to DB fails in addAgentToPool",
new ArrayList<AgentModel>());
}
this.getAgentService().getAgentLock().notifyAll();
return setAgentResponseModel(true, "", new ArrayList<AgentModel>());
if (!this.getAgentService().addAgentToPool(
BusinessModelMapFactory.toBusiness(agent))) {
return setAgentResponseModel(false,
"add agent to DB fails in addAgentToPool",
new ArrayList<AgentModel>());
}
return setAgentResponseModel(true, "", new ArrayList<AgentModel>());
}
@RequestMapping(value = "/removeAgentFromPool", method = RequestMethod.GET)
@ -61,15 +58,13 @@ public class AgentController extends BaseController {
"you don't have the power to remove agent from pool!",
new ArrayList<AgentModel>());
}
synchronized (this.getAgentService().getAgentLock()) {
if (!this.getAgentService().removeAgentFromPool(agentId)) {
return setAgentResponseModel(false,
"remove agent from DB fails in removeAgentFromPool",
new ArrayList<AgentModel>());
}
return this.setAgentResponseModel(true, "",
if (!this.getAgentService().removeAgentFromPool(agentId)) {
return setAgentResponseModel(false,
"remove agent from DB fails in removeAgentFromPool",
new ArrayList<AgentModel>());
}
return this
.setAgentResponseModel(true, "", new ArrayList<AgentModel>());
}
@RequestMapping(value = "/queryAgentList", method = { RequestMethod.POST,

View File

@ -6,7 +6,12 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.share.models.agent.RunScenarioResultModel;
@Entity
@Table(name = "agent")
@ -18,8 +23,6 @@ public class Agent {
protected int remainLoad;
protected int currentStatus;
// private AgentRepository repository;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
@ -86,4 +89,16 @@ public class Agent {
return agent;
}
public void resetLoad() {
this.setRemainLoad(this.getMaxLoad());
}
public RunScenarioResultModel bookTest(int requireLoad) {
RunScenarioResultModel resultModel = ApplicationContextHelper.getBean(
AgentMessenger.class).bookTest(this, requireLoad);
this.setCurrentStatus(AgentService.AGENT_STATUS_InRun);
this.setRemainLoad(this.getRemainLoad() - requireLoad);
ApplicationContextHelper.getBean(AgentRepository.class).update(this);
return resultModel;
}
}

View File

@ -133,11 +133,11 @@ public class RunningAgentDB implements RunningAgentInterface {
runScenarioModel.setPoolSize(getLoadInUse());
RunScenarioResultModel runScenarioResultModel = this
.getAgentMessenger().submitScenrioWithParams(this.getAgent(),
script2.loadParamFiles(), runScenarioModel);
this.getAgentRunId(), script2.loadParamFiles(),
runScenarioModel);
if (runScenarioResultModel == null) {
return false;
}
this.setAgentRunId(runScenarioResultModel.getRunId());
return true;
}

View File

@ -102,6 +102,9 @@ public class Script {
private void saveScriptParamFiles(List<MultipartFile> paramFiles) {
String folderPath = buildParamFilesFolder();
FileHelper.guardFolderExist(folderPath);
if (paramFiles == null) {
return;
}
for (MultipartFile multipartFile : paramFiles) {
try {
multipartFile.transferTo(new File(folderPath

View File

@ -195,13 +195,17 @@ public class TestPlan implements IAggregate {
public void run() {
Logger.getLogger(TestPlan.class).info(
this.getTestPlanRunId() + " start to run ");
this.setLastRunningTime(new Date());
if (!applyForLoad()) {
this.failTimes++;
return;
}
if (!distributeScriptAndParams()) {
this.failTimes++;
return;
}
doRun();
this.update();
}
private boolean applyForLoad() {
@ -216,7 +220,9 @@ public class TestPlan implements IAggregate {
result = false;
}
this.setCurrentStatus(currentStatus.name());
this.getRepository().updateEntity(this);
Logger.getLogger(TestPlan.class).info(
this.getTestPlanRunId() + " status is " + this.currentStatus);
this.update();
return result;
}

View File

@ -1,5 +1,6 @@
package org.bench4q.master.domain.repository;
import java.util.Collection;
import java.util.List;
import org.bench4q.master.domain.entity.Agent;
@ -14,6 +15,12 @@ import org.springframework.stereotype.Component;
@Component
public class AgentRepository extends AbstractRepositoty {
private final Object addDeleteLock = new Object();
public Object getAddDeleteLock() {
return addDeleteLock;
}
public boolean attach(Agent agentWithoutId) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
@ -82,6 +89,24 @@ public class AgentRepository extends AbstractRepositoty {
}
}
public boolean Update(Collection<Agent> poolToUpdate) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
for (Agent agent : poolToUpdate) {
session.update(agent);
}
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
public Agent getAgentBy(String hostName) {
Session session = this.getSessionHelper().openSession();
try {

View File

@ -49,22 +49,27 @@ public class AgentService {
}
public boolean addAgentToPool(Agent agentWithoutId) {
if (!this.getAgentRepository().attach(
Agent.createAgentWithoutId(agentWithoutId.getHostName(),
agentWithoutId.getPort()))) {
return false;
synchronized (this.getAgentRepository().getAddDeleteLock()) {
if (!this.getAgentRepository().attach(
Agent.createAgentWithoutId(agentWithoutId.getHostName(),
agentWithoutId.getPort()))) {
return false;
}
Agent agent = this.getAgentRepository().getAgentBy(
agentWithoutId.getHostName());
if (agent == null) {
return false;
}
this.getHighAvailablePool().getPool()
.put(agent.getHostName(), agent);
return true;
}
Agent agent = this.getAgentRepository().getAgentBy(
agentWithoutId.getHostName());
if (agent == null) {
return false;
}
this.getHighAvailablePool().getPool().put(agent.getHostName(), agent);
return true;
}
public boolean removeAgentFromPool(int agentId) {
return this.getAgentRepository().detach(agentId);
synchronized (this.getAgentRepository().getAddDeleteLock()) {
return this.getAgentRepository().detach(agentId);
}
}
public List<Agent> loadAgentPoolFromDB() {

View File

@ -119,6 +119,7 @@ public class TestPlanEngine implements TaskCompleteCallback,
return;
}
testPlan.run();
this.getTestPlanRepository().attachRunningTestPlan(testPlan);
}
public void doTaskComplete(UUID testPlanID) {
@ -132,7 +133,7 @@ public class TestPlanEngine implements TaskCompleteCallback,
logger.info("Test plan with id " + testPlanID.toString() + " finishes!");
}
public void cleanUpTestPlan(TestPlan testPlan) {
private void cleanUpTestPlan(TestPlan testPlan) {
this.getHaPool().cleanUpAboutTestPlan(testPlan.getTestPlanScripts());
this.getTestPlanRepository().detachRunningTestPlan(
UUID.fromString(testPlan.getTestPlanRunId()));

View File

@ -1,117 +0,0 @@
package org.bench4q.master.domain.valueobject.transaction.impl;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.domain.valueobject.transaction.Transaction;
import org.bench4q.master.domain.valueobject.transaction.exception.AgentRunException;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
public class AgentExecutionTransaction implements Transaction {
private String agentHostName;
private int agentPort;
private RunScenarioModel runScenarioModel;
private AgentService agentService;
private AgentMessenger agentMessenger;
private Logger logger = Logger.getLogger(AgentExecutionTransaction.class);
private String getAgentHostName() {
return agentHostName;
}
private void setAgentHostName(String agentHostName) {
this.agentHostName = agentHostName;
}
private int getAgentPort() {
return agentPort;
}
private void setAgentPort(int agentPort) {
this.agentPort = agentPort;
}
private RunScenarioModel getRunScenarioModel() {
return runScenarioModel;
}
private void setRunScenarioModel(RunScenarioModel runScenarioModel) {
this.runScenarioModel = runScenarioModel;
}
private AgentService getAgentService() {
return agentService;
}
private void setAgentService(AgentService agentService) {
this.agentService = agentService;
}
public AgentMessenger getAgentMessenger() {
return agentMessenger;
}
public void setAgentMessenger(AgentMessenger agentMessenger) {
this.agentMessenger = agentMessenger;
}
public AgentExecutionTransaction(String agentHostName, int port,
RunScenarioModel runScenarioModel) {
this.setAgentHostName(agentHostName);
this.setAgentPort(port);
this.setRunScenarioModel(runScenarioModel);
this.setAgentService(ApplicationContextHelper.getContext().getBean(
AgentService.class));
this.setAgentMessenger(ApplicationContextHelper.getContext().getBean(
AgentMessenger.class));
}
public Object execute() throws AgentRunException {
try {
if (!this.getAgentService().getLoadFromAgent(
this.getAgentHostName(),
this.getRunScenarioModel().getPoolSize())) {
throw new AgentRunException();
}
if (!isValidRunScenarioModel(runScenarioModel)) {
throw new AgentRunException();
}
RunScenarioResultModel ret = this.getAgentMessenger()
.runWithoutParams(
buildAgent(this.getAgentHostName(),
this.getAgentPort()),
this.getRunScenarioModel());
if (ret == null) {
throw new AgentRunException();
}
return ret;
} catch (IOException e) {
this.logger.error(ExceptionLog.getStackTrace(e).toString());
throw new AgentRunException();
}
}
private Agent buildAgent(String hostName, int port) {
Agent agent = new Agent();
agent.setHostName(hostName);
agent.setPort(port);
return agent;
}
private boolean isValidRunScenarioModel(RunScenarioModel runScenarioModel) {
return runScenarioModel == null ? false : true;
}
public void rollBack() {
this.getAgentService().backLoadToAgent(this.getAgentHostName(),
this.getRunScenarioModel().getPoolSize());
}
}

View File

@ -11,8 +11,6 @@ import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.RunningAgentInterface.AbstractRunningAgent;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.domain.valueobject.transaction.Transaction;
import org.bench4q.master.domain.valueobject.transaction.exception.ScriptLoadDistributeException;
import org.bench4q.master.exception.ExceptionLog;
@ -21,6 +19,7 @@ import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.highavailable.AgentRunBlotter;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.StopTestModel;
public abstract class ScriptLoadBase implements Transaction {
@ -29,7 +28,6 @@ public abstract class ScriptLoadBase implements Transaction {
private List<RunningAgentInterface> agentListThisTime;
private AgentMessenger runningAgentService;
private HighAvailablePool highAvailableAgentPool;
private AgentRepository agentRepository;
private static Logger logger = Logger
.getLogger(ScriptLoadApplication.class);
@ -75,14 +73,6 @@ public abstract class ScriptLoadBase implements Transaction {
this.highAvailableAgentPool = highAvailableAgentPool;
}
private AgentRepository getAgentRepository() {
return agentRepository;
}
private void setAgentRepository(AgentRepository agentRepository) {
this.agentRepository = agentRepository;
}
public static Logger getLogger() {
return logger;
}
@ -103,8 +93,6 @@ public abstract class ScriptLoadBase implements Transaction {
.getBean(AgentMessenger.class));
this.setHighAvailableAgentPool(ApplicationContextHelper.getContext()
.getBean(HighAvailablePool.class));
this.setAgentRepository(ApplicationContextHelper
.getBean(AgentRepository.class));
this.setAgentListThisTime(new ArrayList<RunningAgentInterface>());
}
@ -122,8 +110,8 @@ public abstract class ScriptLoadBase implements Transaction {
+ this.getRunningScript().getScriptId());
throw new RuntimeException("Running Script not valid!");
}
this.runAgentsWithScenario(getRequireLoad(), this
.getRunningScript().getScriptId(), this.getTestPlanRunID());
this.applyForLoad(getRequireLoad(), this.getRunningScript()
.getScriptId(), this.getTestPlanRunID());
return this.getAgentListThisTime();
} catch (Exception e) {
@ -142,11 +130,10 @@ public abstract class ScriptLoadBase implements Transaction {
protected abstract int getRequireLoad();
private void runAgentsWithScenario(int totalRequireLoad, int scriptId,
private void applyForLoad(int totalRequireLoad, int scriptId,
UUID testPlanId) throws JAXBException,
ScriptLoadDistributeException {
// RunScenarioResultModel runScenarioResultModel = new
// RunScenarioResultModel();
RunScenarioResultModel runScenarioResultModel = null;
int loadForRunCurrent;
if (totalRequireLoad >= this.getHighAvailableAgentPool()
@ -154,7 +141,6 @@ public abstract class ScriptLoadBase implements Transaction {
logger.info("currentAvailableLoad not enough for substitute");
return;
}
for (Agent agent : this.getHighAvailableAgentPool().getPool().values()) {
if (allocationFinish(totalRequireLoad)) {
break;
@ -164,20 +150,20 @@ public abstract class ScriptLoadBase implements Transaction {
continue;
}
loadForRunCurrent = getMin(totalRequireLoad, agent.getRemainLoad());
// runScenarioResultModel = agent.runWithouParams(runScenarioModel);
// if (runScenarioResultModel == null) {
// continue;
// }
agent.setCurrentStatus(AgentService.AGENT_STATUS_InRun);
agent.setRemainLoad(agent.getRemainLoad() - loadForRunCurrent);
updateAgentInRepository(agent);
runScenarioResultModel = agent.bookTest(loadForRunCurrent);
if (runScenarioResultModel == null
|| runScenarioResultModel.getRunId() == null) {
logger.error(runScenarioResultModel == null ? "runScenarioResultModel is null"
: "runScenarioResultModel.getRunId()"
+ runScenarioResultModel.getRunId());
continue;
}
RunningAgentInterface runningAgent = AbstractRunningAgent
.buildRunningAgentDBWithoutId(agent, loadForRunCurrent,
this.getRunningScript(), null);
this.getRunningScript(),
runScenarioResultModel.getRunId());
this.getAgentListThisTime().add(runningAgent);
// This one should move to runningAgent.run();
updateAgentBlotterInHA(testPlanId, runningAgent);
addAgentBlotterToHA(testPlanId, runningAgent);
totalRequireLoad -= loadForRunCurrent;
}
if (!allocationFinish(totalRequireLoad)) {
@ -187,15 +173,12 @@ public abstract class ScriptLoadBase implements Transaction {
}
}
private void updateAgentInRepository(Agent agent) {
this.getAgentRepository().update(agent);
}
private boolean allocationFinish(int requireLoad) {
return requireLoad <= 0;
}
private boolean inUse(Agent agent) {
logger.info(agent.getCurrentStatus() + " : " + agent.getRemainLoad());
return agent.getRemainLoad() < agent.getMaxLoad();
}
@ -204,7 +187,7 @@ public abstract class ScriptLoadBase implements Transaction {
: remainLoadByStart;
}
private void updateAgentBlotterInHA(UUID testPlanId,
private void addAgentBlotterToHA(UUID testPlanId,
RunningAgentInterface runningAgent) {
this.getHighAvailableAgentPool()
.getAgentRunBlotters()

View File

@ -1,16 +1,12 @@
package org.bench4q.master.infrastructure.communication;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.infrastructure.highavailable.faultolerence.FaultTolerenceFactory;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
@ -20,10 +16,6 @@ import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.ServerStatusModel;
import org.bench4q.share.models.agent.StopTestModel;
import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel;
import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel;
import org.bench4q.share.models.agent.statistics.AgentPageBriefModel;
import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -41,50 +33,43 @@ public class AgentMessenger {
this.httpRequester = httpRequester;
}
public RunScenarioResultModel runWithoutParams(Agent agent,
RunScenarioModel runScenarioModel) throws IOException {
HttpResponse httpResponse = null;
try {
httpResponse = this.httpRequester.sendPostXml(buildBaseUrl(agent)
+ "/test/runWithoutParams", MarshalHelper.marshal(
RunScenarioModel.class, runScenarioModel), null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (JAXBException e) {
logIt(httpResponse, e);
FaultTolerenceFactory.getRunAgentFault(agent, runScenarioModel)
.doTolerance();
return null;
}
}
private void logIt(HttpResponse httpResponse, Throwable e) {
logger.error(HttpRequester.isInvalidResponse(httpResponse) ? ""
: httpResponse.getContent(), e);
}
public RunScenarioResultModel submitScenrioWithParams(Agent agent,
List<File> paramFiles, final RunScenarioModel runScenarioModel) {
public RunScenarioResultModel bookTest(Agent agent, int requireLoad) {
HttpResponse httpResponse = null;
try {
final String modelCOntent = MarshalHelper
httpResponse = this.getHttpRequester().sendGet(
buildBaseUrl(agent) + "/test/bookTest/" + requireLoad,
null, null);
System.out.println(buildBaseUrl(agent) + "/test/bookTest/"
+ requireLoad);
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (Exception e) {
logger.error(httpResponse.getContent(), e);
return null;
}
}
public RunScenarioResultModel submitScenrioWithParams(Agent agent,
UUID agentRunId, List<File> paramFiles,
final RunScenarioModel runScenarioModel) {
HttpResponse httpResponse = null;
try {
final String modelContent = MarshalHelper
.tryMarshal(runScenarioModel);
httpResponse = this.httpRequester.postFiles(null,
buildBaseUrl(agent) + "/test/submitScenarioWithParams",
"files[]", paramFiles, "scenarioModel",
new LinkedList<String>() {
buildBaseUrl(agent) + "/test/submitScenarioWithParams/"
+ agentRunId, "files[]", paramFiles,
"scenarioModel", new LinkedList<String>() {
private static final long serialVersionUID = 1L;
{
add(modelCOntent);
add(modelContent);
}
});
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (Exception e) {
@ -139,29 +124,6 @@ public class AgentMessenger {
}
}
public AgentBriefStatusModel brief(Agent agent, UUID agentRunId) {
HttpResponse httpResponse = null;
try {
if (agent == null || agentRunId == null) {
return null;
}
httpResponse = this.httpRequester.sendGet(buildBaseUrl(agent)
+ "/test/brief/" + agentRunId, null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
logger.info(httpResponse.getContent());
return (AgentBriefStatusModel) MarshalHelper.unmarshal(
AgentBriefStatusModel.class, httpResponse.getContent());
} catch (Exception e) {
logIt(httpResponse, e);
logger.error(e.toString() + " When brief the agent with hostName "
+ agent.getHostName());
FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance();
return null;
}
}
public StopTestModel stop(Agent agent, UUID runId) {
HttpResponse httpResponse;
try {
@ -187,61 +149,6 @@ public class AgentMessenger {
return agent.getHostName() + ":" + agent.getPort();
}
public AgentBehaviorsBriefModel behaviorsBrief(Agent agent, UUID runId) {
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
buildBaseUrl(agent) + "/test/behaviorsBrief/"
+ runId.toString(), null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
logger.info(httpResponse.getContent());
return (AgentBehaviorsBriefModel) MarshalHelper.unmarshal(
AgentBehaviorsBriefModel.class, httpResponse.getContent());
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e) + " When behaviorsBrief");
FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance();
return null;
}
}
public AgentPageBriefModel pageBrief(Agent agent, UUID runId, int pageId) {
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
buildBaseUrl(agent) + "/test/pageBrief/" + runId.toString()
+ "/" + pageId, null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
return (AgentPageBriefModel) MarshalHelper.unmarshal(
AgentPageBriefModel.class, httpResponse.getContent());
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance();
return null;
}
}
public AgentPagesBriefModel pagesBrief(Agent agent, UUID runId) {
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
buildBaseUrl(agent) + "/test/pagesBrief/"
+ runId.toString(), null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
return null;
}
logger.info(httpResponse.getContent());
return (AgentPagesBriefModel) MarshalHelper.unmarshal(
AgentPagesBriefModel.class, httpResponse.getContent());
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance();
return null;
}
}
public ServerStatusModel askLiving(String hostName, int port) {
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(

View File

@ -11,6 +11,7 @@ import org.apache.log4j.Logger;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.share.models.agent.ServerStatusModel;
@ -20,7 +21,7 @@ import org.springframework.stereotype.Component;
@Component
public class HighAvailablePool extends CurrentLoadSubject {
private AgentService agentService;
private AgentRepository agentRepository;
private AgentMessenger agentMessenger;
private Map<String, Agent> pool;
private Map<String, ServerStatusModel> agentStatusOfPreviousBeatMap;
@ -38,11 +39,6 @@ public class HighAvailablePool extends CurrentLoadSubject {
this.pool = pool;
}
@Autowired
public void setAgentService(AgentService agentService) {
this.agentService = agentService;
}
private AgentMessenger getAgentMessenger() {
return agentMessenger;
}
@ -52,6 +48,15 @@ public class HighAvailablePool extends CurrentLoadSubject {
this.agentMessenger = agentMessenger;
}
private AgentRepository getAgentRepository() {
return agentRepository;
}
@Autowired
private void setAgentRepository(AgentRepository agentRepository) {
this.agentRepository = agentRepository;
}
public Long getMaxAvailableLoad() {
this.calculateHAPoolLoadStatusInMonopolize();
return maxAvailableLoad;
@ -102,7 +107,7 @@ public class HighAvailablePool extends CurrentLoadSubject {
@Scheduled(cron = "0,30 */1 * * * *")
public void timerTask() {
synchronized (this.agentService.getAgentLock()) {
synchronized (this.getAgentRepository().getAddDeleteLock()) {
synchronized (this.getPool()) {
heartBeatsAndUpdateHAPool();
doSubstituteIfRequired();
@ -142,9 +147,10 @@ public class HighAvailablePool extends CurrentLoadSubject {
this.pool.clear();
this.setCurrentAvailableLoad(0);
this.setMaxAvailableLoad((long) 0);
for (Agent agent : this.agentService.loadAgentPoolFromDB()) {
for (Agent agent : this.getAgentRepository().loadEntities()) {
checkHeartBeat(agent);
}
this.getAgentRepository().Update(this.getPool().values());
}
public void checkHeartBeat(Agent agent) {
@ -174,14 +180,13 @@ public class HighAvailablePool extends CurrentLoadSubject {
private void updateAgentStatus(int status, Agent agent) {
agent.setCurrentStatus(status);
// TODO: update this all together to db
this.agentService.updateAgentStatus(status, agent.getHostName());
// this.getAgentRepository().update(agent);
}
private void doForHealth(ServerStatusModel newModel, Agent agent) {
List<UUID> agentUnfinishedRunIds = newModel.getRunningTests();
if (agentUnfinishedRunIds == null || agentUnfinishedRunIds.size() == 0) {
// TODO:remove this function
// doForInIdle(agent);
doForInIdle(agent);
return;
}
doForInRunning(agent, newModel);
@ -192,11 +197,11 @@ public class HighAvailablePool extends CurrentLoadSubject {
this.agentStatusOfPreviousBeatMap.put(agent.getHostName(), newModel);
}
// private void doForInIdle(Agent agent) {
//
// updateAgentStatus(AgentService.AGENT_STATUS_Idel, agent);
// this.agentService.resetAgent(agent);
// }
private void doForInIdle(Agent agent) {
agent.setCurrentStatus(AgentService.AGENT_STATUS_Idel);
agent.resetLoad();
// this.getAgentRepository().update(agent);
}
private List<UUID> queryUnfinishedTest(ServerStatusModel newModel,
String hostName) {

View File

@ -211,6 +211,9 @@ public class TestBase_MakeUpTestPlan extends TestBase {
protected void cleanUpForTestPlanRunning() {
TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy(
getTestPlanRunIdUuid());
if (testPlan == null) {
return;
}
testPlan.setCurrentStatus(TestPlanStatus.Complete.name());
RunningAgentDB runningAgent = extractRunningAgent(
testPlan.extracSpecifiedScript(getScriptId()),
@ -322,7 +325,8 @@ public class TestBase_MakeUpTestPlan extends TestBase {
protected void deleteTestPlan() {
this.getTestPlanRepository().detach(
this.getTestPlanRepository()
.getTestPlanInDomainBy(this.getTestPlanRunIdUuid()).getId());
.getTestPlanInDomainBy(this.getTestPlanRunIdUuid())
.getId());
}
protected ScriptBriefResultModel buildScriptBriefResultModel(int i) {

View File

@ -20,7 +20,6 @@ import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -32,7 +31,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class Test_AgentMessenger extends TestBase_MakeUpTestPlan {
private static final String FILE_SEPARATOR = System
.getProperty("file.separator");
private static final String Test_HOSTNAME = "http://133.133.12.4";
private static final String Test_HOSTNAME = "http://127.0.0.1";
private static final int Test_PORT = 6565;
private Logger logger = Logger.getLogger(Test_AgentMessenger.class);
private AgentMessenger agentMessenger;
@ -49,6 +48,7 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan {
@Test
public void testSubmitScenarioWithParamsAndRun() throws IOException,
JAXBException {
List<File> paramFiles = new ArrayList<File>();
int scriptId = getUserFirstScript(this.getUserRepository().getUser(
"admin"));
@ -68,12 +68,17 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan {
.unmarshal(RunScenarioModel.class,
FileUtils.readFileToString(scenarioFile));
inputModel.setPoolSize(20);
Agent agent = Agent.createAgentWithoutId(Test_HOSTNAME, Test_PORT);
RunScenarioResultModel model = this.getAgentMessenger()
.submitScenrioWithParams(agent, paramFiles, inputModel);
RunScenarioResultModel model = this.getAgentMessenger().bookTest(agent,
20);
assertNotNull(model);
assertNotNull(model.getRunId());
System.out.println(model.getRunId());
RunScenarioResultModel modelAfter = this.getAgentMessenger()
.submitScenrioWithParams(agent, model.getRunId(), paramFiles,
inputModel);
assertEquals(model.getRunId(), modelAfter.getRunId());
model = this.getAgentMessenger().runWithParams(agent, model.getRunId());
assertNotNull(model);
assertNotNull(model.getRunId());
@ -131,8 +136,4 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan {
cleanUpForTestPlanRunning();
}
@After
public void clear() {
cleanUpForTestPlanRunning();
}
}

View File

@ -1,67 +0,0 @@
package org.bench4q.master.test.controller;
import java.io.IOException;
import javax.xml.bind.JAXBException;
import org.bench4q.master.test.TestBase_MakeUpTestPlan;
import org.bench4q.master.test.testplan.TestPlanTester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.TestPlanModel;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestPlanControllerTest extends TestBase_MakeUpTestPlan {
private String url = BASE_URL + "/testPlan";
public TestPlanControllerTest() {
}
@Test
public void runTestPlanWithoutLogOn() throws JAXBException, IOException {
HttpResponse httpResponse = this.httpRequester.sendPostXml(this.url
+ "/runTestPlanWithTestPlanModel", MarshalHelper.marshal(
TestPlanModel.class,
createATestPlanWithOneScript(TestPlanTester.SCRIPTID1)), null);
System.out.println(httpResponse.getContent());
assertEquals(400, httpResponse.getCode());
}
@Test
public void runTestPlanWithNullMonitorList() throws IOException,
JAXBException {
HttpResponse httpResponse = this.httpRequester
.sendPostXml(
this.url + "/runTestPlanWithTestPlanModel",
MarshalHelper
.marshal(
TestPlanModel.class,
TestPlanTester
.createATestPlanWithNullMonitorList(TestPlanTester.SCRIPTID1)),
makeAccessTockenMap(accessTocken));
System.out.println(httpResponse.getContent());
assertEquals(200, httpResponse.getCode());
}
public void runTestPlanWithZeroMonitor() throws IOException, JAXBException {
HttpResponse httpResponse = this.httpRequester
.sendPostXml(
this.url + "/runTestPlanWithTestPlanModel",
MarshalHelper
.marshal(
TestPlanModel.class,
TestPlanTester
.createATestPlanWithZeroMonitor(TestPlanTester.SCRIPTID1)),
makeAccessTockenMap(accessTocken));
System.out.println(httpResponse.getContent());
assertEquals(200, httpResponse.getCode());
}
@Test
public void testLoadTestPlans() throws IOException, JAXBException{
HttpResponse httpResponse = this.httpRequester.sendGet(this.url
+ "/loadTestPlans",null, this.makeAccessTockenMap(this.login()));
System.out.println(httpResponse.getContent());
}
}

View File

@ -1,4 +1,4 @@
package org.bench4q.master.test.testplan;
package org.bench4q.master.test.controller;
import static org.junit.Assert.*;
@ -15,7 +15,6 @@ import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.bench4q.master.test.TestBase_MakeUpTestPlan;
import org.bench4q.master.test.controller.TestBase;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.enums.master.TestPlanStatus;
@ -36,7 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class TestPlanTester extends TestBase_MakeUpTestPlan {
public class Test_TestPlanController extends TestBase_MakeUpTestPlan {
private TestPlanModel testPlanBusinessModel = new TestPlanModel();
private String _url = TestBase.BASE_URL + "/testPlan";
private int scriptSumNum;
@ -62,7 +61,7 @@ public class TestPlanTester extends TestBase_MakeUpTestPlan {
}
@SuppressWarnings("resource")
public TestPlanTester() {
public Test_TestPlanController() {
new ClassPathXmlApplicationContext("classpath:service-test-context.xml");
}

View File

@ -1,4 +1,4 @@
package org.bench4q.master.test.testplan;
package org.bench4q.master.test.controller;
import static org.junit.Assert.*;
@ -6,13 +6,12 @@ import java.io.IOException;
import javax.xml.bind.JAXBException;
import org.bench4q.master.test.controller.TestBase;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.ResultLoadModel;
import org.junit.Test;
public class TestTestPlanResult extends TestBase {
public class Test_TestPlanResultController extends TestBase {
private static String URL = BASE_URL + "/TestPlanResult";
private static String testPlanRunId = "0cdc3398-5b61-4bff-be48-8075e5d2fa64";

View File

@ -1,4 +1,4 @@
package org.bench4q.master.test.testplan;
package org.bench4q.master.test.domain.testPlan;
import static org.junit.Assert.*;

View File

@ -86,11 +86,6 @@ public class Test_UserRepository {
assertNotNull(this.getUserRepository().getUser("test1"));
}
@Test
public void testGetEntity() {
assertNotNull(this.getUserRepository().getEntity(5));
}
@Test
public void testUpdateEntity() {
// This test can reference to the test Of the

View File

@ -4,6 +4,7 @@ import java.util.Date;
import org.bench4q.master.Main;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.test.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus;
@ -38,12 +39,6 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan {
testForStatus(status);
}
@Test
public void testInRunning() throws Exception {
testForStatus(TestPlanStatus.InRunning);
}
@Test
public void testRunTestPlanRightly() throws InterruptedException {
assertNotNull(this.getAgentMessenger().askLiving(Test_AGENT_HOSTNAME,
@ -52,6 +47,7 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan {
int scriptId = getUserFirstScript(user);
this.getHaPool().timerTask();
Date dateBeforeRun = new Date();
Thread.sleep(1000);
this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith(
createATestPlanWithOneScript(scriptId), user));
assertNotNull(getTestPlanRunIdUuid());
@ -65,7 +61,10 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan {
TestPlanStatus.valueOf(getTestPlanRepository()
.getRunningTestPlanBy(getTestPlanRunIdUuid())
.getCurrentStatus()));
for (TestPlanScript testPlanScript : testPlan.getTestPlanScripts()) {
testPlanScript.doForComplete();
}
this.getTestPlanEngine().doTaskComplete(getTestPlanRunIdUuid());
}
@Test

View File

@ -48,17 +48,6 @@ public class Test_TestPlanScriptService extends TestBase_MakeUpTestPlan {
assertTrue(scriptBriefResultModels.size() == 0);
}
@Test
public void testLoadScriptResultRightly() {
TestPlanScript testPlanScript = this.getTestPlanScriptService()
.getTestPlanScript(getScriptId(), getTestPlanRunIdUuid());
assertNotNull(testPlanScript);
List<TestPlanScriptResult> scriptBriefList = this
.getTestPlanScriptService().queryScriptBriefResults(
testPlanScript);
assertTrue(scriptBriefList.size() > 0);
}
@Test
public void testSaveScriptBriefResultWithWrongScriptIdOrRunId() {
ScriptBriefResultModel briefResultModel = new ScriptBriefResultModel();

View File

@ -83,7 +83,7 @@ public class Test_TestPlanService extends TestBase_MakeUpTestPlan {
submitATestPlanWithTwoScript();
int sizeAfterSubmitTestPlanWithTwoScript = this.getTestPlanRepository()
.loadEntities(user).size();
assertEquals(initialTestPlanSize + 2,
assertEquals(initialTestPlanSize + 1,
sizeAfterSubmitTestPlanWithTwoScript);
}

View File

@ -63,7 +63,7 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan {
assertNotNull(runningAgents);
assertNotNull(runningAgents.get(0));
assertNull(runningAgents.get(0).getAgentRunId());
assertNotNull(runningAgents.get(0).getAgentRunId());
this.setAgentRunId(runningAgents.get(0).getAgentRunId());
assertEquals(EACH_SCRIPT_LOAD_SMALLSCALE, runningAgents.get(0)
.getLoadInUse());