refactor the tests

refactor the tests
This commit is contained in:
coderfengyun 2014-08-29 15:43:15 +08:00
parent 22cd79bb4e
commit 24a529451e
15 changed files with 33 additions and 662 deletions

View File

@ -3,30 +3,30 @@
<ui>
<plugin name="MongoDBPlugin">
<params>
<param name="hostName" label="hostName">
<param name="hostName" label="hostName" required="true">
<field size="7" />
</param>
<param name="port" label="port">
<param name="port" label="port" required="true">
<field size="7" />
</param>
<param name="dbname" label="dbname">
<param name="dbname" label="dbname" required="true">
<field size="7" />
</param>
</params>
</plugin>
<behavior name="Insert">
<params>
<param name="key" label="key">
<param name="key" label="key" required="true">
<field size="7" />
</param>
<param name="value" label="value">
<param name="value" label="value" required="true">
<field size="7" />
</param>
</params>
</behavior>
<behavior name="Query">
<params>
<param name="properties" label="properties">
<param name="properties" label="properties" required="true">
<table cols="propertyName;propertyValue" />
</param>
</params>

View File

@ -6,7 +6,7 @@
</plugin>
<behavior name="Command">
<params>
<param name="command">
<param name="command" required="true">
<field size="6"></field>
</param>
</params>

View File

@ -6,7 +6,7 @@
</plugin>
<control name="Set">
<params>
<param name="toSave">
<param name="toSave" required="true">
<field size="7"></field>
</param>
</params>

View File

@ -3,10 +3,11 @@
<ui>
<plugin name="IteratorNumber">
<params>
<param name="begin" label="The Beginning of the Iteration">
<param name="begin" label="The Beginning of the Iteration"
required="true">
<field size="5"></field>
</param>
<param name="format" label="The format of the value">
<param name="format" label="The format of the value" required="false">
<field size="5"></field>
</param>
</params>

View File

@ -6,7 +6,7 @@
</plugin>
<behavior name="Log">
<params>
<param name="message">
<param name="message" required="true">
<field size="4" />
</param>
</params>

View File

@ -3,10 +3,11 @@
<ui>
<plugin name="UniqueNumber">
<params>
<param name="begin" label="The Beginning of the Iteration">
<param name="begin" label="The Beginning of the Iteration"
required="true">
<field size="5"></field>
</param>
<param name="format" label="The format of the value">
<param name="format" label="The format of the value" required="true">
<field size="5"></field>
</param>
</params>

View File

@ -20,7 +20,7 @@
<!ATTLIST param
name CDATA #REQUIRED
label CDATA #IMPLIED
required CDATA #REQUIRED
required CDATA #REQUIRED
>
<!ELEMENT group (param|group)*>
<!ATTLIST group

View File

@ -1,134 +0,0 @@
package org.bench4q.agent.scenario;
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;
}
}

View File

@ -1,87 +0,0 @@
package org.bench4q.agent.scenario;
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<BehaviorResult> behaviorResults) {
PageResult result = new PageResult();
result.setPageId(pageId);
if (behaviorResults == null) {
behaviorResults = new ArrayList<BehaviorResult>();
}
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;
}
}

View File

@ -1,128 +0,0 @@
package org.bench4q.agent.scenario;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.datacollector.impl.ScenarioResultCollector;
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;
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 ScenarioContext() {
}
public static ScenarioContext buildScenarioContext(UUID testId,
final Scenario scenario, int poolSize) {
ScenarioContext scenarioContext = new ScenarioContext();
scenarioContext.setTestId(testId);
final SynchronousQueue<Runnable> workQueue = new SynchronousQueue<Runnable>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize,
poolSize, keepAliveTime, TimeUnit.MINUTES, workQueue,
new DiscardPolicy());
scenarioContext.setScenario(scenario);
scenarioContext.setStartDate(new Date(System.currentTimeMillis()));
scenarioContext.setExecutorService(executor);
scenarioContext.setDataStatistics(new ScenarioResultCollector(testId));
return scenarioContext;
}
public static ScenarioContext buildScenarioContextWithoutScenario(
UUID testId, int poolSize) {
ScenarioContext scenarioContext = new ScenarioContext();
scenarioContext.setTestId(testId);
final SynchronousQueue<Runnable> workQueue = new SynchronousQueue<Runnable>();
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);
}
}

View File

@ -1,93 +0,0 @@
package org.bench4q.agent.scenario;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.PluginManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ScenarioEngine {
private Map<UUID, ScenarioContext> runningTests;
private Logger logger = Logger.getLogger(ScenarioEngine.class);
private PluginManager pluginManager;
public ScenarioEngine() {
this.setRunningTests(new HashMap<UUID, ScenarioContext>());
}
public Map<UUID, ScenarioContext> getRunningTests() {
return runningTests;
}
private void setRunningTests(Map<UUID, ScenarioContext> 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);
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, runId);
}
private boolean runWith(final ScenarioContext scenarioContext,
final UUID runId) {
if (scenarioContext == null) {
logger.error("The context required is null");
return false;
}
ExecutorService taskMaker = Executors.newSingleThreadExecutor();
taskMaker.execute(new Runnable() {
public void run() {
int currentIterationId = 0;
while (!scenarioContext.isFinished()) {
scenarioContext.getExecutor().execute(
new VUser(scenarioContext, currentIterationId,
getPluginManager()));
currentIterationId++;
}
}
});
return true;
}
public void updatePopulation(UUID testId, int requiredLoad) {
ScenarioContext context = this.getRunningTests().get(testId);
context.updatePopulation(requiredLoad);
}
}

View File

@ -1,194 +0,0 @@
package org.bench4q.agent.scenario;
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.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.setScenarioContext(null);
}
private void doRunScenario(ScenarioContext context) {
Map<String, Object> plugins = new LinkedHashMap<String, Object>();
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<String, Object> 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<String, String> reassamblyParameters(Behavior behavior,
Map<String, Object> plugins) {
Map<String, String> behaviorParameters = new HashMap<String, String>();
for (Parameter parameter : behavior.getParameters()) {
behaviorParameters.put(parameter.getKey(),
reassamblyParameter(parameter.getParamParts(), plugins));
}
return behaviorParameters;
}
private String reassamblyParameter(ParamPart[] parts,
Map<String, Object> 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<String, Object> plugins) {
for (UsePlugin usePlugin : scenario.getUsePlugins()) {
String pluginId = usePlugin.getId();
Class<?> pluginClass = this.getPluginManager().getPlugins()
.get(usePlugin.getName());
Map<String, String> initParameters = new HashMap<String, String>();
for (Parameter parameter : usePlugin.getParameters()) {
initParameters.put(parameter.getKey(), parameter.getValue());
}
Map<String, String> extraParameters = new HashMap<String, String>();
extraParameters.put("testId", this.getScenarioContext().getTestId()
.toString());
Object plugin = this.getPluginManager().initializePlugin(
pluginClass, initParameters, extraParameters);
plugins.put(pluginId, plugin);
}
}
}

View File

@ -111,6 +111,6 @@ public abstract class TestBase {
public VUser createVUser(Scenario scenario, UUID testId) {
return new VUser(ScenarioContext.buildScenarioContext(testId, scenario,
10), 1, this.getPluginManager());
10, pluginManager), 1, this.getPluginManager());
}
}

View File

@ -5,21 +5,25 @@ import static org.junit.Assert.*;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.engine.ScenarioContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
public class Test_ScenarioContext {
@Autowired
private PluginManager pluginManager;
@Test
public void testBuildScenarioContext() {
ScenarioContext context = ScenarioContext.buildScenarioContext(
UUID.randomUUID(), new Scenario(), 20);
UUID.randomUUID(), new Scenario(), 20, this.pluginManager);
assertEquals(10,
context.getExecutor().getKeepAliveTime(TimeUnit.MINUTES));
assertEquals(20, context.getExecutor().getMaximumPoolSize());

View File

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.engine.ScenarioContext;
import org.bench4q.agent.scenario.engine.ScenarioEngine;
@ -26,6 +27,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class Test_ScenarioEngine extends TestBase {
private ScenarioEngine scenarioEngine;
private UUID testId = UUID.randomUUID();
@Autowired
private PluginManager pluginManager;
private ScenarioEngine getScenarioEngine() {
return scenarioEngine;
@ -38,15 +41,13 @@ public class Test_ScenarioEngine extends TestBase {
@Test
public void test_UpdatePopulation() {
ScenarioContext scenarioContext = ScenarioContext
.buildScenarioContext(
testId,
Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith(
new ArrayList<UsePluginModel>(),
BehaviorModel
.UserBehaviorBuilder(1, "ok", "http",
new ArrayList<ParameterModel>()))),
100);
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext(
testId,
Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith(
new ArrayList<UsePluginModel>(), BehaviorModel
.UserBehaviorBuilder(1, "ok", "http",
new ArrayList<ParameterModel>()))),
100, this.pluginManager);
this.getScenarioEngine().getRunningTests().put(testId, scenarioContext);
this.getScenarioEngine().updatePopulation(testId, 20);
assertEquals(20, scenarioContext.getExecutor().getMaximumPoolSize());
@ -59,7 +60,7 @@ public class Test_ScenarioEngine extends TestBase {
testId, Scenario.scenarioBuilderWithCompile(FileUtils
.readFileToString(new File("Scripts"
+ System.getProperty("file.separator")
+ "homepage.xml"))), 100);
+ "homepage.xml"))), 100, this.pluginManager);
this.getScenarioEngine().getRunningTests().put(testId, scenarioContext);
this.getScenarioEngine().runWith(testId);
}