diff --git a/src/main/java/org/bench4q/master/api/TestPlanController.java b/src/main/java/org/bench4q/master/api/TestPlanController.java index a722c6e5..e244b697 100644 --- a/src/main/java/org/bench4q/master/api/TestPlanController.java +++ b/src/main/java/org/bench4q/master/api/TestPlanController.java @@ -19,6 +19,7 @@ import org.bench4q.master.domain.TestPlanInBusiness; import org.bench4q.master.entity.TestPlanDB; import org.bench4q.master.exception.Bench4QException; import org.bench4q.master.report.ReportService; +import org.bench4q.master.service.infrastructure.TestPlanScriptResultService; import org.bench4q.master.service.infrastructure.TestPlanService; import org.bench4q.master.service.infrastructure.UserService; import org.bench4q.master.testplan.TestPlanContainer; @@ -26,6 +27,7 @@ import org.bench4q.master.testplan.TestPlanEngine; import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.RunningScriptModel; +import org.bench4q.share.models.master.TestPlanScriptBrieResultModel; import org.bench4q.share.models.master.TestPlanBusinessModel; import org.bench4q.share.models.master.TestPlanDBModel; import org.bench4q.share.models.master.TestPlanResponseModel; @@ -55,6 +57,8 @@ public class TestPlanController extends BaseController { private TestPlanContainer testPlanContainer; private TestPlanService testPlanService; private ReportService reportService; + private TestPlanScriptResultService testPlanScriptResultService; + private BusinessModelMapFactory businessMapFactory; private Logger logger; private TestPlanEngine getTestPlanRunner() { @@ -93,6 +97,26 @@ public class TestPlanController extends BaseController { this.reportService = reportService; } + private BusinessModelMapFactory getBusinessMapFactory() { + return businessMapFactory; + } + + @Autowired + private void setBusinessMapFactory( + BusinessModelMapFactory businessMapFactory) { + this.businessMapFactory = businessMapFactory; + } + + private TestPlanScriptResultService getTestPlanScriptResultService() { + return testPlanScriptResultService; + } + + @Autowired + private void setTestPlanScriptResultService( + TestPlanScriptResultService testPlanScriptResultService) { + this.testPlanScriptResultService = testPlanScriptResultService; + } + private Logger getLogger() { return logger; } @@ -166,8 +190,8 @@ public class TestPlanController extends BaseController { List runningScriptModels = new ArrayList(); for (RunningScript runningScript : testPlanContext.getAllScript()) { - runningScriptModels.add(BusinessModelMapFactory - .toModel(runningScript)); + runningScriptModels.add(this.getBusinessMapFactory().toModel( + runningScript)); } List monitorModels = new ArrayList(); for (MonitorInBusiness monitorInBusiness : testPlanContext @@ -180,35 +204,27 @@ public class TestPlanController extends BaseController { runningScriptModels, monitorModels); } - @RequestMapping(value = "/scriptBrief/{testPlanId}/{scriptId}", method = RequestMethod.GET) + @RequestMapping(value = "/scriptBrief/{testPlanId}/{scriptId}/{duationBegin}", method = RequestMethod.GET) @ResponseStatus(value = HttpStatus.OK) @ResponseBody - public ScriptBriefResultModel getScriptBrief(@PathVariable UUID testPlanId, - @PathVariable int scriptId) throws Bench4QException, + public TestPlanScriptBrieResultModel getScriptBrief( + @PathVariable UUID testPlanId, @PathVariable int scriptId, + @PathVariable long duationBegin) throws Bench4QException, NullPointerException { if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { throw new Bench4QException(HAVE_NO_POWER, "You have not power to get test plan script brief", "/getRunningInfo"); } - ScriptBriefResultModel ret = new ScriptBriefResultModel(); if (!this.getTestPlanContainer().isContextExist(testPlanId)) { throw new Bench4QException(CONTEXT_NOT_EXIST, THE_CONTEXT_IS_NULL_WHOSE_ID_IS, "/getScriptBrief"); } - TestPlanContext context = this.getTestPlanContainer() - .queryTestPlanContext(testPlanId); - if (context.isFinish()) { - ret.setFinished(true); - return ret; - } - RunningScript runningScript = context.queryRunningScript(scriptId); - if (runningScript == null || runningScript.isFinished()) { - ret.setFinished(true); - return ret; - } - ret = runningScript.getScriptBrief(); - ret.setPlanedRunningTime(runningScript.getConfig().getExecuteRange()); + List scriptBriefResultModels = this + .getTestPlanScriptResultService().loadScriptBriefWithDuation( + testPlanId, scriptId, duationBegin); + TestPlanScriptBrieResultModel ret = new TestPlanScriptBrieResultModel(); + ret.setScriptBriefResultModels(scriptBriefResultModels); return ret; } diff --git a/src/main/java/org/bench4q/master/api/modelfactory/BusinessModelMapFactory.java b/src/main/java/org/bench4q/master/api/modelfactory/BusinessModelMapFactory.java index 8da18f71..e0bd0c87 100644 --- a/src/main/java/org/bench4q/master/api/modelfactory/BusinessModelMapFactory.java +++ b/src/main/java/org/bench4q/master/api/modelfactory/BusinessModelMapFactory.java @@ -12,6 +12,7 @@ import org.bench4q.master.entity.Port; import org.bench4q.master.entity.Script; import org.bench4q.master.entity.TestPlanDB; import org.bench4q.master.entity.User; +import org.bench4q.master.service.infrastructure.ScriptService; import org.bench4q.share.models.master.AgentModel; import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.PortModel; @@ -21,8 +22,21 @@ import org.bench4q.share.models.master.ScriptModel; import org.bench4q.share.models.master.TestPlanBusinessModel; import org.bench4q.share.models.master.TestPlanDBModel; import org.bench4q.share.models.master.UserModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +@Component public class BusinessModelMapFactory { + private ScriptService scriptService; + + private ScriptService getScriptService() { + return scriptService; + } + + @Autowired + private void setScriptService(ScriptService scriptService) { + this.scriptService = scriptService; + } public static Agent toBusiness(AgentModel agentModel) { Agent agent = new Agent(); @@ -96,16 +110,20 @@ public class BusinessModelMapFactory { return runningAgents; } - public static RunningScriptModel toModel(RunningScript runningScript) { + public RunningScriptModel toModel(RunningScript runningScript) { RunningScriptModel ret = new RunningScriptModel(); ret.setConfig(runningScript.getConfig()); ret.setFinished(runningScript.isFinished()); ret.setRequireLoad(runningScript.getRequireLoad()); List runningAgentModels = new ArrayList(); - for (RunningAgent runningAgent : runningScript.queryRunningAgentsUnModifiable()) { + for (RunningAgent runningAgent : runningScript + .queryRunningAgentsUnModifiable()) { runningAgentModels.add(BusinessModelMapFactory .toModel(runningAgent)); } + ret.setScenarioModel(runningScript.getScenario()); + ret.setScriptName(this.getScriptService() + .getScript(runningScript.getScriptId()).getName()); ret.setRunningAgentModels(runningAgentModels); ret.setScriptId(runningScript.getScriptId()); ret.setTestPlanID(runningScript.getTestPlanID()); @@ -132,15 +150,13 @@ public class BusinessModelMapFactory { return ret; } - public static TestPlanBusinessModel toModel( - TestPlanInBusiness testPlanInBusiness) { + public TestPlanBusinessModel toModel(TestPlanInBusiness testPlanInBusiness) { TestPlanBusinessModel ret = new TestPlanBusinessModel(); ret.setName(testPlanInBusiness.getName()); List runningScriptModels = new ArrayList(); for (RunningScript runningScript : testPlanInBusiness .getRunningScripts()) { - runningScriptModels.add(BusinessModelMapFactory - .toModel(runningScript)); + runningScriptModels.add(toModel(runningScript)); } ret.setRunningScriptModels(runningScriptModels); List monitorModels = new ArrayList(); diff --git a/src/main/java/org/bench4q/master/domain/RunningScript.java b/src/main/java/org/bench4q/master/domain/RunningScript.java index d60b04e6..7516d33f 100644 --- a/src/main/java/org/bench4q/master/domain/RunningScript.java +++ b/src/main/java/org/bench4q/master/domain/RunningScript.java @@ -225,12 +225,12 @@ public class RunningScript extends Observable { } public ScriptBriefResultModel getScriptBrief() { - for (RunningAgent runningAgent : this.getRunningAgents()) { + for (RunningAgent runningAgent : getRunningAgents()) { if (runningAgent.isBreakDown()) { continue; } - AgentBriefStatusModel briefStatusModel = this - .getRunningAgentService().brief(runningAgent.getAgent(), + AgentBriefStatusModel briefStatusModel = getRunningAgentService() + .brief(runningAgent.getAgent(), runningAgent.getAgentRunId()); if (briefStatusModel == null) { continue; diff --git a/src/main/java/org/bench4q/master/service/communication/TestPlanControllerService.java b/src/main/java/org/bench4q/master/service/communication/TestPlanControllerService.java new file mode 100644 index 00000000..860e3bf4 --- /dev/null +++ b/src/main/java/org/bench4q/master/service/communication/TestPlanControllerService.java @@ -0,0 +1,7 @@ +package org.bench4q.master.service.communication; + +import org.springframework.stereotype.Component; + +@Component +public class TestPlanControllerService { +} diff --git a/src/main/java/org/bench4q/master/service/infrastructure/ScriptService.java b/src/main/java/org/bench4q/master/service/infrastructure/ScriptService.java index 39db701b..ef67dac7 100644 --- a/src/main/java/org/bench4q/master/service/infrastructure/ScriptService.java +++ b/src/main/java/org/bench4q/master/service/infrastructure/ScriptService.java @@ -83,7 +83,7 @@ public class ScriptService { } } - private Script doGetScript(int scriptId, Session session) { + public Script doGetScript(int scriptId, Session session) { return (Script) session.createCriteria(Script.class) .add(Restrictions.eq("id", scriptId)).uniqueResult(); } diff --git a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptResultService.java b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptResultService.java index 786727a0..d7e19496 100644 --- a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptResultService.java +++ b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptResultService.java @@ -2,6 +2,7 @@ package org.bench4q.master.service.infrastructure; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -10,15 +11,19 @@ import javax.xml.bind.JAXBException; import org.apache.log4j.Logger; import org.bench4q.master.entity.TestPlanScriptResult; import org.bench4q.master.exception.ExceptionLog; +import org.bench4q.master.helper.SessionHelper; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.ValueTimeModel; import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; +import org.hibernate.Session; +import org.hibernate.criterion.Restrictions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class TestPlanScriptResultService { private TestPlanScriptService testPlanScriptService; + private SessionHelper sessionHelper; private static Logger logger = Logger .getLogger(TestPlanScriptResultService.class); @@ -32,6 +37,15 @@ public class TestPlanScriptResultService { this.testPlanScriptService = testPlanScriptService; } + public SessionHelper getSessionHelper() { + return sessionHelper; + } + + @Autowired + public void setSessionHelper(SessionHelper sessionHelper) { + this.sessionHelper = sessionHelper; + } + /** * Here I just use the Time that createDateTime in testplanscriptResult, If * it's not accuracy, i just need to modify the way of pick createDateTime @@ -84,4 +98,31 @@ public class TestPlanScriptResultService { return field; } + public List loadScriptBriefWithDuation( + UUID testPlanId, int scriptId, long startTime) { + Session session = this.getSessionHelper().openSession(); + List result = new ArrayList(); + try { + @SuppressWarnings("unchecked") + List scriptResults = (List) session + .createCriteria(TestPlanScriptResult.class) + .add(Restrictions.eq( + "testPlanScript", + this.getTestPlanScriptService() + .doGetTestPlanScript(scriptId, testPlanId, + session))) + .add(Restrictions.eq("resultType", + ScriptBriefResultModel.class.getName())) + .add(Restrictions.ge("createDatetime", new Date(startTime))) + .list(); + for (TestPlanScriptResult testPlanScriptResult : scriptResults) { + result.add((ScriptBriefResultModel) MarshalHelper.unmarshal( + ScriptBriefResultModel.class, + testPlanScriptResult.getResultContent())); + } + } catch (Exception e) { + logger.error(ExceptionLog.getExceptionStackTrace(e)); + } + return result; + } } diff --git a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptService.java b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptService.java index 3ba5076a..2884a5aa 100644 --- a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptService.java +++ b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptService.java @@ -66,13 +66,7 @@ public class TestPlanScriptService implements Observer { public TestPlanScript getTestPlanScript(int scriptId, UUID testPlanRunId) { Session session = this.getSessionHelper().openSession(); try { - return (TestPlanScript) session - .createCriteria(TestPlanScript.class) - .add(Restrictions.eq("script", this.getScriptService() - .getScript(scriptId))) - .add(Restrictions.eq("testPlanDB", - this.testPlanService.getTestPlan(testPlanRunId))) - .uniqueResult(); + return doGetTestPlanScript(scriptId, testPlanRunId, session); } catch (Exception e) { e.printStackTrace(); return null; @@ -83,6 +77,18 @@ public class TestPlanScriptService implements Observer { } } + TestPlanScript doGetTestPlanScript(int scriptId, UUID testPlanRunId, + Session session) { + // TODO:refactor this to doGet... + return (TestPlanScript) session + .createCriteria(TestPlanScript.class) + .add(Restrictions.eq("script", this.getScriptService() + .getScript(scriptId))) + .add(Restrictions.eq("testPlanDB", + this.testPlanService.getTestPlan(testPlanRunId))) + .uniqueResult(); + } + public boolean saveScriptBriefResult(final UUID testPlanRunId, final int scriptId, final ScriptBriefResultModel resultModel) { return saveResultPrivate(testPlanRunId, scriptId, resultModel); diff --git a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanService.java b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanService.java index f1d95fcb..264aa00d 100644 --- a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanService.java +++ b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanService.java @@ -30,6 +30,7 @@ import org.springframework.stereotype.Component; public class TestPlanService { private SessionHelper sessionHelper; private ScriptService scriptService; + private BusinessModelMapFactory businessMapFactory; private static Logger logger = Logger.getLogger(TestPlanService.class); public static final long TIME_UNIT = 1000; @@ -47,6 +48,16 @@ public class TestPlanService { this.scriptService = scriptService; } + private BusinessModelMapFactory getBusinessMapFactory() { + return businessMapFactory; + } + + @Autowired + private void setBusinessMapFactory( + BusinessModelMapFactory businessMapFactory) { + this.businessMapFactory = businessMapFactory; + } + public boolean saveTestPlanToDB(final TestPlanInBusiness testPlanInParam, final User user, final UUID testPlanRunId, final Collection collection) { @@ -57,8 +68,8 @@ public class TestPlanService { testPlanInSession = saveToTestPlan(session, testPlanInParam.getName(), MarshalHelper.marshal( TestPlanBusinessModel.class, - BusinessModelMapFactory.toModel(testPlanInParam)), - user, testPlanRunId); + this.getBusinessMapFactory().toModel( + testPlanInParam)), user, testPlanRunId); if (testPlanInSession == null) { return false; } diff --git a/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptResultService.java b/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptResultService.java index ed9c1049..924dd1b0 100644 --- a/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptResultService.java +++ b/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptResultService.java @@ -54,7 +54,8 @@ public class TestTestPlanScriptResultService extends TestBase_MakeUpTestPlan { @Test public void testLoadAvgFromScriptBriefResult() throws NoSuchFieldException, - SecurityException { + SecurityException, InterruptedException { + Thread.sleep(5000); List results = this.getTestPlanScriptResultService() .loadScriptBriefSpecificField( this.getTestPlanRunIdUuid(), @@ -66,6 +67,15 @@ public class TestTestPlanScriptResultService extends TestBase_MakeUpTestPlan { assertTrue(results.get(1).getValue() % 100 == 0); } + @Test + public void testLoadScriptBriefWithDuation() throws InterruptedException { + Thread.sleep(5000); + List results = this + .getTestPlanScriptResultService().loadScriptBriefWithDuation( + this.getTestPlanRunIdUuid(), this.getScriptId(), 0); + assertEquals(3, results.size()); + } + @Before public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult() { UUID testPlanRunId = UUID.randomUUID(); diff --git a/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java b/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java index 9a4009d5..caed1218 100644 --- a/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java +++ b/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java @@ -24,6 +24,7 @@ import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.RunningScriptModel; import org.bench4q.share.models.master.TestPlanBusinessModel; import org.bench4q.share.models.master.TestPlanResultModel; +import org.bench4q.share.models.master.TestPlanScriptBrieResultModel; import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; import org.bench4q.share.models.master.statistics.ScriptPageBriefModel; import org.junit.Test; @@ -81,7 +82,12 @@ public class TestPlanTester extends TestBase_MakeUpTestPlan { this.setScriptSumNum(1); this.setAccessTocken(this.login()); this.setTestPlanRunIdUuid(testWithTestPlanModel()); + TestPlanResultModel resultModel = this.getRunningInfo(this + .getTestPlanRunIdUuid()); + // System.out.println(MarshalHelper.marshal(TestPlanResultModel.class, + // resultModel)); Thread.sleep(60000); + this.getTestPlanReport(this.getTestPlanRunIdUuid()); } public void behaviorsBrief(UUID testPlanID, int scriptId) @@ -201,16 +207,19 @@ public class TestPlanTester extends TestBase_MakeUpTestPlan { return ret; } - public ScriptBriefResultModel getScriptBrief(UUID testPlanId, int scriptId) - throws IOException, JAXBException { + public TestPlanScriptBrieResultModel getScriptBrief(UUID testPlanId, + int scriptId) throws IOException, JAXBException { HttpResponse httpResponse = this.httpRequester.sendGet(this._url - + "/scriptBrief/" + testPlanId.toString() + "/" + scriptId, - null, createAccessTokenMap()); + + "/scriptBrief/" + testPlanId.toString() + "/" + scriptId + + "/" + 0, null, createAccessTokenMap()); System.out.println(httpResponse.getContent()); - ScriptBriefResultModel ret = (ScriptBriefResultModel) MarshalHelper - .unmarshal(ScriptBriefResultModel.class, + TestPlanScriptBrieResultModel ret = (TestPlanScriptBrieResultModel) MarshalHelper + .unmarshal(TestPlanScriptBrieResultModel.class, httpResponse.getContent()); - System.out.println(ret.getAverageResponseTime()); + for (ScriptBriefResultModel scriptBriefResultModel : ret + .getScriptBriefResultModels()) { + System.out.println(scriptBriefResultModel.getAverageResponseTime()); + } return ret; } diff --git a/src/test/resources/service-test-context.xml b/src/test/resources/service-test-context.xml index 799a586b..7deea251 100644 --- a/src/test/resources/service-test-context.xml +++ b/src/test/resources/service-test-context.xml @@ -8,7 +8,7 @@ http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> + base-package="org.bench4q.master.service, org.bench4q.master.helper, org.bench4q.master.testplan, org.bench4q.share, org.bench4q.master.api.modelfactory" />