refactor, let the briefAll can be called

refactor
refactor, let the briefAll can be called
This commit is contained in:
coderfengyun 2014-10-11 15:57:15 +08:00
parent 7884246521
commit e0392650dd
3 changed files with 108 additions and 78 deletions

View File

@ -1,9 +1,9 @@
package org.bench4q.agent.api; package org.bench4q.agent.api;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.ParameterFileCollector; import org.bench4q.agent.plugin.ParameterFileCollector;
@ -11,9 +11,6 @@ import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.engine.ScenarioContext; import org.bench4q.agent.scenario.engine.ScenarioContext;
import org.bench4q.agent.scenario.engine.ScenarioEngine; import org.bench4q.agent.scenario.engine.ScenarioEngine;
import org.bench4q.agent.scenario.engine.Schedule; import org.bench4q.agent.scenario.engine.Schedule;
import org.bench4q.agent.scenario.node.Behavior;
import org.bench4q.agent.scenario.node.PluginImplementBehavior;
import org.bench4q.agent.scenario.node.UserBehavior;
import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.CleanTestResultModel; import org.bench4q.share.models.agent.CleanTestResultModel;
import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioModel;
@ -23,9 +20,7 @@ import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.bench4q.share.models.agent.scriptrecord.ScheduleModel; import org.bench4q.share.models.agent.scriptrecord.ScheduleModel;
import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel;
import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel;
import org.bench4q.share.models.agent.statistics.AgentPageBriefModel;
import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel; import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel;
import org.bench4q.share.models.agent.statistics.BehaviorBriefModel;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -40,7 +35,7 @@ import org.springframework.web.multipart.MultipartFile;
public class TestController { public class TestController {
private ScenarioEngine scenarioEngine; private ScenarioEngine scenarioEngine;
private ParameterFileCollector paramFileCollector; private ParameterFileCollector paramFileCollector;
private Logger logger = Logger.getLogger(TestController.class); public Logger logger = Logger.getLogger(TestController.class);
private ScenarioEngine getScenarioEngine() { private ScenarioEngine getScenarioEngine() {
return scenarioEngine; return scenarioEngine;
@ -63,9 +58,13 @@ public class TestController {
@RequestMapping(value = "/bookTest/{poolSize}", method = { @RequestMapping(value = "/bookTest/{poolSize}", method = {
RequestMethod.GET, RequestMethod.POST }) RequestMethod.GET, RequestMethod.POST })
@ResponseBody @ResponseBody
public RunScenarioResultModel bookTest(@PathVariable int poolSize) { public RunScenarioResultModel bookTest(@PathVariable int poolSize,
@RequestParam(value = "briefCycle") int briefCycle,
@RequestParam(value = "briefUnit") String briefUnit) {
try { try {
UUID runId = UUID.randomUUID(); UUID runId = UUID.randomUUID();
long sampleCycleInMillis = TimeUnit.valueOf(briefUnit).toMillis(
briefCycle);
this.getScenarioEngine().addRunningTestWithoutScenario(runId, this.getScenarioEngine().addRunningTestWithoutScenario(runId,
poolSize); poolSize);
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel(); RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
@ -88,6 +87,7 @@ public class TestController {
try { try {
this.getParamFileCollector().collectParamFiles(files, runId); this.getParamFileCollector().collectParamFiles(files, runId);
System.out.println(scenarioModel); System.out.println(scenarioModel);
logger.info("Real start time is:" + new Date(realStartTime)); logger.info("Real start time is:" + new Date(realStartTime));
RunScenarioModel runScenarioModel = MarshalHelper.unmarshal( RunScenarioModel runScenarioModel = MarshalHelper.unmarshal(
RunScenarioModel.class, scenarioModel); RunScenarioModel.class, scenarioModel);
@ -121,11 +121,7 @@ public class TestController {
@RequestMapping(value = "/briefAll/{runId}", method = RequestMethod.GET) @RequestMapping(value = "/briefAll/{runId}", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public TestBriefStatusModel briefAll(@PathVariable UUID runId) { public TestBriefStatusModel briefAll(@PathVariable UUID runId) {
TestBriefStatusModel result = new TestBriefStatusModel(); return this.getScenarioEngine().getRunningTests().get(runId).briefAll();
result.setScenarioBriefModel(this.brief(runId));
result.setPagesBriefModel(this.pagesBrief(runId));
result.setBehaviorsBriefModel(this.behaviorsBrief(runId));
return result;
} }
@RequestMapping(value = "/pagesBrief/{runId}") @RequestMapping(value = "/pagesBrief/{runId}")
@ -133,55 +129,16 @@ public class TestController {
public AgentPagesBriefModel pagesBrief(@PathVariable UUID runId) { public AgentPagesBriefModel pagesBrief(@PathVariable UUID runId) {
ScenarioContext context = this.getScenarioEngine().getRunningTests() ScenarioContext context = this.getScenarioEngine().getRunningTests()
.get(runId); .get(runId);
AgentPagesBriefModel result = new AgentPagesBriefModel(); return context.briefAll().getPagesBriefModel();
List<AgentPageBriefModel> pageBrieves = new ArrayList<AgentPageBriefModel>();
if (context == null) {
return null;
}
for (int i = 0; i < context.getScenario().getPages().length; i++) {
pageBrieves.add((AgentPageBriefModel) context.getDataCollector()
.getPageBriefStatistics(i));
}
result.setPageBriefModels(pageBrieves);
return result;
} }
@RequestMapping(value = "/behaviorsBrief/{runId}") @RequestMapping(value = "/behaviorsBrief/{runId}")
@ResponseBody @ResponseBody
public AgentBehaviorsBriefModel behaviorsBrief(@PathVariable UUID runId) { public AgentBehaviorsBriefModel behaviorsBrief(@PathVariable UUID runId) {
AgentBehaviorsBriefModel ret = new AgentBehaviorsBriefModel();
List<BehaviorBriefModel> behaviorBriefModels = new ArrayList<BehaviorBriefModel>();
ScenarioContext scenarioContext = this.getScenarioEngine() ScenarioContext scenarioContext = this.getScenarioEngine()
.getRunningTests().get(runId); .getRunningTests().get(runId);
if (scenarioContext == null) { AgentBehaviorsBriefModel ret = scenarioContext.briefAll()
return null; .getBehaviorsBriefModel();
}
for (Behavior behavior : scenarioContext.getScenario()
.getAllBehaviors()) {
if (!(behavior instanceof UserBehavior)) {
continue;
}
PluginImplementBehavior pluginImplementBehavior = (PluginImplementBehavior) behavior;
try {
BehaviorBriefModel briefModel = pluginImplementBehavior
.getBehaviorBriefResult(scenarioContext
.getDataCollector());
logger.info(pluginImplementBehavior.getId());
if (briefModel == null) {
continue;
}
briefModel.setBehaviorUrl(pluginImplementBehavior
.getSpecificParamValue("url"));
behaviorBriefModels.add(briefModel);
} catch (Exception e) {
logger.info(
"where behaviorId is "
+ pluginImplementBehavior.getId(), e);
}
}
ret.setBehaviorBriefModels(behaviorBriefModels);
return ret; return ret;
} }
@ -190,14 +147,7 @@ public class TestController {
public AgentBriefStatusModel brief(@PathVariable UUID runId) { public AgentBriefStatusModel brief(@PathVariable UUID runId) {
ScenarioContext scenarioContext = this.getScenarioEngine() ScenarioContext scenarioContext = this.getScenarioEngine()
.getRunningTests().get(runId); .getRunningTests().get(runId);
if (scenarioContext == null) { return scenarioContext.briefAll().getScenarioBriefModel();
return null;
}
AgentBriefStatusModel agentStatusModel = (AgentBriefStatusModel) scenarioContext
.getDataCollector().getScenarioBriefStatistics();
agentStatusModel.setvUserCount(scenarioContext.getExecutor()
.getActiveCount());
return agentStatusModel;
} }
@RequestMapping(value = "/stop/{runId}", method = { RequestMethod.GET, @RequestMapping(value = "/stop/{runId}", method = { RequestMethod.GET,
@ -210,11 +160,9 @@ public class TestController {
if (scenarioContext == null) { if (scenarioContext == null) {
return null; return null;
} }
System.out.println("when before stop, classId:"
+ scenarioContext.getExecutor().toString());
System.out.println("when after stop, classId:"
+ scenarioContext.getExecutor().toString());
scenarioContext.stop(); scenarioContext.stop();
System.out
.println("**********************Stop*****************************");
clean(); clean();
return new StopTestModel(true); return new StopTestModel(true);
} }

View File

@ -1,6 +1,8 @@
package org.bench4q.agent.scenario.engine; package org.bench4q.agent.scenario.engine;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import java.util.UUID; import java.util.UUID;
@ -14,7 +16,16 @@ import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.datacollector.impl.DataCollectorImpl; import org.bench4q.agent.datacollector.impl.DataCollectorImpl;
import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.node.Behavior;
import org.bench4q.agent.scenario.node.PluginImplementBehavior;
import org.bench4q.agent.scenario.node.UserBehavior;
import org.bench4q.agent.storage.StorageHelper; import org.bench4q.agent.storage.StorageHelper;
import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel;
import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel;
import org.bench4q.share.models.agent.statistics.AgentPageBriefModel;
import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel;
import org.bench4q.share.models.agent.statistics.BehaviorBriefModel;
public class ScenarioContext implements Observer { public class ScenarioContext implements Observer {
private static final long keepAliveTime = 10; private static final long keepAliveTime = 10;
@ -27,8 +38,9 @@ public class ScenarioContext implements Observer {
private final DataCollector dataCollector; private final DataCollector dataCollector;
private final PluginManager pluginManager; private final PluginManager pluginManager;
private Schedule schedule; private Schedule schedule;
private Logger logger = Logger.getLogger(this.getClass());
public ScenarioContext(UUID testId, Date startDate, private ScenarioContext(UUID testId, Date startDate,
ThreadPoolExecutor executor, DataCollector dataCollector, ThreadPoolExecutor executor, DataCollector dataCollector,
PluginManager pluginManager) { PluginManager pluginManager) {
this.testId = testId; this.testId = testId;
@ -62,7 +74,7 @@ public class ScenarioContext implements Observer {
return scenario; return scenario;
} }
public void setScenario(Scenario scenario) { private void setScenario(Scenario scenario) {
this.scenario = scenario; this.scenario = scenario;
} }
@ -70,11 +82,11 @@ public class ScenarioContext implements Observer {
return finished; return finished;
} }
public void setFinished(boolean finished) { private void setFinished(boolean finished) {
this.finished = finished; this.finished = finished;
} }
public DataCollector getDataCollector() { DataCollector getDataCollector() {
return dataCollector; return dataCollector;
} }
@ -120,6 +132,9 @@ public class ScenarioContext implements Observer {
} }
/** /**
* Task Management section -------- Start
*
*
* Now, I tolerate that if the requiredLoad < * Now, I tolerate that if the requiredLoad <
* this.getExecutor.getCorePoolSize(), then the excess threads will be * this.getExecutor.getCorePoolSize(), then the excess threads will be
* killed when its current task complete * killed when its current task complete
@ -181,9 +196,79 @@ public class ScenarioContext implements Observer {
} }
} }
/**
* Task Management section --------- Stop
*/
/**
* Brief When Running Section -------- Start
*
* @return
*/
public TestBriefStatusModel briefAll() {
TestBriefStatusModel result = new TestBriefStatusModel();
result.setScenarioBriefModel(getScenarioBrief());
result.setPagesBriefModel(getPagesBrief());
result.setBehaviorsBriefModel(getBehaviorsBrief());
return result;
}
private AgentBriefStatusModel getScenarioBrief() {
AgentBriefStatusModel result = (AgentBriefStatusModel) this.dataCollector
.getScenarioBriefStatistics();
result.setvUserCount(this.getExecutor().getActiveCount());
return result;
}
private AgentPagesBriefModel getPagesBrief() {
AgentPagesBriefModel result = new AgentPagesBriefModel();
List<AgentPageBriefModel> pageBrieves = new ArrayList<AgentPageBriefModel>();
for (int i = 0; i < getScenario().getPages().length; i++) {
pageBrieves.add((AgentPageBriefModel) getDataCollector()
.getPageBriefStatistics(i));
}
result.setPageBriefModels(pageBrieves);
return result;
}
private AgentBehaviorsBriefModel getBehaviorsBrief() {
List<BehaviorBriefModel> behaviorBriefModels = new ArrayList<BehaviorBriefModel>();
AgentBehaviorsBriefModel ret = new AgentBehaviorsBriefModel();
for (Behavior behavior : getScenario().getAllBehaviors()) {
if (!(behavior instanceof UserBehavior)) {
continue;
}
PluginImplementBehavior pluginImplementBehavior = (PluginImplementBehavior) behavior;
try {
BehaviorBriefModel briefModel = pluginImplementBehavior
.getBehaviorBriefResult(getDataCollector());
this.logger.info(pluginImplementBehavior.getId());
if (briefModel == null) {
continue;
}
briefModel.setBehaviorUrl(pluginImplementBehavior
.getSpecificParamValue("url"));
behaviorBriefModels.add(briefModel);
} catch (Exception e) {
this.logger.info("where behaviorId is "
+ pluginImplementBehavior.getId(), e);
}
}
ret.setBehaviorBriefModels(behaviorBriefModels);
return ret;
}
/**
* Brief When Running-------- End
*/
public void stop() { public void stop() {
this.setFinished(true); this.setFinished(true);
this.getExecutor().shutdownNow(); this.getExecutor().shutdownNow();
this.getSchedule().stop(); this.getSchedule().stop();
} }
} }

View File

@ -193,9 +193,8 @@ public class TestWithScriptFile extends TestBase {
+ "testJD.xml")); + "testJD.xml"));
HttpResponse httpResponse1 = this.getHttpRequester().sendPost( HttpResponse httpResponse1 = this.getHttpRequester().sendPost(
url + "/bookTest/20", null, null); url + "/bookTest/20", null, null);
RunScenarioResultModel bookResponse = MarshalHelper RunScenarioResultModel bookResponse = MarshalHelper.tryUnmarshal(
.tryUnmarshal(RunScenarioResultModel.class, RunScenarioResultModel.class, httpResponse1.getContent());
httpResponse1.getContent());
assertNotNull(bookResponse); assertNotNull(bookResponse);
assertNotNull(bookResponse.getRunId().toString()); assertNotNull(bookResponse.getRunId().toString());
System.out.println(bookResponse.getRunId().toString()); System.out.println(bookResponse.getRunId().toString());
@ -204,7 +203,6 @@ public class TestWithScriptFile extends TestBase {
MarshalHelper.tryMarshal(getScenarioModel())); MarshalHelper.tryMarshal(getScenarioModel()));
stringPart.put("scheduleContent", stringPart.put("scheduleContent",
MarshalHelper.tryMarshal(buildScheduleModel())); MarshalHelper.tryMarshal(buildScheduleModel()));
// TODO:
HttpResponse httpResponse = this.getHttpRequester().postFiles( HttpResponse httpResponse = this.getHttpRequester().postFiles(
null, null,
url + "/submitScenarioWithParams/" url + "/submitScenarioWithParams/"
@ -213,9 +211,8 @@ public class TestWithScriptFile extends TestBase {
assertNotNull(httpResponse); assertNotNull(httpResponse);
assertNotNull(httpResponse.getContent()); assertNotNull(httpResponse.getContent());
assertEquals(200, httpResponse.getCode()); assertEquals(200, httpResponse.getCode());
RunScenarioResultModel resultModel = MarshalHelper RunScenarioResultModel resultModel = MarshalHelper.tryUnmarshal(
.tryUnmarshal(RunScenarioResultModel.class, RunScenarioResultModel.class, httpResponse.getContent());
httpResponse.getContent());
String dirPath = (String) TestHelper.invokePrivate( String dirPath = (String) TestHelper.invokePrivate(
new ParameterFileCollector(), "guardDirExists", new ParameterFileCollector(), "guardDirExists",
new Class[] { UUID.class }, new Class[] { UUID.class },