refactor runningScript with runningScriptSampler

This commit is contained in:
coderfengyun 2014-02-25 21:40:01 +08:00
parent b978922294
commit ef19610328
6 changed files with 27 additions and 105 deletions

View File

@ -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;

View File

@ -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<RunningAgent> runningAgents;
private Timer timer;
private List<DataStatistics> dataStatisticsList;
private RunningScriptSampler sampler;
// private List<DataStatistics> 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<DataStatistics> getDataStatisticsList() {
return dataStatisticsList;
private RunningScriptSampler getSampler() {
return sampler;
}
private void setDataStatisticsList(List<DataStatistics> 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<RunningAgent>());
this.setDataStatisticsList(new ArrayList<DataStatistics>());
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<? extends DataStatistics> 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<RunningAgent> agentsAfterDistribute) {
this.getRunningAgents().addAll(agentsAfterDistribute);
this.setSampler(new RunningScriptSampler(this));
doPeriodicBrief();
}

View File

@ -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));

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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"));
}
}