refactor and add test about RunningScriptSampler

This commit is contained in:
coderfengyun 2014-02-25 20:14:02 +08:00
parent b71749a35e
commit b978922294
9 changed files with 279 additions and 35 deletions

View File

@ -45,7 +45,7 @@ public class RunningScript extends Observable implements RunningScriptInterface
private boolean finished;
private TestScriptConfig config;
private RunScenarioModel scenario;
private Set<RunningAgent> runningAgents = new HashSet<RunningAgent>();
private Set<RunningAgent> runningAgents;
private Timer timer;
private List<DataStatistics> 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<RunningAgent> queryRunningAgentsUnModifiable() {
return Collections.unmodifiableCollection(this.runningAgents);
}

View File

@ -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<RunningAgentDB> 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<RunningAgent> 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() {

View File

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

View File

@ -66,7 +66,6 @@ public class LoadDistribute {
private void doAfterDistributeLoadSuccess(
RunningScriptInterface scriptInput, List<RunningAgent> runningAgents) {
scriptInput.doAfterDistributeLoad(runningAgents);
// scriptInput.doPeriodicBrief();
}
private boolean hasEnoughCurrentLoad(int totalRequireLoad) {

View File

@ -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<DataStatistics> dataStatisticsList;
private List<RunningAgentInterface> runningAgents;
private AgentMessenger agentMessenger;
private List<DataStatistics> getDataStatisticsList() {
return dataStatisticsList;
}
private void setDataStatisticsList(List<DataStatistics> dataStatisticsList) {
this.dataStatisticsList = dataStatisticsList;
}
private List<RunningAgentInterface> getRunningAgents() {
return runningAgents;
}
private void setRunningAgents(List<RunningAgentInterface> 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<DataStatistics>());
this.getDataStatisticsList().add(new ScriptBriefStatistics());
this.getDataStatisticsList().add(new BehaviorsBriefStatistics());
this.getDataStatisticsList().add(new PagesBriefStatistics());
List<RunningAgentInterface> runningAgentsFromParam = new ArrayList<RunningAgentInterface>();
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<? 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;
}
};
}
}

View File

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

View File

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

View File

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

View File

@ -54,8 +54,7 @@ public class Test_LoadDistribute extends TestBase_MakeUpTestPlan {
@Before
public void prepare() {
addAAgentToPool();
this.getHaPool().timerTask();
prepareForTestPlanRunning();
submitATestPlanWithOneScript();
}