diff --git a/src/main/java/org/bench4q/master/Main.java b/src/main/java/org/bench4q/master/Main.java index 9fa67ab7..0385e188 100644 --- a/src/main/java/org/bench4q/master/Main.java +++ b/src/main/java/org/bench4q/master/Main.java @@ -12,6 +12,7 @@ import org.apache.log4j.Logger; */ public class Main { private static Logger logger = Logger.getLogger(Main.class); + public static int Min_Sample_Cycle_InSecond = 10; public static int MAX_FAIL_TIMES = 10; public static int MIN_EXECUTE_INTERVAL_IN_SECONDS = 600; public static int PICK_CYCLE_IN_SECONDS = 60; @@ -42,11 +43,14 @@ public class Main { .getProperty("minExcuteIntervalInSeconds")); PICK_CYCLE_IN_SECONDS = Integer.parseInt(prop .getProperty("pickTestPlanCycleInSeconds")); + Min_Sample_Cycle_InSecond = Integer.parseInt(prop + .getProperty("minSampleCycleInSeconds")); } catch (Exception e) { portToUse = 8080; MAX_FAIL_TIMES = 10; MIN_EXECUTE_INTERVAL_IN_SECONDS = 600; PICK_CYCLE_IN_SECONDS = 60; + Min_Sample_Cycle_InSecond = 10; logger.error("There is no config file for port to serve! where path is " + configFile); } diff --git a/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java b/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java index 6a8d0115..cb53ec1e 100644 --- a/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java +++ b/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java @@ -261,7 +261,7 @@ public class TestPlanScript extends Observable implements notifyObserver(getSampler().getPagesBrief()); notifyObserver(getSampler().getBehaviorsBrief()); } - }, 0, 10000); + }, 0, this.getTestPlan().getSampleCycleInSeconds() * 1000); } private void notifyObserver(SampleModel sample) { diff --git a/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java b/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java index e5c87a60..5bcd8990 100644 --- a/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java +++ b/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java @@ -7,11 +7,16 @@ import java.util.List; import java.util.Set; import java.util.UUID; +import javax.xml.bind.JAXBException; + +import org.apache.log4j.Logger; +import org.bench4q.master.Main; import org.bench4q.master.domain.entity.Monitor; import org.bench4q.master.domain.entity.PlanedConfig; import org.bench4q.master.domain.entity.RunningAgentDB; import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlanScript; +import org.bench4q.master.domain.entity.TestPlanScriptResult; import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.interfaces.RunningAgentInterface; import org.bench4q.master.domain.repository.AgentRepository; @@ -22,17 +27,19 @@ import org.bench4q.master.domain.testplan.LoadDistribute; import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.share.enums.master.TestPlanStatus; +import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.RunningScriptModel; import org.bench4q.share.models.master.TestPlanModel; import org.bench4q.share.models.master.TestScriptConfig; +import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class TestPlanFactory { - private static final int Min_Sample_Cycle_InSecond = 10; + private Logger logger = Logger.getLogger(TestPlanFactory.class); private ScriptService scriptService; private AgentRepository agentRepository; @@ -96,7 +103,7 @@ public class TestPlanFactory { private int getLargerOneBetweenUserOptionAndMinInSystem( TestPlanModel testPlanModel) { - return testPlanModel.getSampleCycleInSeconds() <= Min_Sample_Cycle_InSecond ? Min_Sample_Cycle_InSecond + return testPlanModel.getSampleCycleInSeconds() <= Main.Min_Sample_Cycle_InSecond ? Main.Min_Sample_Cycle_InSecond : testPlanModel.getSampleCycleInSeconds(); } @@ -118,6 +125,22 @@ public class TestPlanFactory { return planedConfig; } + public TestPlanScriptResult createScriptResultWithOutId(Object model, + TestPlanScript testPlanScript, Date createDatetime) { + TestPlanScriptResult result = new TestPlanScriptResult(); + result.setTestPlanScript(testPlanScript); + try { + result.setResultContent(MarshalHelper.marshal(model.getClass(), + model)); + result.setResultType(model.getClass().getName()); + } catch (JAXBException e) { + logger.error(ExceptionLog.getStackTrace(e)); + result.setResultContent(""); + } + result.setCreateDatetime(createDatetime); + return result; + } + public TestPlan convertToDomain(TestPlan testPlanInRepo) { if (testPlanInRepo == null) { return null; diff --git a/src/main/java/org/bench4q/master/domain/repository/TestPlanRepository.java b/src/main/java/org/bench4q/master/domain/repository/TestPlanRepository.java index c435e63b..8667e2e3 100644 --- a/src/main/java/org/bench4q/master/domain/repository/TestPlanRepository.java +++ b/src/main/java/org/bench4q/master/domain/repository/TestPlanRepository.java @@ -102,6 +102,13 @@ public class TestPlanRepository extends AbstractRepositoty { return this.getTestPlanFactory().convertToDomain(result); } + public TestPlan doGetTestPlanBy(Session session, UUID testPlanRunId) { + return (TestPlan) session + .createCriteria(TestPlan.class) + .add(Restrictions.eq("testPlanRunId", testPlanRunId.toString())) + .uniqueResult(); + } + public List loadEntities(User user) { Session session = this.getSessionHelper().openSession(); try { @@ -154,6 +161,17 @@ public class TestPlanRepository extends AbstractRepositoty { } } + public void doUpdateEntity(Session session, TestPlan testPlan) { + Transaction transaction = session.beginTransaction(); + try { + session.update(testPlan); + transaction.commit(); + } catch (Exception e) { + transaction.rollback(); + logger.error(ExceptionLog.getStackTrace(e)); + } + } + public TestPlan getRunningTestPlanBy(UUID testPlanRunId) { return this.getRunningTestPlans().get(testPlanRunId); } diff --git a/src/main/java/org/bench4q/master/domain/service/TestPlanScriptService.java b/src/main/java/org/bench4q/master/domain/service/TestPlanScriptService.java index 47fe0dbc..2c86fed7 100644 --- a/src/main/java/org/bench4q/master/domain/service/TestPlanScriptService.java +++ b/src/main/java/org/bench4q/master/domain/service/TestPlanScriptService.java @@ -7,25 +7,18 @@ import java.util.Observable; import java.util.Observer; import java.util.UUID; -import javax.xml.bind.JAXBException; - import org.apache.log4j.Logger; -import org.bench4q.master.domain.entity.Script; import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlanScript; import org.bench4q.master.domain.entity.TestPlanScriptResult; +import org.bench4q.master.domain.factory.TestPlanFactory; import org.bench4q.master.domain.interfaces.RunningScriptInterface; import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.helper.SessionHelper; -import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; -import org.bench4q.share.models.master.statistics.ScriptPageBriefModel; -import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel; import org.bench4q.share.models.master.statistics.SampleModel; import org.hibernate.Session; -import org.hibernate.Transaction; import org.hibernate.criterion.Restrictions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -35,6 +28,7 @@ public class TestPlanScriptService implements Observer { private static Logger logger = Logger.getLogger(TestPlanService.class); private SessionHelper sessionHelper; private TestPlanRepository testPlanRepository; + private TestPlanFactory testPlanFactory; private SessionHelper getSessionHelper() { return sessionHelper; @@ -54,6 +48,15 @@ public class TestPlanScriptService implements Observer { this.testPlanRepository = testPlanRepository; } + private TestPlanFactory getTestPlanFactory() { + return testPlanFactory; + } + + @Autowired + private void setTestPlanFactory(TestPlanFactory testPlanFactory) { + this.testPlanFactory = testPlanFactory; + } + public TestPlanScript getTestPlanScript(int scriptId, UUID testPlanRunId) { TestPlan testPlan = this.getTestPlanRepository().getTestPlanBy( testPlanRunId); @@ -61,65 +64,23 @@ public class TestPlanScriptService implements Observer { .extracSpecifiedScript(scriptId); } - public boolean saveScriptBriefResult(final UUID testPlanRunId, - final int scriptId, final ScriptBriefResultModel resultModel) { - return saveResultPrivate(testPlanRunId, scriptId, resultModel); - } - - public boolean saveBehaviorsBriefResult(final UUID testPlanRunId, - final int scriptId, - final ScriptBehaviorsBriefModel scriptBehaviorsBriefModel) { - return saveResultPrivate(testPlanRunId, scriptId, - scriptBehaviorsBriefModel); - } - - public boolean savePageBriefResult(final UUID testPlanRunId, - final int scriptId, final ScriptPageBriefModel pageBriefModel) { - return saveResultPrivate(testPlanRunId, scriptId, pageBriefModel); - } - - public boolean savePagesBriefResult(final UUID testPlanRunId, - final int scriptId, final ScriptPagesBriefModel pagesBriefModel) { - return saveResultPrivate(testPlanRunId, scriptId, pagesBriefModel); - } - - private boolean saveResultPrivate(final UUID testPlanRunId, - final int scriptId, final Object resultModel) { - doSaveResult(testPlanRunId, scriptId, resultModel, new Date()); - return true; - } - - private void doSaveResult(UUID testPlanRunId, int scriptId, + public void doSaveResult(UUID testPlanRunId, int scriptId, Object resultModel, Date now) { Session session = this.getSessionHelper().openSession(); - Transaction transaction = session.beginTransaction(); try { - TestPlan testPlanDB = (TestPlan) session - .createCriteria(TestPlan.class) - .add(Restrictions.eq("testPlanRunId", - testPlanRunId.toString())).uniqueResult(); - if (testPlanDB == null) { + TestPlan testPlan = this.getTestPlanRepository().doGetTestPlanBy( + session, testPlanRunId); + if (testPlan == null) { return; } - Script script = (Script) session.createCriteria(Script.class) - .add(Restrictions.eq("id", scriptId)).uniqueResult(); - if (script == null) { - return; - } - - TestPlanScript testPlanScript = testPlanDB - .extracSpecifiedScript(script.getId()); - if (testPlanScript == null) { - return; - } - session.merge(buildScriptResultWithModel(resultModel, - testPlanScript, now)); - transaction.commit(); - return; + TestPlanScript specifiedScript = testPlan + .extracSpecifiedScript(scriptId); + specifiedScript.getTestPlanScriptResults().add( + this.getTestPlanFactory().createScriptResultWithOutId( + resultModel, specifiedScript, now)); + this.getTestPlanRepository().doUpdateEntity(session, testPlan); } catch (Exception e) { - e.printStackTrace(); - transaction.rollback(); - return; + logger.error(ExceptionLog.getStackTrace(e)); } finally { if (session != null) { session.close(); @@ -127,22 +88,6 @@ public class TestPlanScriptService implements Observer { } } - private TestPlanScriptResult buildScriptResultWithModel(Object model, - TestPlanScript testPlanScript, Date createDatetime) { - TestPlanScriptResult result = new TestPlanScriptResult(); - result.setTestPlanScript(testPlanScript); - try { - result.setResultContent(MarshalHelper.marshal(model.getClass(), - model)); - result.setResultType(model.getClass().getName()); - } catch (JAXBException e) { - logger.error(ExceptionLog.getStackTrace(e)); - result.setResultContent(""); - } - result.setCreateDatetime(createDatetime); - return result; - } - public Collection queryTestPlanScripts(UUID testPlanRunId) { return this.getTestPlanRepository().getTestPlanBy(testPlanRunId) .getTestPlanScripts(); diff --git a/src/main/resources/org/bench4q/master/config/ServerPort.properties b/src/main/resources/org/bench4q/master/config/ServerPort.properties index 3729bdf6..c04e4a9a 100644 --- a/src/main/resources/org/bench4q/master/config/ServerPort.properties +++ b/src/main/resources/org/bench4q/master/config/ServerPort.properties @@ -1,4 +1,5 @@ portToServe=7979 pickTestPlanCycleInSeconds=60 maxFailTime=10 -minExcuteIntervalInSeconds=600 \ No newline at end of file +minExcuteIntervalInSeconds=600 +minSampleCycleInSeconds=10 \ No newline at end of file diff --git a/src/test/java/org/bench4q/master/test/controller/TestPlanScriptResultControllerTest.java b/src/test/java/org/bench4q/master/test/controller/TestPlanScriptResultControllerTest.java index 61d7da0b..9203cdb5 100644 --- a/src/test/java/org/bench4q/master/test/controller/TestPlanScriptResultControllerTest.java +++ b/src/test/java/org/bench4q/master/test/controller/TestPlanScriptResultControllerTest.java @@ -3,6 +3,7 @@ package org.bench4q.master.test.controller; import static org.junit.Assert.*; import java.io.IOException; +import java.util.Date; import java.util.UUID; import javax.xml.bind.JAXBException; @@ -44,21 +45,20 @@ public class TestPlanScriptResultControllerTest extends TestBase_MakeUpTestPlan UUID testPlanRunId = UUID.randomUUID(); User user = this.getUserRepository().getUser("admin"); this.setScriptId(this.getUserFirstScript(user)); - TestPlanModel model = createATestPlanWithOneScript(this - .getScriptId()); + TestPlanModel model = createATestPlanWithOneScript(this.getScriptId()); // TestPlanInBusiness testPlan = // BusinessModelMapFactory.toBusiness(model); this.getTestPlanService().submitTestPlan(model, user, testPlanRunId); this.setTestPlanRunIdUuid(testPlanRunId); for (int i = 0; i < 3; i++) { - this.getTestPlanScriptService().saveScriptBriefResult( - testPlanRunId, this.getScriptId(), - buildScriptBriefResultModel(i)); - this.getTestPlanScriptService().saveBehaviorsBriefResult( - testPlanRunId, this.getScriptId(), - new ScriptBehaviorsBriefModel()); - this.getTestPlanScriptService().savePageBriefResult(testPlanRunId, - this.getScriptId(), new ScriptPageBriefModel()); + this.getTestPlanScriptService().doSaveResult(testPlanRunId, + this.getScriptId(), buildScriptBriefResultModel(i), + new Date()); + this.getTestPlanScriptService().doSaveResult(testPlanRunId, + this.getScriptId(), new ScriptBehaviorsBriefModel(), + new Date()); + this.getTestPlanScriptService().doSaveResult(testPlanRunId, + this.getScriptId(), new ScriptPageBriefModel(), new Date()); } } diff --git a/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptResultService.java b/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptResultService.java index 3ede36b3..dc34afb8 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptResultService.java +++ b/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptResultService.java @@ -2,6 +2,7 @@ package org.bench4q.master.test.service; import static org.junit.Assert.*; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -118,14 +119,15 @@ public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan { this.getTestPlanService().submitTestPlan(model, user, testPlanRunId); this.setTestPlanRunIdUuid(testPlanRunId); for (int i = 0; i < 3; i++) { - this.getTestPlanScriptService().saveScriptBriefResult( - testPlanRunId, this.getScriptId(), - buildScriptBriefResultModel(i)); - this.getTestPlanScriptService().saveBehaviorsBriefResult( - testPlanRunId, this.getScriptId(), - new ScriptBehaviorsBriefModel()); - this.getTestPlanScriptService().savePagesBriefResult(testPlanRunId, - this.getScriptId(), makeUpScriptPagesBriefModel(i)); + this.getTestPlanScriptService().doSaveResult(testPlanRunId, + this.getScriptId(), buildScriptBriefResultModel(i), + new Date()); + this.getTestPlanScriptService().doSaveResult(testPlanRunId, + this.getScriptId(), new ScriptBehaviorsBriefModel(), + new Date()); + this.getTestPlanScriptService().doSaveResult(testPlanRunId, + this.getScriptId(), makeUpScriptPagesBriefModel(i), + new Date()); } } diff --git a/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptService.java b/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptService.java index daf61e3b..7937e0a9 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptService.java +++ b/src/test/java/org/bench4q/master/test/service/Test_TestPlanScriptService.java @@ -2,6 +2,7 @@ package org.bench4q.master.test.service; import static org.junit.Assert.*; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -26,9 +27,8 @@ public class Test_TestPlanScriptService extends TestBase_MakeUpTestPlan { public void prepare() { submitATestPlanWithOneScript(); System.out.println(getScriptId()); - this.getTestPlanScriptService().saveScriptBriefResult( - getTestPlanRunIdUuid(), getScriptId(), - new ScriptBriefResultModel()); + this.getTestPlanScriptService().doSaveResult(getTestPlanRunIdUuid(), + getScriptId(), new ScriptBriefResultModel(), new Date()); } @After @@ -66,9 +66,9 @@ public class Test_TestPlanScriptService extends TestBase_MakeUpTestPlan { briefResultModel.setFailRateThisTime(0); briefResultModel.setFailThroughputThisTime(0); briefResultModel.setFinished(true); - this.getTestPlanScriptService().saveScriptBriefResult( + this.getTestPlanScriptService().doSaveResult( UUID.fromString(WRONG_TEST_PLAN_RUN_ID), WRONG_SCRIPT_ID, - briefResultModel); + briefResultModel, new Date()); TestPlanScript testPlanScript = this.getTestPlanScriptService() .getTestPlanScript(WRONG_SCRIPT_ID, UUID.fromString(WRONG_TEST_PLAN_RUN_ID));