From b97892229461763339d4ea11342af0a7416ec387 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Tue, 25 Feb 2014 20:14:02 +0800 Subject: [PATCH] refactor and add test about RunningScriptSampler --- .../bench4q/master/domain/RunningScript.java | 6 +- .../master/domain/entity/TestPlanScript.java | 32 +++- .../domain/factory/TestPlanFactory.java | 2 + .../domain/testplan/LoadDistribute.java | 1 - .../domain/testplan/RunningScriptSampler.java | 144 ++++++++++++++++++ .../master/test/TestBase_MakeUpTestPlan.java | 32 ++++ .../domain/Test_RunningScriptSampler.java | 68 +++++++++ .../master/test/domain/Test_TestPlan.java | 26 +--- .../test/testplan/Test_LoadDistribute.java | 3 +- 9 files changed, 279 insertions(+), 35 deletions(-) create mode 100644 src/main/java/org/bench4q/master/domain/testplan/RunningScriptSampler.java create mode 100644 src/test/java/org/bench4q/master/test/domain/Test_RunningScriptSampler.java diff --git a/src/main/java/org/bench4q/master/domain/RunningScript.java b/src/main/java/org/bench4q/master/domain/RunningScript.java index b6c59394..9bce043e 100644 --- a/src/main/java/org/bench4q/master/domain/RunningScript.java +++ b/src/main/java/org/bench4q/master/domain/RunningScript.java @@ -45,7 +45,7 @@ public class RunningScript extends Observable implements RunningScriptInterface private boolean finished; private TestScriptConfig config; private RunScenarioModel scenario; - private Set runningAgents = new HashSet(); + private Set runningAgents; private Timer timer; private List dataStatisticsList; @@ -192,10 +192,6 @@ public class RunningScript extends Observable implements RunningScriptInterface this.getScriptId())); } - public static boolean notValidScript(RunningScriptInterface input) { - return input == null || input.getRunningAgents() == null; - } - public Collection queryRunningAgentsUnModifiable() { return Collections.unmodifiableCollection(this.runningAgents); } 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 0ecb322d..3d9a5864 100644 --- a/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java +++ b/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java @@ -3,6 +3,8 @@ package org.bench4q.master.domain.entity; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; import java.util.UUID; import javax.persistence.CascadeType; @@ -23,6 +25,7 @@ import org.apache.log4j.Logger; import org.bench4q.master.domain.RunningAgent; import org.bench4q.master.domain.factory.TestPlanFactory; import org.bench4q.master.domain.interfaces.RunningScriptInterface; +import org.bench4q.master.domain.testplan.RunningScriptSampler; import org.bench4q.master.exception.ExceptionLog; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.agent.RunScenarioModel; @@ -38,6 +41,8 @@ public class TestPlanScript implements RunningScriptInterface { private Set runningAgents; private TestPlanFactory testPlanFactory; + private RunningScriptSampler sampler; + private Timer timer = new Timer(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -135,16 +140,37 @@ public class TestPlanScript implements RunningScriptInterface { this.testPlanFactory = testPlanFactory; } + @Transient + public RunningScriptSampler getSampler() { + return sampler; + } + + public void setSampler(RunningScriptSampler sampler) { + this.sampler = sampler; + } + + @Transient + private Timer getTimer() { + return timer; + } + public void doAfterDistributeLoad(List agentsAfterDistribute) { // TODO: finish this this.getRunningAgentsDB().addAll( this.getTestPlanFactory().createRunningAgentDBsWithoutId( agentsAfterDistribute, this)); - doBrief(); + doPeriodicalBrief(); } - private void doBrief() { - // TODO Auto-generated method stub + private void doPeriodicalBrief() { + this.getTimer().scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + getSampler().getScriptBrief(); + getSampler().getPagesBrief(); + getSampler().getBehaviorsBrief(); + } + }, 0, 10000); } public void doForComplete() { 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 43c11a55..0d872a27 100644 --- a/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java +++ b/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java @@ -17,6 +17,7 @@ import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.service.ScriptService; import org.bench4q.master.domain.testplan.LoadDistribute; +import org.bench4q.master.domain.testplan.RunningScriptSampler; import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.models.master.MonitorModel; @@ -107,6 +108,7 @@ public class TestPlanFactory { for (TestPlanScript testPlanScript : testPlanInRepo .getTestPlanScripts()) { testPlanScript.setTestPlanFactory(this); + testPlanScript.setSampler(new RunningScriptSampler(testPlanScript)); } return testPlanInRepo; } diff --git a/src/main/java/org/bench4q/master/domain/testplan/LoadDistribute.java b/src/main/java/org/bench4q/master/domain/testplan/LoadDistribute.java index 6e48be92..898e9cce 100644 --- a/src/main/java/org/bench4q/master/domain/testplan/LoadDistribute.java +++ b/src/main/java/org/bench4q/master/domain/testplan/LoadDistribute.java @@ -66,7 +66,6 @@ public class LoadDistribute { private void doAfterDistributeLoadSuccess( RunningScriptInterface scriptInput, List runningAgents) { scriptInput.doAfterDistributeLoad(runningAgents); - // scriptInput.doPeriodicBrief(); } private boolean hasEnoughCurrentLoad(int totalRequireLoad) { diff --git a/src/main/java/org/bench4q/master/domain/testplan/RunningScriptSampler.java b/src/main/java/org/bench4q/master/domain/testplan/RunningScriptSampler.java new file mode 100644 index 00000000..da41214c --- /dev/null +++ b/src/main/java/org/bench4q/master/domain/testplan/RunningScriptSampler.java @@ -0,0 +1,144 @@ +package org.bench4q.master.domain.testplan; + +import java.util.ArrayList; +import java.util.List; + +import org.bench4q.master.domain.RunningScript; +import org.bench4q.master.domain.entity.TestPlanScript; +import org.bench4q.master.domain.interfaces.RunningAgentInterface; +import org.bench4q.master.domain.interfaces.RunningScriptInterface; +import org.bench4q.master.helper.ApplicationContextHelper; +import org.bench4q.master.infrastructure.communication.AgentMessenger; +import org.bench4q.master.testplan.datastatistics.BehaviorsBriefStatistics; +import org.bench4q.master.testplan.datastatistics.DataStatistics; +import org.bench4q.master.testplan.datastatistics.PagesBriefStatistics; +import org.bench4q.master.testplan.datastatistics.ScriptBriefStatistics; +import org.bench4q.master.testplan.datastatistics.ScriptStatistics; +import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; +import org.bench4q.share.models.agent.statistics.DataStatisticsModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; +import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; +import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel; + +public class RunningScriptSampler { + private List dataStatisticsList; + private List runningAgents; + private AgentMessenger agentMessenger; + + private List getDataStatisticsList() { + return dataStatisticsList; + } + + private void setDataStatisticsList(List dataStatisticsList) { + this.dataStatisticsList = dataStatisticsList; + } + + private List getRunningAgents() { + return runningAgents; + } + + private void setRunningAgents(List runningAgents) { + this.runningAgents = runningAgents; + } + + private AgentMessenger getAgentMessenger() { + return agentMessenger; + } + + private void setAgentMessenger(AgentMessenger agentMessenger) { + this.agentMessenger = agentMessenger; + } + + public RunningScriptSampler(RunningScriptInterface runningScript) { + this.setDataStatisticsList(new ArrayList()); + this.getDataStatisticsList().add(new ScriptBriefStatistics()); + this.getDataStatisticsList().add(new BehaviorsBriefStatistics()); + this.getDataStatisticsList().add(new PagesBriefStatistics()); + List runningAgentsFromParam = new ArrayList(); + if (runningScript instanceof RunningScript) { + for (RunningAgentInterface e : runningScript.getRunningAgents()) { + runningAgentsFromParam.add(e); + } + } else if (runningScript instanceof TestPlanScript) { + TestPlanScript testPlanScript = (TestPlanScript) runningScript; + for (RunningAgentInterface e : testPlanScript.getRunningAgentsDB()) { + runningAgentsFromParam.add(e); + } + } + + this.setRunningAgents(runningAgentsFromParam); + this.setAgentMessenger(ApplicationContextHelper.getContext().getBean( + AgentMessenger.class)); + } + + public ScriptBriefResultModel getScriptBrief() { + for (RunningAgentInterface runningAgent : getRunningAgents()) { + if (runningAgent.isBreakDown()) { + continue; + } + AgentBriefStatusModel briefStatusModel = this.getAgentMessenger() + .brief(runningAgent.getAgent(), + runningAgent.getAgentRunId()); + if (briefStatusModel == null) { + continue; + } + getScriptBriefStatistics().add(briefStatusModel); + } + return (ScriptBriefResultModel) getScriptBriefStatistics() + .getStatistics(); + } + + public ScriptBehaviorsBriefModel getBehaviorsBrief() { + for (RunningAgentInterface runningAgent : getRunningAgents()) { + getBehaviorsBriefStatistics().add( + this.getAgentMessenger().behaviorsBrief( + runningAgent.getAgent(), + runningAgent.getAgentRunId())); + } + + ScriptBehaviorsBriefModel result = (ScriptBehaviorsBriefModel) getBehaviorsBriefStatistics() + .getStatistics(); + return result; + } + + public ScriptPagesBriefModel getPagesBrief() { + for (RunningAgentInterface runningAgent : getRunningAgents()) { + this.getPagesBriefStatistics().add( + this.getAgentMessenger().pagesBrief( + runningAgent.getAgent(), + runningAgent.getAgentRunId())); + } + ScriptPagesBriefModel result = (ScriptPagesBriefModel) this + .getPagesBriefStatistics().getStatistics(); + return result; + } + + private DataStatistics getScriptBriefStatistics() { + return getSpecificDataStatistics(ScriptBriefStatistics.class); + } + + private DataStatistics getBehaviorsBriefStatistics() { + return getSpecificDataStatistics(BehaviorsBriefStatistics.class); + } + + public DataStatistics getPagesBriefStatistics() { + return getSpecificDataStatistics(PagesBriefStatistics.class); + } + + private DataStatistics getSpecificDataStatistics( + Class dataStatisticsClass) { + for (DataStatistics dataStatistics : this.getDataStatisticsList()) { + if (dataStatistics.getClass().equals(dataStatisticsClass)) { + return dataStatistics; + } + } + return new ScriptStatistics() { + public void add(DataStatisticsModel dataUnit) { + } + + public Object getStatistics() { + return null; + } + }; + } +} diff --git a/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java b/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java index 1c06c067..9954a9e6 100644 --- a/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java +++ b/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java @@ -6,6 +6,8 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; +import org.bench4q.master.domain.entity.RunningAgentDB; +import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.repository.TestPlanRepository; @@ -15,6 +17,7 @@ import org.bench4q.master.domain.service.TestPlanScriptResultService; import org.bench4q.master.domain.service.TestPlanScriptService; import org.bench4q.master.domain.service.TestPlanService; import org.bench4q.master.domain.service.UserService; +import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.master.infrastructure.communication.AgentStateMessenger; import org.bench4q.master.test.controller.TestBase; import org.bench4q.master.testplan.highavailable.HighAvailablePool; @@ -36,6 +39,7 @@ public class TestBase_MakeUpTestPlan extends TestBase { private HighAvailablePool haPool; private AgentRepository agentRepository; private AgentStateMessenger agentStateMessenger; + private AgentMessenger agentMessenger; private UUID testPlanRunIdUuid; private int scriptId; private int scriptIdForSecond; @@ -143,6 +147,15 @@ public class TestBase_MakeUpTestPlan extends TestBase { this.agentStateMessenger = agentStateMessenger; } + private AgentMessenger getAgentMessenger() { + return agentMessenger; + } + + @Autowired + private void setAgentMessenger(AgentMessenger agentMessenger) { + this.agentMessenger = agentMessenger; + } + public UUID getTestPlanRunIdUuid() { return testPlanRunIdUuid; } @@ -181,6 +194,25 @@ public class TestBase_MakeUpTestPlan extends TestBase { public TestBase_MakeUpTestPlan() { } + protected void prepareForTestPlanRunning() { + addAAgentToPool(); + this.getHaPool().timerTask(); + } + + protected void cleanUpForTestPlanRunning() { + TestPlan testPlan = this.getTestPlanRepository().getTestPlanBy( + getTestPlanRunIdUuid()); + RunningAgentDB runningAgent = testPlan.extracSpecifiedScript( + this.getScriptService().getScript(getScriptId())) + .extractSpecificRunningAgentDB(Test_AGENT_HOSTNAME); + this.getAgentMessenger().stop(runningAgent.getAgent(), + runningAgent.getAgentRunId()); + runningAgent.getAgent().setRemainLoad( + runningAgent.getAgent().getMaxLoad()); + this.getAgentRepository().update(runningAgent.getAgent()); + this.getTestPlanRepository().detach(testPlan.getId()); + } + protected void addAAgentToPool() { System.out.println("!!!!!!!This need 127.0.0.1 agent in running!!!!!!"); this.getHaPool() diff --git a/src/test/java/org/bench4q/master/test/domain/Test_RunningScriptSampler.java b/src/test/java/org/bench4q/master/test/domain/Test_RunningScriptSampler.java new file mode 100644 index 00000000..72f79f82 --- /dev/null +++ b/src/test/java/org/bench4q/master/test/domain/Test_RunningScriptSampler.java @@ -0,0 +1,68 @@ +package org.bench4q.master.test.domain; + +import static org.junit.Assert.*; + +import java.util.Date; + +import org.bench4q.master.domain.entity.TestPlan; +import org.bench4q.master.domain.entity.TestPlanScript; +import org.bench4q.master.domain.testplan.RunningScriptSampler; +import org.bench4q.master.test.TestBase_MakeUpTestPlan; +import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:service-test-context.xml" }) +public class Test_RunningScriptSampler extends TestBase_MakeUpTestPlan { + private RunningScriptSampler runningScriptSampler; + + private RunningScriptSampler getRunningScriptSampler() { + return runningScriptSampler; + } + + private void setRunningScriptSampler( + RunningScriptSampler runningScriptSampler) { + this.runningScriptSampler = runningScriptSampler; + } + + @Before + public void prepare() { + prepareForTestPlanRunning(); + this.submitATestPlanWithOneScript(); + + TestPlan testPlan = fetchTestPlan(); + testPlan.run(); + this.getTestPlanRepository().updateEntity(testPlan); + TestPlan testPlanInRunning = fetchTestPlan(); + TestPlanScript testPlanScript = testPlanInRunning + .extracSpecifiedScript(this.getScriptService().getScript( + getScriptId())); + this.setRunningScriptSampler(new RunningScriptSampler(testPlanScript)); + } + + @After + public void cleanUp() { + cleanUpForTestPlanRunning(); + } + + private TestPlan fetchTestPlan() { + return this.getTestPlanRepository().getTestPlanBy( + getTestPlanRunIdUuid()); + } + + @Test + public void testGetScriptBrief() throws InterruptedException { + ScriptBriefResultModel resultModel = this.getRunningScriptSampler() + .getScriptBrief(); + Thread.sleep(100); + assertNotNull(resultModel); + assertTrue(resultModel.getvUserCount() > 0); + assertTrue(resultModel.getSamplingTime().before(new Date()) + && resultModel.getSamplingTime().after(new Date(0))); + } +} diff --git a/src/test/java/org/bench4q/master/test/domain/Test_TestPlan.java b/src/test/java/org/bench4q/master/test/domain/Test_TestPlan.java index ce2c3f38..615ac50e 100644 --- a/src/test/java/org/bench4q/master/test/domain/Test_TestPlan.java +++ b/src/test/java/org/bench4q/master/test/domain/Test_TestPlan.java @@ -8,7 +8,6 @@ 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.factory.TestPlanFactory; -import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.master.test.TestBase_MakeUpTestPlan; import org.junit.After; import org.junit.Before; @@ -22,7 +21,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations = { "classpath:service-test-context.xml" }) public class Test_TestPlan extends TestBase_MakeUpTestPlan { private TestPlanFactory testPlanFactory; - private AgentMessenger agentMessenger; private TestPlanFactory getTestPlanFactory() { return testPlanFactory; @@ -33,35 +31,15 @@ public class Test_TestPlan extends TestBase_MakeUpTestPlan { this.testPlanFactory = testPlanFactory; } - private AgentMessenger getAgentMessenger() { - return agentMessenger; - } - - @Autowired - private void setAgentMessenger(AgentMessenger agentMessenger) { - this.agentMessenger = agentMessenger; - } - @Before public void prepare() { - addAAgentToPool(); - this.getHaPool().timerTask(); + prepareForTestPlanRunning(); submitATestPlanWithOneScript(); } @After public void cleanUp() { - TestPlan testPlan = this.getTestPlanRepository().getTestPlanBy( - getTestPlanRunIdUuid()); - RunningAgentDB runningAgent = testPlan.extracSpecifiedScript( - this.getScriptService().getScript(getScriptId())) - .extractSpecificRunningAgentDB(Test_AGENT_HOSTNAME); - this.getAgentMessenger().stop(runningAgent.getAgent(), - runningAgent.getAgentRunId()); - runningAgent.getAgent().setRemainLoad( - runningAgent.getAgent().getMaxLoad()); - this.getAgentRepository().update(runningAgent.getAgent()); - this.getTestPlanRepository().detach(testPlan.getId()); + cleanUpForTestPlanRunning(); } @Test diff --git a/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java b/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java index 0945f433..fbe8919d 100644 --- a/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java +++ b/src/test/java/org/bench4q/master/test/testplan/Test_LoadDistribute.java @@ -54,8 +54,7 @@ public class Test_LoadDistribute extends TestBase_MakeUpTestPlan { @Before public void prepare() { - addAAgentToPool(); - this.getHaPool().timerTask(); + prepareForTestPlanRunning(); submitATestPlanWithOneScript(); }