From 5ff8fb00acd1da9de90e716ed6d26e86b397bb7a Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Fri, 29 Aug 2014 15:34:14 +0800 Subject: [PATCH] remove the bug of taskMaker remove the bug of taskMaker that just make so many junk jobs --- .../org/bench4q/agent/api/HomeController.java | 4 +- .../org/bench4q/agent/api/TestController.java | 9 +- .../agent/datacollector/DataCollector.java | 4 +- .../impl/AbstractDataCollector.java | 2 +- .../impl/PageResultCollector.java | 4 +- .../impl/ScenarioResultCollector.java | 4 +- .../agent/scenario/engine/BehaviorResult.java | 134 ++++++++++++ .../agent/scenario/engine/PageResult.java | 87 ++++++++ .../scenario/engine/ScenarioContext.java | 149 +++++++++++++ .../agent/scenario/engine/ScenarioEngine.java | 79 +++++++ .../agent/scenario/engine/TaskMonitor.java | 26 +++ .../bench4q/agent/scenario/engine/VUser.java | 200 ++++++++++++++++++ .../java/org/bench4q/agent/test/TestBase.java | 4 +- .../agent/test/TestWithScriptFile.java | 4 +- .../datastatistics/DetailStatisticsTest.java | 2 +- .../PageResultStatisticsTest.java | 2 +- .../ScenarioStatisticsTest.java | 4 +- .../agent/test/plugin/Test_ContextPlugin.java | 2 +- .../agent/test/plugin/Test_CsvProvider.java | 2 +- .../agent/test/plugin/Test_HttpPlugin.java | 2 +- .../agent/test/plugin/Test_MongoDBPlugin.java | 2 +- .../test/scenario/Test_ScenarioContext.java | 2 +- .../test/scenario/Test_ScenarioEngine.java | 20 +- .../agent/test/scenario/Test_VUser.java | 2 +- .../test/resources/application-context.xml | 5 +- 25 files changed, 721 insertions(+), 34 deletions(-) create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/BehaviorResult.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/PageResult.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/TaskMonitor.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/HomeController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/HomeController.java index 1077e3a2..c3cc85e9 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/HomeController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/HomeController.java @@ -5,8 +5,8 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -import org.bench4q.agent.scenario.ScenarioContext; -import org.bench4q.agent.scenario.ScenarioEngine; +import org.bench4q.agent.scenario.engine.ScenarioContext; +import org.bench4q.agent.scenario.engine.ScenarioEngine; import org.bench4q.share.models.agent.ServerStatusModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java index 6a007d8c..45ff91ca 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java @@ -10,9 +10,9 @@ import org.apache.log4j.Logger; import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; import org.bench4q.agent.plugin.ParameterFileCollector; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.ScenarioContext; -import org.bench4q.agent.scenario.ScenarioEngine; import org.bench4q.agent.scenario.behavior.Behavior; +import org.bench4q.agent.scenario.engine.ScenarioContext; +import org.bench4q.agent.scenario.engine.ScenarioEngine; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.agent.BehaviorBriefModel; import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; @@ -97,7 +97,7 @@ public class TestController { return null; } } - + private RunScenarioResultModel buildWith(UUID runId) { RunScenarioResultModel result = new RunScenarioResultModel(); result.setRunId(runId); @@ -267,7 +267,8 @@ public class TestController { return new CleanTestResultModel(true); } - @RequestMapping(value = "/updatePopulation/{runId}/{requiredLoad}", method = RequestMethod.POST) + @RequestMapping(value = "/updatePopulation/{runId}/{requiredLoad}", method = { + RequestMethod.POST, RequestMethod.GET }) @ResponseBody public UpdatePopulationModel updatePopulation(@PathVariable UUID runId, @PathVariable int requiredLoad) { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java index 94c13c9f..1e1a073a 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java @@ -3,8 +3,8 @@ package org.bench4q.agent.datacollector; import java.util.Map; import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; -import org.bench4q.agent.scenario.BehaviorResult; -import org.bench4q.agent.scenario.PageResult; +import org.bench4q.agent.scenario.engine.BehaviorResult; +import org.bench4q.agent.scenario.engine.PageResult; public interface DataCollector { public void add(BehaviorResult behaviorResult); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java index 6f846ac7..1ab0ed1d 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java @@ -7,7 +7,7 @@ import java.util.concurrent.Executors; import org.bench4q.agent.Main; import org.bench4q.agent.datacollector.DataCollector; import org.bench4q.agent.helper.ApplicationContextHelper; -import org.bench4q.agent.scenario.BehaviorResult; +import org.bench4q.agent.scenario.engine.BehaviorResult; import org.bench4q.agent.storage.StorageHelper; import org.bench4q.share.models.agent.BehaviorResultModel; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java index f046c746..e75b5a30 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java @@ -4,8 +4,8 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; -import org.bench4q.agent.scenario.BehaviorResult; -import org.bench4q.agent.scenario.PageResult; +import org.bench4q.agent.scenario.engine.BehaviorResult; +import org.bench4q.agent.scenario.engine.PageResult; import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; public class PageResultCollector extends AbstractDataCollector { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java index 53b44994..f58328b6 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java @@ -7,8 +7,8 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -import org.bench4q.agent.scenario.BehaviorResult; -import org.bench4q.agent.scenario.PageResult; +import org.bench4q.agent.scenario.engine.BehaviorResult; +import org.bench4q.agent.scenario.engine.PageResult; import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; /** diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/BehaviorResult.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/BehaviorResult.java new file mode 100644 index 00000000..8c1bf6f5 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/BehaviorResult.java @@ -0,0 +1,134 @@ +package org.bench4q.agent.scenario.engine; + +import java.util.Date; + +public class BehaviorResult { + private String pluginId; + private String pluginName; + private int behaviorId; + private String behaviorName; + private Date startDate; + private Date endDate; + private long responseTime; + private int successCount; + private int failCount; + private boolean shouldBeCountResponseTime; + + private String behaviorUrl; + private long contentLength; + private int statusCode; + private String contentType; + + public String getPluginId() { + return pluginId; + } + + public void setPluginId(String pluginId) { + this.pluginId = pluginId; + } + + public String getPluginName() { + return pluginName; + } + + public void setPluginName(String pluginName) { + this.pluginName = pluginName; + } + + public String getBehaviorName() { + return behaviorName; + } + + public void setBehaviorName(String behaviorName) { + this.behaviorName = behaviorName; + } + + public Date getStartDate() { + return startDate; + } + + public void setStartDate(Date startDate) { + this.startDate = startDate; + } + + public Date getEndDate() { + return endDate; + } + + public void setEndDate(Date endDate) { + this.endDate = endDate; + } + + public long getResponseTime() { + return responseTime; + } + + public void setResponseTime(long responseTime) { + this.responseTime = responseTime; + } + + public int getSuccessCount() { + return successCount; + } + + public void setSuccessCount(int successCount) { + this.successCount = successCount; + } + + public int getFailCount() { + return failCount; + } + + public void setFailCount(int failCount) { + this.failCount = failCount; + } + + public int getBehaviorId() { + return behaviorId; + } + + public void setBehaviorId(int behaviorId) { + this.behaviorId = behaviorId; + } + + public String getBehaviorUrl() { + return behaviorUrl; + } + + public void setBehaviorUrl(String behaviorUrl) { + this.behaviorUrl = behaviorUrl; + } + + public long getContentLength() { + return contentLength; + } + + public void setContentLength(long contentLength) { + this.contentLength = contentLength; + } + + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public boolean isShouldBeCountResponseTime() { + return shouldBeCountResponseTime; + } + + public void setShouldBeCountResponseTime(boolean shouldBeCountResponseTime) { + this.shouldBeCountResponseTime = shouldBeCountResponseTime; + } + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/PageResult.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/PageResult.java new file mode 100644 index 00000000..c8e1e64c --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/PageResult.java @@ -0,0 +1,87 @@ +package org.bench4q.agent.scenario.engine; + +import java.util.ArrayList; +import java.util.List; + +public class PageResult { + private int pageId; + private long pageStartTime; + private long pageEndTime; + private long executeRange; + + public int getPageId() { + return pageId; + } + + private void setPageId(int pageId) { + this.pageId = pageId; + } + + private long getPageStartTime() { + return pageStartTime; + } + + private void setPageStartTime(long pageStartTime) { + this.pageStartTime = pageStartTime; + } + + public long getPageEndTime() { + return pageEndTime; + } + + private void setPageEndTime(long pageEndTime) { + this.pageEndTime = pageEndTime; + } + + public long getExecuteRange() { + return executeRange; + } + + private void setExecuteRange(long executeRange) { + this.executeRange = executeRange; + } + + private PageResult() { + init(); + } + + private void init() { + this.setPageStartTime(Long.MAX_VALUE); + this.setPageEndTime(Long.MIN_VALUE); + this.setExecuteRange(0); + } + + public static PageResult buildPageResult(int pageId, + List behaviorResults) { + PageResult result = new PageResult(); + result.setPageId(pageId); + if (behaviorResults == null) { + behaviorResults = new ArrayList(); + } + for (BehaviorResult behaviorResult : behaviorResults) { + if (behaviorResult.getStartDate().getTime() < result + .getPageStartTime()) { + result.setPageStartTime(behaviorResult.getStartDate().getTime()); + } + if (behaviorResult.getEndDate().getTime() > result.getPageEndTime()) { + result.setPageEndTime(behaviorResult.getEndDate().getTime()); + } + // Page excuteRange rely on the behaviors' execute way, if it's + // executed in batch, i should take the longest behavior in batch + // to calculate this One. + } + result.setExecuteRange(result.getPageEndTime() + - result.getPageStartTime()); + return result; + } + + public static PageResult buildPageResult(int pageId, long startTime, + long endTime) { + PageResult result = new PageResult(); + result.setPageId(pageId); + result.setPageStartTime(startTime); + result.setPageEndTime(endTime); + result.setExecuteRange(endTime - startTime); + return result; + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java new file mode 100644 index 00000000..f742ebcd --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java @@ -0,0 +1,149 @@ +package org.bench4q.agent.scenario.engine; + +import java.util.Date; +import java.util.UUID; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy; + +import org.apache.log4j.Logger; +import org.bench4q.agent.datacollector.DataCollector; +import org.bench4q.agent.datacollector.impl.ScenarioResultCollector; +import org.bench4q.agent.plugin.PluginManager; +import org.bench4q.agent.scenario.Scenario; + +public class ScenarioContext { + private static final long keepAliveTime = 10; + private UUID testId; + private Date startDate; + private Date endDate; + private ThreadPoolExecutor executor; + private Scenario scenario; + private boolean finished; + private DataCollector dataStatistics; + private PluginManager pluginManager; + + public UUID getTestId() { + return testId; + } + + private void setTestId(UUID testId) { + this.testId = testId; + } + + public Date getStartDate() { + return startDate; + } + + public void setStartDate(Date saveStartDate) { + this.startDate = saveStartDate; + } + + public Date getEndDate() { + return endDate; + } + + public void setEndDate(Date endDate) { + this.endDate = endDate; + } + + public ThreadPoolExecutor getExecutor() { + return executor; + } + + public void setExecutorService(ThreadPoolExecutor executor) { + this.executor = executor; + } + + public Scenario getScenario() { + return scenario; + } + + public void setScenario(Scenario scenario) { + this.scenario = scenario; + } + + public boolean isFinished() { + return finished; + } + + public void setFinished(boolean finished) { + this.finished = finished; + } + + public DataCollector getDataStatistics() { + return dataStatistics; + } + + private void setDataStatistics(DataCollector dataStatistics) { + this.dataStatistics = dataStatistics; + } + + private PluginManager getPluginManager() { + return pluginManager; + } + + private void setPluginManager(PluginManager pluginManager) { + this.pluginManager = pluginManager; + } + + private ScenarioContext() { + } + + public static ScenarioContext buildScenarioContext(UUID testId, + final Scenario scenario, int poolSize, PluginManager pluginManager) { + ScenarioContext scenarioContext = buildScenarioContextWithoutScenario( + testId, poolSize, pluginManager); + scenarioContext.setScenario(scenario); + return scenarioContext; + } + + public static ScenarioContext buildScenarioContextWithoutScenario( + UUID testId, int poolSize, PluginManager pluginManager) { + ScenarioContext scenarioContext = new ScenarioContext(); + scenarioContext.setTestId(testId); + scenarioContext.setPluginManager(pluginManager); + final ArrayBlockingQueue workQueue = new ArrayBlockingQueue( + poolSize); + ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize, + poolSize, keepAliveTime, TimeUnit.MINUTES, workQueue, + new DiscardPolicy()); + scenarioContext.setStartDate(new Date(System.currentTimeMillis())); + scenarioContext.setExecutorService(executor); + scenarioContext.setDataStatistics(new ScenarioResultCollector(testId)); + return scenarioContext; + } + + public ScenarioContext addScenrio(Scenario scenario) { + this.setScenario(scenario); + return this; + } + + /** + * Now, I tolerate that if the requiredLoad < + * this.getExecutor.getCorePoolSize(), then the excess threads will be + * killed when its current task complete + * + * @param requiredLoad + */ + void updatePopulation(int requiredLoad) { + this.getExecutor().setCorePoolSize(requiredLoad); + this.getExecutor().setMaximumPoolSize(requiredLoad); + } + + public void addTask() { + if (this.isFinished()) { + return; + } + this.getExecutor().execute(new VUser(this, 1, getPluginManager())); + Logger.getLogger(this.getClass()).info( + this.getExecutor().getActiveCount()); + } + + public void initTasks() { + for (int i = 0; i < this.getExecutor().getCorePoolSize(); i++) { + addTask(); + } + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java new file mode 100644 index 00000000..a9d3b0f9 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java @@ -0,0 +1,79 @@ +package org.bench4q.agent.scenario.engine; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import org.apache.log4j.Logger; +import org.bench4q.agent.plugin.PluginManager; +import org.bench4q.agent.scenario.Scenario; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ScenarioEngine { + private Map runningTests; + private Logger logger = Logger.getLogger(ScenarioEngine.class); + private PluginManager pluginManager; + + public ScenarioEngine() { + this.setRunningTests(new HashMap()); + } + + public Map getRunningTests() { + return runningTests; + } + + private void setRunningTests(Map runningTests) { + this.runningTests = runningTests; + } + + private PluginManager getPluginManager() { + return pluginManager; + } + + @Autowired + private void setPluginManager(PluginManager pluginManager) { + this.pluginManager = pluginManager; + } + + public void addRunningTestWithoutScenario(UUID runId, int poolSize) { + try { + final ScenarioContext scenarioContext = ScenarioContext + .buildScenarioContextWithoutScenario(runId, poolSize, + getPluginManager()); + this.getRunningTests().put(runId, scenarioContext); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void submitScenario(UUID runId, final Scenario scenario) { + try { + this.getRunningTests().get(runId).addScenrio(scenario); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public boolean runWith(UUID runId) { + if (!this.getRunningTests().containsKey(runId)) { + return false; + } + final ScenarioContext context = this.getRunningTests().get(runId); + return runWith(context); + } + + private boolean runWith(final ScenarioContext scenarioContext) { + if (scenarioContext == null) { + logger.error("The context required is null"); + return false; + } + scenarioContext.initTasks(); + return true; + } + + public void updatePopulation(UUID testId, int requiredLoad) { + ScenarioContext context = this.getRunningTests().get(testId); + context.updatePopulation(requiredLoad); + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/TaskMonitor.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/TaskMonitor.java new file mode 100644 index 00000000..3f4adc0a --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/TaskMonitor.java @@ -0,0 +1,26 @@ +package org.bench4q.agent.scenario.engine; + +public class TaskMonitor { + + public static class TaskProducer implements Runnable { + private ScenarioContext context; + + public TaskProducer(ScenarioContext context) { + this.context = context; + } + + @Override + public void run() { + int poolSize = context.getExecutor().getCorePoolSize(); + try { + if (context.getExecutor().getTaskCount() > poolSize * 2) { + this.wait(); + } + } catch (Exception e) { + + } finally { + + } + } + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java new file mode 100644 index 00000000..4d158553 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java @@ -0,0 +1,200 @@ +package org.bench4q.agent.scenario.engine; + +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.bench4q.agent.datacollector.DataCollector; +import org.bench4q.agent.plugin.ParameterBarn; +import org.bench4q.agent.plugin.Plugin; +import org.bench4q.agent.plugin.PluginManager; +import org.bench4q.agent.plugin.basic.PluginReturn; +import org.bench4q.agent.plugin.basic.http.HttpReturn; +import org.bench4q.agent.scenario.Batch; +import org.bench4q.agent.scenario.Page; +import org.bench4q.agent.scenario.Parameter; +import org.bench4q.agent.scenario.Scenario; +import org.bench4q.agent.scenario.UsePlugin; +import org.bench4q.agent.scenario.behavior.Behavior; +import org.bench4q.agent.scenario.dfa.ParamPart; + +public class VUser implements Runnable { + private ScenarioContext scenarioContext; + private int currentIterationId; + + private PluginManager pluginManager; + + private int getCurrentIterationId() { + return currentIterationId; + } + + private void setCurrentIterationId(int currentIterationId) { + this.currentIterationId = currentIterationId; + } + + private ScenarioContext getScenarioContext() { + return scenarioContext; + } + + private void setScenarioContext(ScenarioContext scenarioContext) { + this.scenarioContext = scenarioContext; + } + + private PluginManager getPluginManager() { + return pluginManager; + } + + private void setPluginManager(PluginManager pluginManager) { + this.pluginManager = pluginManager; + } + + public VUser(ScenarioContext scenarioContext, int currentIterationId, + PluginManager pluginManager) { + this.setScenarioContext(scenarioContext); + this.setPluginManager(pluginManager); + this.setCurrentIterationId(currentIterationId); + } + + public void run() { + doRunScenario(getScenarioContext()); + doCleanUp(); + } + + private void doCleanUp() { + this.getScenarioContext().addTask(); + this.setScenarioContext(null); + } + + private void doRunScenario(ScenarioContext context) { + Map plugins = new LinkedHashMap(); + preparePlugins(context.getScenario(), plugins); + for (int i = 0; i < context.getScenario().getPages().length; i++) { + Page page = context.getScenario().getPages()[i]; + long startTime = System.currentTimeMillis(); + for (Batch batch : page.getBatches()) { + doRunBatch(plugins, batch, context.getDataStatistics()); + } + long endTime = System.currentTimeMillis(); + context.getDataStatistics().add( + PageResult.buildPageResult(i, startTime, endTime)); + } + } + + private void doRunBatch(Map plugins, Batch batch, + DataCollector dataCollector) { + for (Behavior behavior : batch.getBehaviors()) { + Object plugin = plugins.get(behavior.getUse()); + Date startDate = new Date(System.currentTimeMillis()); + PluginReturn pluginReturn = (PluginReturn) this.getPluginManager() + .doBehavior(plugin, behavior.getName(), + reassamblyParameters(behavior, plugins)); + Date endDate = new Date(System.currentTimeMillis()); + if (!behavior.shouldBeCount()) { + continue; + } + dataCollector.add(buildBehaviorResult(behavior, plugin, startDate, + pluginReturn, endDate)); + } + } + + private Map reassamblyParameters(Behavior behavior, + Map plugins) { + Map behaviorParameters = new HashMap(); + for (Parameter parameter : behavior.getParameters()) { + behaviorParameters.put(parameter.getKey(), + reassamblyParameter(parameter.getParamParts(), plugins)); + } + return behaviorParameters; + } + + private String reassamblyParameter(ParamPart[] parts, + Map plugins) { + StringBuilder buf = new StringBuilder(); + for (int i = 0, len = parts.length; i < len; i++) { + switch (parts[i].getType()) { + case STRING: + buf.append(parts[i].getContentForStringType()); + break; + case SESSION_ID: + buf.append(this.getCurrentIterationId()); + break; + case CONTEXT_CALL: + try { + Object dpObj = plugins.get(parts[i] + .getPluginIdForContextCallType()); + if (dpObj == null) { + throw new RuntimeException("No such plug-in id: " + + parts[i].getPluginIdForContextCallType()); + } else if (dpObj instanceof ParameterBarn) { + buf.append(((ParameterBarn) dpObj).getValue(parts[i] + .getVariableForContextCallAndProperty())); + } else { + throw new RuntimeException("Plug-in " + + parts[i].getPluginIdForContextCallType() + + " is not a data provider."); + } + } catch (Exception e) { + Logger.getLogger(VUser.class) + .error(parts[i].getPluginIdForContextCallType() + + "variable " + + parts[i] + .getVariableForContextCallAndProperty(), + e); + } + break; + case PROPERTY: + buf.append(System.getProperty( + parts[i].getVariableForContextCallAndProperty(), "")); + break; + } + } + return buf.toString(); + } + + private BehaviorResult buildBehaviorResult(Behavior behavior, + Object plugin, Date startDate, PluginReturn pluginReturn, + Date endDate) { + BehaviorResult result = new BehaviorResult(); + result.setBehaviorId(behavior.getId()); + result.setStartDate(startDate); + result.setEndDate(endDate); + result.setSuccessCount(pluginReturn.getSuccessCount()); + result.setFailCount(pluginReturn.getFailCount()); + result.setResponseTime(endDate.getTime() - startDate.getTime()); + result.setBehaviorName(behavior.getName()); + result.setPluginId(behavior.getUse()); + result.setPluginName(plugin.getClass().getAnnotation(Plugin.class) + .value()); + result.setShouldBeCountResponseTime(behavior.shouldBeCount()); + if (pluginReturn instanceof HttpReturn) { + HttpReturn httpReturn = (HttpReturn) pluginReturn; + // TODO: this param in result is not Appropriate + result.setBehaviorUrl(behavior.getSpecificParamValue("url")); + result.setContentLength(httpReturn.getContentLength()); + result.setContentType(httpReturn.getContentType()); + result.setStatusCode(httpReturn.getStatusCode()); + } + return result; + } + + private void preparePlugins(Scenario scenario, Map plugins) { + for (UsePlugin usePlugin : scenario.getUsePlugins()) { + String pluginId = usePlugin.getId(); + Class pluginClass = this.getPluginManager().getPlugins() + .get(usePlugin.getName()); + Map initParameters = new HashMap(); + for (Parameter parameter : usePlugin.getParameters()) { + initParameters.put(parameter.getKey(), parameter.getValue()); + } + Map extraParameters = new HashMap(); + extraParameters.put("testId", this.getScenarioContext().getTestId() + .toString()); + Object plugin = this.getPluginManager().initializePlugin( + pluginClass, initParameters, extraParameters); + plugins.put(pluginId, plugin); + } + } + +} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase.java index ccde802d..5d98ba51 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase.java @@ -10,8 +10,8 @@ import org.apache.commons.io.FileUtils; import org.bench4q.agent.plugin.ParameterBarn; import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.ScenarioContext; -import org.bench4q.agent.scenario.VUser; +import org.bench4q.agent.scenario.engine.ScenarioContext; +import org.bench4q.agent.scenario.engine.VUser; import org.bench4q.share.helper.TestHelper; import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.RunScenarioModel; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java index c610ca04..f929c8ff 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java @@ -62,7 +62,7 @@ public class TestWithScriptFile { public TestWithScriptFile() { this.setFilePath("Scripts" + System.getProperty("file.separator") - + "httpWithoutTimer.xml"); + + "homepage.xml"); this.setHttpRequester(new HttpRequester()); } @@ -193,7 +193,7 @@ public class TestWithScriptFile { files.add(new File("Scripts" + System.getProperty("file.separator") + "testJD.xml")); HttpResponse httpResponse1 = this.getHttpRequester().sendPost( - url + "/bookTest/10", null, null); + url + "/bookTest/100", null, null); RunScenarioResultModel bookResponse = (RunScenarioResultModel) MarshalHelper .tryUnmarshal(RunScenarioResultModel.class, httpResponse1.getContent()); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java index ca586ba9..f5bebd3d 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java @@ -9,7 +9,7 @@ import java.util.UUID; import org.bench4q.agent.datacollector.DataCollector; import org.bench4q.agent.datacollector.impl.ScenarioResultCollector; import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; -import org.bench4q.agent.scenario.BehaviorResult; +import org.bench4q.agent.scenario.engine.BehaviorResult; import org.bench4q.agent.storage.StorageHelper; import org.junit.Test; import org.springframework.context.ApplicationContext; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java index 2aa57c88..671bf4ea 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java @@ -2,7 +2,7 @@ package org.bench4q.agent.test.datastatistics; import org.bench4q.agent.datacollector.DataCollector; import org.bench4q.agent.datacollector.impl.PageResultCollector; -import org.bench4q.agent.scenario.PageResult; +import org.bench4q.agent.scenario.engine.PageResult; import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java index 9271b126..b24923d4 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java @@ -9,8 +9,8 @@ import java.util.UUID; import org.bench4q.agent.datacollector.DataCollector; import org.bench4q.agent.datacollector.impl.ScenarioResultCollector; -import org.bench4q.agent.scenario.BehaviorResult; -import org.bench4q.agent.scenario.PageResult; +import org.bench4q.agent.scenario.engine.BehaviorResult; +import org.bench4q.agent.scenario.engine.PageResult; import org.bench4q.agent.storage.StorageHelper; import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; import org.junit.Test; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ContextPlugin.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ContextPlugin.java index 6b3d885f..498964a6 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ContextPlugin.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ContextPlugin.java @@ -6,7 +6,7 @@ import java.util.UUID; import org.bench4q.agent.plugin.basic.context.Context; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.VUser; +import org.bench4q.agent.scenario.engine.VUser; import org.bench4q.agent.test.TestBase; import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.RunScenarioModel; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_CsvProvider.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_CsvProvider.java index cf4c6c8b..2c8c55ed 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_CsvProvider.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_CsvProvider.java @@ -13,7 +13,7 @@ import java.util.UUID; import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.plugin.basic.csvprovider.CsvProvider; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.VUser; +import org.bench4q.agent.scenario.engine.VUser; import org.bench4q.agent.test.TestBase; import org.bench4q.share.helper.TestHelper; import org.bench4q.share.models.agent.ParameterModel; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java index 97af7505..97ebad8b 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java @@ -16,7 +16,7 @@ import org.bench4q.agent.plugin.basic.http.BatchRequest; import org.bench4q.agent.plugin.basic.http.HttpPlugin; import org.bench4q.agent.plugin.basic.http.HttpReturn; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.VUser; +import org.bench4q.agent.scenario.engine.VUser; import org.bench4q.agent.test.TestBase; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.helper.TestHelper; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java index 4b5a0ded..627e4f4f 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java @@ -12,7 +12,7 @@ import org.apache.commons.io.FileUtils; import org.bench4q.agent.plugin.basic.MongoDB.MongoDBPlugin; import org.bench4q.agent.plugin.basic.MongoDB.MongoDBReturn; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.VUser; +import org.bench4q.agent.scenario.engine.VUser; import org.bench4q.agent.test.TestBase; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.helper.TestHelper; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java index 8cf48bc2..3bd6344b 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java @@ -6,7 +6,7 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.ScenarioContext; +import org.bench4q.agent.scenario.engine.ScenarioContext; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java index 890fca0d..6a9e1d35 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java @@ -2,12 +2,15 @@ package org.bench4q.agent.test.scenario; import static org.junit.Assert.*; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.UUID; +import org.apache.commons.io.FileUtils; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.ScenarioContext; -import org.bench4q.agent.scenario.ScenarioEngine; +import org.bench4q.agent.scenario.engine.ScenarioContext; +import org.bench4q.agent.scenario.engine.ScenarioEngine; import org.bench4q.agent.test.TestBase; import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; @@ -22,6 +25,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations = { "classpath:application-context.xml" }) public class Test_ScenarioEngine extends TestBase { private ScenarioEngine scenarioEngine; + private UUID testId = UUID.randomUUID(); private ScenarioEngine getScenarioEngine() { return scenarioEngine; @@ -34,7 +38,6 @@ public class Test_ScenarioEngine extends TestBase { @Test public void test_UpdatePopulation() { - UUID testId = UUID.randomUUID(); ScenarioContext scenarioContext = ScenarioContext .buildScenarioContext( testId, @@ -49,4 +52,15 @@ public class Test_ScenarioEngine extends TestBase { assertEquals(20, scenarioContext.getExecutor().getMaximumPoolSize()); System.out.println(scenarioContext.getExecutor().getActiveCount()); } + + @Test + public void test_RunWithContext() throws IOException { + ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( + testId, Scenario.scenarioBuilderWithCompile(FileUtils + .readFileToString(new File("Scripts" + + System.getProperty("file.separator") + + "homepage.xml"))), 100); + this.getScenarioEngine().getRunningTests().put(testId, scenarioContext); + this.getScenarioEngine().runWith(testId); + } } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java index f949af49..b4bda65a 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java @@ -9,8 +9,8 @@ import static org.junit.Assert.*; import org.bench4q.agent.plugin.basic.csvprovider.CsvProvider; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.VUser; import org.bench4q.agent.scenario.behavior.Behavior; +import org.bench4q.agent.scenario.engine.VUser; import org.bench4q.agent.test.TestBase; import org.bench4q.share.helper.TestHelper; import org.bench4q.share.models.agent.ParameterModel; diff --git a/Bench4Q-Agent/src/test/resources/application-context.xml b/Bench4Q-Agent/src/test/resources/application-context.xml index 6013e944..6c72d7ff 100644 --- a/Bench4Q-Agent/src/test/resources/application-context.xml +++ b/Bench4Q-Agent/src/test/resources/application-context.xml @@ -5,9 +5,6 @@ xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> - - + -