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;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
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.ScenarioEngine;
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.models.agent.CleanTestResultModel;
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.statistics.AgentBriefStatusModel;
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.BehaviorBriefModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
@ -40,7 +35,7 @@ import org.springframework.web.multipart.MultipartFile;
public class TestController {
private ScenarioEngine scenarioEngine;
private ParameterFileCollector paramFileCollector;
private Logger logger = Logger.getLogger(TestController.class);
public Logger logger = Logger.getLogger(TestController.class);
private ScenarioEngine getScenarioEngine() {
return scenarioEngine;
@ -63,9 +58,13 @@ public class TestController {
@RequestMapping(value = "/bookTest/{poolSize}", method = {
RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public RunScenarioResultModel bookTest(@PathVariable int poolSize) {
public RunScenarioResultModel bookTest(@PathVariable int poolSize,
@RequestParam(value = "briefCycle") int briefCycle,
@RequestParam(value = "briefUnit") String briefUnit) {
try {
UUID runId = UUID.randomUUID();
long sampleCycleInMillis = TimeUnit.valueOf(briefUnit).toMillis(
briefCycle);
this.getScenarioEngine().addRunningTestWithoutScenario(runId,
poolSize);
RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
@ -88,6 +87,7 @@ public class TestController {
try {
this.getParamFileCollector().collectParamFiles(files, runId);
System.out.println(scenarioModel);
logger.info("Real start time is:" + new Date(realStartTime));
RunScenarioModel runScenarioModel = MarshalHelper.unmarshal(
RunScenarioModel.class, scenarioModel);
@ -121,11 +121,7 @@ public class TestController {
@RequestMapping(value = "/briefAll/{runId}", method = RequestMethod.GET)
@ResponseBody
public TestBriefStatusModel briefAll(@PathVariable UUID runId) {
TestBriefStatusModel result = new TestBriefStatusModel();
result.setScenarioBriefModel(this.brief(runId));
result.setPagesBriefModel(this.pagesBrief(runId));
result.setBehaviorsBriefModel(this.behaviorsBrief(runId));
return result;
return this.getScenarioEngine().getRunningTests().get(runId).briefAll();
}
@RequestMapping(value = "/pagesBrief/{runId}")
@ -133,55 +129,16 @@ public class TestController {
public AgentPagesBriefModel pagesBrief(@PathVariable UUID runId) {
ScenarioContext context = this.getScenarioEngine().getRunningTests()
.get(runId);
AgentPagesBriefModel result = new AgentPagesBriefModel();
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;
return context.briefAll().getPagesBriefModel();
}
@RequestMapping(value = "/behaviorsBrief/{runId}")
@ResponseBody
public AgentBehaviorsBriefModel behaviorsBrief(@PathVariable UUID runId) {
AgentBehaviorsBriefModel ret = new AgentBehaviorsBriefModel();
List<BehaviorBriefModel> behaviorBriefModels = new ArrayList<BehaviorBriefModel>();
ScenarioContext scenarioContext = this.getScenarioEngine()
.getRunningTests().get(runId);
if (scenarioContext == null) {
return null;
}
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);
AgentBehaviorsBriefModel ret = scenarioContext.briefAll()
.getBehaviorsBriefModel();
return ret;
}
@ -190,14 +147,7 @@ public class TestController {
public AgentBriefStatusModel brief(@PathVariable UUID runId) {
ScenarioContext scenarioContext = this.getScenarioEngine()
.getRunningTests().get(runId);
if (scenarioContext == null) {
return null;
}
AgentBriefStatusModel agentStatusModel = (AgentBriefStatusModel) scenarioContext
.getDataCollector().getScenarioBriefStatistics();
agentStatusModel.setvUserCount(scenarioContext.getExecutor()
.getActiveCount());
return agentStatusModel;
return scenarioContext.briefAll().getScenarioBriefModel();
}
@RequestMapping(value = "/stop/{runId}", method = { RequestMethod.GET,
@ -210,11 +160,9 @@ public class TestController {
if (scenarioContext == 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();
System.out
.println("**********************Stop*****************************");
clean();
return new StopTestModel(true);
}

View File

@ -1,6 +1,8 @@
package org.bench4q.agent.scenario.engine;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
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.plugin.PluginManager;
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.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 {
private static final long keepAliveTime = 10;
@ -27,8 +38,9 @@ public class ScenarioContext implements Observer {
private final DataCollector dataCollector;
private final PluginManager pluginManager;
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,
PluginManager pluginManager) {
this.testId = testId;
@ -62,7 +74,7 @@ public class ScenarioContext implements Observer {
return scenario;
}
public void setScenario(Scenario scenario) {
private void setScenario(Scenario scenario) {
this.scenario = scenario;
}
@ -70,11 +82,11 @@ public class ScenarioContext implements Observer {
return finished;
}
public void setFinished(boolean finished) {
private void setFinished(boolean finished) {
this.finished = finished;
}
public DataCollector getDataCollector() {
DataCollector getDataCollector() {
return dataCollector;
}
@ -120,6 +132,9 @@ public class ScenarioContext implements Observer {
}
/**
* Task Management section -------- Start
*
*
* Now, I tolerate that if the requiredLoad <
* this.getExecutor.getCorePoolSize(), then the excess threads will be
* 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() {
this.setFinished(true);
this.getExecutor().shutdownNow();
this.getSchedule().stop();
}
}

View File

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