From ef19610328ade2cdbcb60c970531825866c92edd Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Tue, 25 Feb 2014 21:40:01 +0800 Subject: [PATCH] refactor runningScript with runningScriptSampler --- .../master/api/TestPlanController.java | 2 +- .../bench4q/master/domain/RunningScript.java | 96 ++++--------------- .../master/domain/TestPlanContext.java | 9 -- .../communication/AgentMessenger.java | 4 +- .../test/domain/Test_RunningScript.java | 14 --- .../test/service/Test_ReportService.java | 7 ++ 6 files changed, 27 insertions(+), 105 deletions(-) diff --git a/src/main/java/org/bench4q/master/api/TestPlanController.java b/src/main/java/org/bench4q/master/api/TestPlanController.java index 56873abf..a000d3a3 100644 --- a/src/main/java/org/bench4q/master/api/TestPlanController.java +++ b/src/main/java/org/bench4q/master/api/TestPlanController.java @@ -46,7 +46,7 @@ public class TestPlanController extends BaseController { private ReportService reportService; private TestPlanScriptResultService testPlanScriptResultService; private BusinessModelMapFactory businessMapFactory; - private Logger logger; + private Logger logger = Logger.getLogger(TestPlanController.class); private TestPlanEngine getTestPlanRunner() { return testPlanRunner; diff --git a/src/main/java/org/bench4q/master/domain/RunningScript.java b/src/main/java/org/bench4q/master/domain/RunningScript.java index 9bce043e..9af3c5fb 100644 --- a/src/main/java/org/bench4q/master/domain/RunningScript.java +++ b/src/main/java/org/bench4q/master/domain/RunningScript.java @@ -1,6 +1,5 @@ package org.bench4q.master.domain; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -16,22 +15,17 @@ import org.bench4q.master.domain.interfaces.RunningScriptInterface; import org.bench4q.master.domain.service.AgentService; import org.bench4q.master.domain.service.ScriptService; import org.bench4q.master.domain.service.TestPlanEngine; +import org.bench4q.master.domain.service.TestPlanScriptService; import org.bench4q.master.domain.service.TestPlanService; +import org.bench4q.master.domain.testplan.RunningScriptSampler; 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.master.testplan.highavailable.AgentRunBlotter; import org.bench4q.master.testplan.highavailable.faultolerence.StopAgentFault; import org.bench4q.master.testplan.schedulscript.ExecutionOverTask; import org.bench4q.master.testplan.schedulscript.WarmUpOverTask; import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.StopTestModel; -import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; -import org.bench4q.share.models.agent.statistics.DataStatisticsModel; import org.bench4q.share.models.master.TestScriptConfig; import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; @@ -48,7 +42,8 @@ public class RunningScript extends Observable implements RunningScriptInterface private Set runningAgents; private Timer timer; - private List dataStatisticsList; + private RunningScriptSampler sampler; + // private List dataStatisticsList; private AgentMessenger runningAgentService; private AgentService agentService; private ScriptService scriptService; @@ -111,12 +106,12 @@ public class RunningScript extends Observable implements RunningScriptInterface this.testPlanID = testPlanID; } - private List getDataStatisticsList() { - return dataStatisticsList; + private RunningScriptSampler getSampler() { + return sampler; } - private void setDataStatisticsList(List dataStatisticsList) { - this.dataStatisticsList = dataStatisticsList; + private void setSampler(RunningScriptSampler sampler) { + this.sampler = sampler; } private AgentMessenger getRunningAgentService() { @@ -166,10 +161,6 @@ public class RunningScript extends Observable implements RunningScriptInterface this.setLatestScriptBriefModel(scriptBriefResultModel); this.setRunningAgents(new HashSet()); - this.setDataStatisticsList(new ArrayList()); - this.getDataStatisticsList().add(new ScriptBriefStatistics()); - this.getDataStatisticsList().add(new BehaviorsBriefStatistics()); - this.getDataStatisticsList().add(new PagesBriefStatistics()); this.setTimer(new Timer()); this.setRunningAgentService(ApplicationContextHelper.getContext() .getBean(AgentMessenger.class)); @@ -177,6 +168,9 @@ public class RunningScript extends Observable implements RunningScriptInterface AgentService.class)); this.setScriptService(ApplicationContextHelper.getContext().getBean( ScriptService.class)); + this.addObserver(ApplicationContextHelper.getContext().getBean( + TestPlanScriptService.class)); + } public RunningScript(TestScriptConfig config, int requireLoad, @@ -196,67 +190,17 @@ public class RunningScript extends Observable implements RunningScriptInterface return Collections.unmodifiableCollection(this.runningAgents); } - 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; - } - }; - } - private ScriptBriefResultModel getScriptBrief() { - for (RunningAgent runningAgent : getRunningAgents()) { - if (runningAgent.isBreakDown()) { - continue; - } - AgentBriefStatusModel briefStatusModel = getRunningAgentService() - .brief(runningAgent.getAgent(), - runningAgent.getAgentRunId()); - if (briefStatusModel == null) { - continue; - } - getScriptBriefStatistics().add(briefStatusModel); - } - ScriptBriefResultModel result = (ScriptBriefResultModel) getScriptBriefStatistics() - .getStatistics(); + ScriptBriefResultModel result = this.getSampler().getScriptBrief(); result.setFinished(isFinished()); this.setLatestScriptBriefModel(result); - this.setChanged(); - this.notifyObservers(result); + notifyObserver(result); return result; } private ScriptBehaviorsBriefModel getBehaviorsBrief() { - for (RunningAgent runningAgent : getRunningAgents()) { - getBehaviorsBriefStatistics().add( - this.getRunningAgentService().behaviorsBrief( - runningAgent.getAgent(), - runningAgent.getAgentRunId())); - } - - ScriptBehaviorsBriefModel result = (ScriptBehaviorsBriefModel) getBehaviorsBriefStatistics() - .getStatistics(); + ScriptBehaviorsBriefModel result = this.getSampler() + .getBehaviorsBrief(); result.setFinished(isFinished()); notifyObserver(result); return result; @@ -268,14 +212,7 @@ public class RunningScript extends Observable implements RunningScriptInterface } private ScriptPagesBriefModel getPagesBrief() { - for (RunningAgent runningAgent : getRunningAgents()) { - this.getPagesBriefStatistics().add( - this.getRunningAgentService().pagesBrief( - runningAgent.getAgent(), - runningAgent.getAgentRunId())); - } - ScriptPagesBriefModel result = (ScriptPagesBriefModel) this - .getPagesBriefStatistics().getStatistics(); + ScriptPagesBriefModel result = this.getSampler().getPagesBrief(); notifyObserver(result); return result; } @@ -310,6 +247,7 @@ public class RunningScript extends Observable implements RunningScriptInterface public void doAfterDistributeLoad(List agentsAfterDistribute) { this.getRunningAgents().addAll(agentsAfterDistribute); + this.setSampler(new RunningScriptSampler(this)); doPeriodicBrief(); } diff --git a/src/main/java/org/bench4q/master/domain/TestPlanContext.java b/src/main/java/org/bench4q/master/domain/TestPlanContext.java index 4f1a16e2..38d6c5f2 100644 --- a/src/main/java/org/bench4q/master/domain/TestPlanContext.java +++ b/src/main/java/org/bench4q/master/domain/TestPlanContext.java @@ -9,8 +9,6 @@ import java.util.Map; import java.util.UUID; import org.bench4q.master.domain.factory.BusinessModelMapFactory; -import org.bench4q.master.domain.service.TestPlanScriptService; -import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.share.models.master.RunningScriptModel; import org.bench4q.share.models.master.TestPlanBusinessModel; @@ -61,18 +59,11 @@ public class TestPlanContext { } for (RunningScript runningScript : list) { runningScript.setTestPlanID(testPlanID); - runningScript.addObserver(ApplicationContextHelper.getContext() - .getBean(TestPlanScriptService.class)); this.getRunningScriptMap().put( new Integer(runningScript.getScriptId()), runningScript); } } - // public void addToScriptMap(RunningScript runningScript) { - // this.runningScriptMap.put(new Integer(runningScript.getScriptId()), - // runningScript); - // } - public RunningScript queryRunningScript(int scriptId) { return this.getRunningScriptMap() == null ? null : this.runningScriptMap.get(new Integer(scriptId)); diff --git a/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java b/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java index 0e333849..287172b9 100644 --- a/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java +++ b/src/main/java/org/bench4q/master/infrastructure/communication/AgentMessenger.java @@ -110,11 +110,11 @@ public class AgentMessenger { 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"); + logger.error(ExceptionLog.getStackTrace(e) + " When behaviorsBrief"); return null; } } diff --git a/src/test/java/org/bench4q/master/test/domain/Test_RunningScript.java b/src/test/java/org/bench4q/master/test/domain/Test_RunningScript.java index f9b9e438..004a9d6f 100644 --- a/src/test/java/org/bench4q/master/test/domain/Test_RunningScript.java +++ b/src/test/java/org/bench4q/master/test/domain/Test_RunningScript.java @@ -10,7 +10,6 @@ import org.bench4q.master.domain.RunningScript; import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.domain.factory.BusinessModelMapFactory; import org.bench4q.master.test.TestBase_MakeUpTestPlan; -import org.bench4q.master.testplan.datastatistics.PagesBriefStatistics; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -56,17 +55,4 @@ public class Test_RunningScript extends TestBase_MakeUpTestPlan { assertEquals(true, this.getRunningScript().isFinished()); } - @Test - public void testForBriefByCycle() throws InterruptedException { - this.getRunningScript().doPeriodicBrief(); - Thread.sleep(20000); - assertEquals(0, this.getRunningScript().getLatestScriptBriefModel() - .getAverageResponseTime()); - } - - @Test - public void testForGetPagesBriefStatistics() { - assertNotNull(this.getRunningScript().getPagesBriefStatistics()); - assertTrue(this.getRunningScript().getPagesBriefStatistics() instanceof PagesBriefStatistics); - } } diff --git a/src/test/java/org/bench4q/master/test/service/Test_ReportService.java b/src/test/java/org/bench4q/master/test/service/Test_ReportService.java index cbe6a783..6f1f4346 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_ReportService.java +++ b/src/test/java/org/bench4q/master/test/service/Test_ReportService.java @@ -2,6 +2,8 @@ package org.bench4q.master.test.service; import static org.junit.Assert.*; +import java.util.UUID; + import org.bench4q.master.report.ReportService; import org.bench4q.master.test.TestBase_MakeUpTestPlan; import org.junit.Test; @@ -31,4 +33,9 @@ public class Test_ReportService extends TestBase_MakeUpTestPlan { getTestPlanRunIdUuid())); } + @Test + public void testCreateReport() { + this.getReportService().createReport( + UUID.fromString("4097990c-7163-4da2-b2e1-609c1d9b66a1")); + } }