now we can use the csvProvider as the

now we can use the csvProvider as the
This commit is contained in:
coderfengyun 2014-04-15 15:19:29 +08:00
parent 8402fe13d9
commit b6626afe2a
10 changed files with 249 additions and 102 deletions

View File

@ -165,7 +165,7 @@ public class Scenario {
} }
} }
private static Behavior extractBehavior(BehaviorModel behaviorModel) { public static Behavior extractBehavior(BehaviorModel behaviorModel) {
Behavior behavior = BehaviorFactory.getBuisinessObject(behaviorModel); Behavior behavior = BehaviorFactory.getBuisinessObject(behaviorModel);
behavior.setName(behaviorModel.getName()); behavior.setName(behaviorModel.getName());
behavior.setUse(behaviorModel.getUse()); behavior.setUse(behaviorModel.getUse());

View File

@ -78,9 +78,11 @@ public class ScenarioEngine {
taskMaker.execute(new Runnable() { taskMaker.execute(new Runnable() {
public void run() { public void run() {
int currentIterationId = 0;
while (!scenarioContext.isFinished()) { while (!scenarioContext.isFinished()) {
scenarioContext.getExecutor().execute( scenarioContext.getExecutor().execute(
new VUser(scenarioContext)); new VUser(scenarioContext, currentIterationId));
currentIterationId++;
} }
} }
}); });

View File

@ -7,21 +7,33 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.log4j.Logger;
import org.bench4q.agent.datacollector.DataCollector; import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.helper.ApplicationContextHelper; import org.bench4q.agent.helper.ApplicationContextHelper;
import org.bench4q.agent.parameterization.SessionObject; import org.bench4q.agent.parameterization.SessionObject;
import org.bench4q.agent.plugin.BaseParameterization;
import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.plugin.basic.PluginReturn; import org.bench4q.agent.plugin.basic.PluginReturn;
import org.bench4q.agent.plugin.basic.http.HttpReturn; import org.bench4q.agent.plugin.basic.http.HttpReturn;
import org.bench4q.agent.scenario.behavior.Behavior; import org.bench4q.agent.scenario.behavior.Behavior;
import org.bench4q.agent.scenario.dfa.ParamPart;
public class VUser implements Runnable { public class VUser implements Runnable {
private ScenarioContext scenarioContext; private ScenarioContext scenarioContext;
private SessionObject sessionObject; private SessionObject sessionObject;
private int currentIterationId;
private PluginManager pluginManager; private PluginManager pluginManager;
private int getCurrentIterationId() {
return currentIterationId;
}
private void setCurrentIterationId(int currentIterationId) {
this.currentIterationId = currentIterationId;
}
private ScenarioContext getScenarioContext() { private ScenarioContext getScenarioContext() {
return scenarioContext; return scenarioContext;
} }
@ -46,11 +58,12 @@ public class VUser implements Runnable {
this.pluginManager = pluginManager; this.pluginManager = pluginManager;
} }
public VUser(ScenarioContext scenarioContext) { public VUser(ScenarioContext scenarioContext, int currentIterationId) {
this.setScenarioContext(scenarioContext); this.setScenarioContext(scenarioContext);
this.setPluginManager(ApplicationContextHelper.getContext().getBean( this.setPluginManager(ApplicationContextHelper.getContext().getBean(
PluginManager.class)); PluginManager.class));
this.setSessionObject(scenarioContext.buildVUserContext()); this.setSessionObject(scenarioContext.buildVUserContext());
this.setCurrentIterationId(currentIterationId);
} }
public void run() { public void run() {
@ -93,10 +106,10 @@ public class VUser implements Runnable {
for (Behavior behavior : batch.getBehaviors()) { for (Behavior behavior : batch.getBehaviors()) {
behavior.distillParams(this.getSessionObject()); behavior.distillParams(this.getSessionObject());
Object plugin = plugins.get(behavior.getUse()); Object plugin = plugins.get(behavior.getUse());
Map<String, String> behaviorParameters = prepareBehaviorParameters(behavior);
Date startDate = new Date(System.currentTimeMillis()); Date startDate = new Date(System.currentTimeMillis());
PluginReturn pluginReturn = (PluginReturn) this.getPluginManager() PluginReturn pluginReturn = (PluginReturn) this.getPluginManager()
.doBehavior(plugin, behavior.getName(), behaviorParameters); .doBehavior(plugin, behavior.getName(),
reassamblyParameters(behavior, plugins));
extractRunTimeParams(pluginReturn); extractRunTimeParams(pluginReturn);
@ -113,6 +126,60 @@ public class VUser implements Runnable {
return results; return results;
} }
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 BaseParameterization) {
buf.append(((BaseParameterization) 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 void extractRunTimeParams(PluginReturn pluginReturn) { private void extractRunTimeParams(PluginReturn pluginReturn) {
if (pluginReturn == null || !pluginReturn.hasRunTimeParams()) if (pluginReturn == null || !pluginReturn.hasRunTimeParams())
return; return;
@ -150,19 +217,6 @@ public class VUser implements Runnable {
return result; return result;
} }
private Map<String, String> prepareBehaviorParameters(Behavior behavior) {
Map<String, String> behaviorParameters = new HashMap<String, String>();
for (Parameter parameter : behavior.getParameters()) {
compile(parameter.getValue());
behaviorParameters.put(parameter.getKey(), parameter.getValue());
}
return behaviorParameters;
}
private void compile(String value) {
// TODO: Add the state pattern to this and parse the value
}
private void preparePlugins(Scenario scenario, Map<String, Object> plugins) { private void preparePlugins(Scenario scenario, Map<String, Object> plugins) {
for (UsePlugin usePlugin : scenario.getUsePlugins()) { for (UsePlugin usePlugin : scenario.getUsePlugins()) {
String pluginId = usePlugin.getId(); String pluginId = usePlugin.getId();

View File

@ -2,11 +2,19 @@ package org.bench4q.agent.test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.bench4q.agent.plugin.BaseParameterization; import org.bench4q.agent.plugin.BaseParameterization;
import org.bench4q.share.helper.TestHelper; import org.bench4q.share.helper.TestHelper;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.PageModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
public abstract class TestBase_Parameterization { public abstract class TestBase_Parameterization {
private BaseParameterization basePara; private BaseParameterization basePara;
@ -31,4 +39,59 @@ public abstract class TestBase_Parameterization {
File dirFile = new File(dirPath); File dirFile = new File(dirPath);
FileUtils.deleteDirectory(dirFile); FileUtils.deleteDirectory(dirFile);
} }
protected UsePluginModel buildCsvProviderWith(final String id,
final String fileName, final String fieldNames) {
UsePluginModel usePluginModel = new UsePluginModel() {
{
setId(id);
setName("CsvProvider");
setParameters(new ArrayList<ParameterModel>() {
private static final long serialVersionUID = 1L;
{
add(ParameterModel
.createParameter("fileName", fileName));
add(ParameterModel.createParameter("separator", ","));
add(ParameterModel.createParameter("fieldNames",
fieldNames));
add(ParameterModel.createParameter("shared",
"Notenable"));
add(ParameterModel.createParameter("loop", "enable"));
add(ParameterModel.createParameter("loadAtRunTime",
"Notenable"));
add(ParameterModel.createParameter("commentPrefix",
null));
}
});
}
};
return usePluginModel;
}
public RunScenarioModel buildRunScenarioModelWith(
List<UsePluginModel> plugins, BehaviorModel... behaviors) {
RunScenarioModel runScenarioModel = new RunScenarioModel();
UsePluginModel httpPlugin = new UsePluginModel() {
{
setId("http");
setName("Http");
setParameters(new ArrayList<ParameterModel>());
}
};
for (UsePluginModel pluginModel : plugins) {
runScenarioModel.getUsePlugins().add(pluginModel);
}
runScenarioModel.getUsePlugins().add(httpPlugin);
PageModel page = new PageModel();
BatchModel batch = new BatchModel();
batch.setBehaviors(new ArrayList<BehaviorModel>());
for (BehaviorModel behavior : behaviors) {
batch.getBehaviors().add(behavior);
}
page.getBatches().add(batch);
runScenarioModel.getPages().add(page);
return runScenarioModel;
}
} }

View File

@ -56,12 +56,12 @@ public class Test_ParameterManager extends TestBase_Parameterization {
UpdateStrategy.ONCE.name()))); UpdateStrategy.ONCE.name())));
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext(
testId, scenario, 1); testId, scenario, 1);
VUser vUser = new VUser(scenarioContext); VUser vUser = new VUser(scenarioContext, 1);
String v1_firstValue = vUser.getParam(paramName); String v1_firstValue = vUser.getParam(paramName);
String v1_secondValue = vUser.getParam(paramName); String v1_secondValue = vUser.getParam(paramName);
assertEquals("12", v1_firstValue); assertEquals("12", v1_firstValue);
assertEquals(v1_firstValue, v1_secondValue); assertEquals(v1_firstValue, v1_secondValue);
VUser vUser2 = new VUser(scenarioContext); VUser vUser2 = new VUser(scenarioContext, 1);
String v2_firstValue = vUser2.getParam(paramName); String v2_firstValue = vUser2.getParam(paramName);
String v2_secondValue = vUser2.getParam(paramName); String v2_secondValue = vUser2.getParam(paramName);
assertEquals(v1_firstValue, v2_firstValue); assertEquals(v1_firstValue, v2_firstValue);
@ -80,12 +80,12 @@ public class Test_ParameterManager extends TestBase_Parameterization {
UpdateStrategy.EACH_ITERATION.name()))); UpdateStrategy.EACH_ITERATION.name())));
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext(
testId, scenario, 1); testId, scenario, 1);
VUser vUser = new VUser(scenarioContext); VUser vUser = new VUser(scenarioContext, 1);
String v1_firstValue = vUser.getParam(paramName); String v1_firstValue = vUser.getParam(paramName);
String v1_secondeValue = vUser.getParam(paramName); String v1_secondeValue = vUser.getParam(paramName);
assertNotEquals(paramName, v1_firstValue); assertNotEquals(paramName, v1_firstValue);
assertEquals(v1_firstValue, v1_secondeValue); assertEquals(v1_firstValue, v1_secondeValue);
VUser vUser2 = new VUser(scenarioContext); VUser vUser2 = new VUser(scenarioContext, 1);
String v2_firstValue = vUser2.getParam(paramName); String v2_firstValue = vUser2.getParam(paramName);
String v2_secondValue = vUser2.getParam(paramName); String v2_secondValue = vUser2.getParam(paramName);
assertNotEquals(paramName, v2_firstValue); assertNotEquals(paramName, v2_firstValue);
@ -105,7 +105,7 @@ public class Test_ParameterManager extends TestBase_Parameterization {
UpdateStrategy.EACH_OCCURRENCE.name()))); UpdateStrategy.EACH_OCCURRENCE.name())));
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext(
testId, scenario, 1); testId, scenario, 1);
VUser vUser = new VUser(scenarioContext); VUser vUser = new VUser(scenarioContext, 1);
String vuserFirstValue = vUser.getParam(paramName); String vuserFirstValue = vUser.getParam(paramName);
String vuserSecondeValue = vUser.getParam(paramName); String vuserSecondeValue = vUser.getParam(paramName);
assertNotEquals(vuserFirstValue, vuserSecondeValue); assertNotEquals(vuserFirstValue, vuserSecondeValue);

View File

@ -24,7 +24,7 @@ public class Test_ClassHelper {
.value(); .value();
} }
}); });
assertEquals(5, plugins.size()); assertEquals(6, plugins.size());
assertEquals(true, plugins.containsKey("Http")); assertEquals(true, plugins.containsKey("Http"));
} }
} }

View File

@ -10,10 +10,10 @@ import org.bench4q.agent.scenario.dfa.ParamPart.ParamPartType;
import org.junit.Test; import org.junit.Test;
public class Test_DFA { public class Test_DFA {
public static final String TEST_CASE = "${csvProvider0:userName}"; private static final String TEST_CASE = "${csvProvider0:userName}";
private static final String TEST_CASE2 = "${csvProvider0:userName}$#"; private static final String TEST_CASE2 = "${csvProvider0:userName}$#";
private static final String TEST_CASE3 = "${csvProvider0:userName}$#okOrNot"; private static final String TEST_CASE3 = "${csvProvider0:userName}$#okOrNot";
static final String TEST_CASE4 = "${csvProvider0:userName}$#okOrNot${file.separator}"; private static final String TEST_CASE4 = "${csvProvider0:userName}$#okOrNot${file.separator}";
private static final String TEST_CASE_NOTSUPPORT1 = "${{abc"; private static final String TEST_CASE_NOTSUPPORT1 = "${{abc";
private static final String TEST_CASE_NOTSUPPORT2 = "${csvProvider0:adc{"; private static final String TEST_CASE_NOTSUPPORT2 = "${csvProvider0:adc{";

View File

@ -11,23 +11,24 @@ import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.behavior.Behavior; import org.bench4q.agent.scenario.behavior.Behavior;
import org.bench4q.agent.scenario.dfa.ParamPart; import org.bench4q.agent.scenario.dfa.ParamPart;
import org.bench4q.agent.scenario.dfa.ParamPart.ParamPartType; import org.bench4q.agent.scenario.dfa.ParamPart.ParamPartType;
import org.bench4q.agent.test.TestBase_Parameterization;
import org.bench4q.agent.test.plugin.Test_HBasePlugin; import org.bench4q.agent.test.plugin.Test_HBasePlugin;
import org.bench4q.share.models.agent.DefinedParameterModel; import org.bench4q.share.models.agent.DefinedParameterModel;
import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.PageModel; import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.junit.Test; import org.junit.Test;
public class Test_Scenario { public class Test_Scenario extends TestBase_Parameterization {
private static final String TEST_CASE = "${csvProvider0:userName}$#okOrNot${file.separator}";
@Test @Test
public void testScenarioBuilderWithZeroDefinedParameter() { public void testScenarioBuilderWithZeroDefinedParameter() {
RunScenarioModel inputModel = Test_HBasePlugin.buildScenario(10); RunScenarioModel inputModel = Test_HBasePlugin.buildScenario(10);
Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel); Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel);
assertNotNull(scenario.getDefinedParameters()); assertNotNull(scenario.getDefinedParameters());
assertEquals(0, scenario.getDefinedParameters().length); assertEquals(1, scenario.getDefinedParameters().length);
} }
@Test @Test
@ -38,7 +39,7 @@ public class Test_Scenario {
"each_iteration")); "each_iteration"));
Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel); Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel);
assertNotNull(scenario.getDefinedParameters()); assertNotNull(scenario.getDefinedParameters());
assertEquals(1, scenario.getDefinedParameters().length); assertEquals(2, scenario.getDefinedParameters().length);
} }
private DefinedParameterModel buildDefinedParameterModel(String name, private DefinedParameterModel buildDefinedParameterModel(String name,
@ -53,18 +54,16 @@ public class Test_Scenario {
@Test @Test
public void test_ScenarioCompile() { public void test_ScenarioCompile() {
RunScenarioModel inputModel = buildRunScenarioModelWith(
List<BehaviorModel> behaviors = new ArrayList<BehaviorModel>(); new ArrayList<UsePluginModel>(),
behaviors.add(BehaviorModel.UserBehaviorBuilder(0, "first", "http", BehaviorModel.UserBehaviorBuilder(0, "first", "http",
new ArrayList<ParameterModel>() { new ArrayList<ParameterModel>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
{ {
add(ParameterModel.createParameter("withParamPart", add(ParameterModel.createParameter(
Test_DFA.TEST_CASE4)); "withParamPart", TEST_CASE));
} }
})); }));
RunScenarioModel inputModel = buildRunScenarioModelWith(behaviors);
Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel); Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel);
assertNotNull(scenario.getDefinedParameters()); assertNotNull(scenario.getDefinedParameters());
for (Behavior behavior : scenario.getAllBehaviors()) { for (Behavior behavior : scenario.getAllBehaviors()) {
@ -100,15 +99,4 @@ public class Test_Scenario {
} }
} }
private RunScenarioModel buildRunScenarioModelWith(
List<BehaviorModel> behaviors) {
RunScenarioModel runScenarioModel = new RunScenarioModel();
PageModel page = new PageModel();
BatchModel batch = new BatchModel();
batch.setBehaviors(new ArrayList<BehaviorModel>());
batch.getBehaviors().addAll(behaviors);
page.getBatches().add(batch);
runScenarioModel.getPages().add(page);
return runScenarioModel;
}
} }

View File

@ -2,22 +2,11 @@ package org.bench4q.agent.test.scenario;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.bench4q.agent.parameterization.PickOrder;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.parameterization.impl.Para_Table;
import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.ScenarioContext; import org.bench4q.agent.scenario.ScenarioContext;
import org.bench4q.agent.scenario.VUser;
import org.bench4q.agent.test.plugin.Test_HBasePlugin;
import org.bench4q.share.helper.TestHelper;
import org.bench4q.share.models.agent.DefinedParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -36,36 +25,36 @@ public class Test_ScenarioContext {
assertEquals(20, context.getExecutor().getMaximumPoolSize()); assertEquals(20, context.getExecutor().getMaximumPoolSize());
} }
@Test // @Test
public void testForParameterization() throws IOException { // public void testForParameterization() throws IOException {
RunScenarioModel model = Test_HBasePlugin.buildScenario(10); // RunScenarioModel model = Test_HBasePlugin.buildScenario(10);
String param_name = "param1"; // String param_name = "param1";
UUID testId = UUID.randomUUID(); // UUID testId = UUID.randomUUID();
//
// createParamFileAndFillParamContent(param_name, testId);
//
// model.getDefinedParameters().add(
// new DefinedParameterModel(param_name, "Para_Table",
// PickOrder.SEQUENTIAL.name(),
// UpdateStrategy.EACH_OCCURRENCE.name()));
//
// ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext(
// testId, Scenario.scenarioBuilderWithCompile(model), 10);
//
// VUser vUser = new VUser(scenarioContext, 1);
// String firstValue = vUser.getParam(param_name);
// String secondValue = vUser.getParam(param_name);
// assertNotEquals(firstValue, secondValue);
// }
createParamFileAndFillParamContent(param_name, testId); // private void createParamFileAndFillParamContent(String param_name,
// UUID testId) throws IOException {
model.getDefinedParameters().add( // File parmFile = new File(
new DefinedParameterModel(param_name, "Para_Table", // new Para_Table(testId, PickOrder.SEQUENTIAL,
PickOrder.SEQUENTIAL.name(), // UpdateStrategy.EACH_ITERATION, param_name, -1)
UpdateStrategy.EACH_OCCURRENCE.name())); // .getParamFileFullPath(param_name));
// TestHelper.createFileIfNotExist(parmFile);
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext( // FileUtils.writeStringToFile(parmFile,
testId, Scenario.scenarioBuilderWithCompile(model), 10); // "row1;10;11~row2;20;21~row3,30,31~");
// }
VUser vUser = new VUser(scenarioContext);
String firstValue = vUser.getParam(param_name);
String secondValue = vUser.getParam(param_name);
assertNotEquals(firstValue, secondValue);
}
private void createParamFileAndFillParamContent(String param_name,
UUID testId) throws IOException {
File parmFile = new File(
new Para_Table(testId, PickOrder.SEQUENTIAL,
UpdateStrategy.EACH_ITERATION, param_name, -1)
.getParamFileFullPath(param_name));
TestHelper.createFileIfNotExist(parmFile);
FileUtils.writeStringToFile(parmFile,
"row1;10;11~row2;20;21~row3,30,31~");
}
} }

View File

@ -1,17 +1,26 @@
package org.bench4q.agent.test.scenario; package org.bench4q.agent.test.scenario;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.bench4q.agent.plugin.basic.PluginReturn; import org.bench4q.agent.plugin.basic.PluginReturn;
import org.bench4q.agent.plugin.basic.csvprovider.CsvProvider;
import org.bench4q.agent.plugin.basic.http.HttpReturn; import org.bench4q.agent.plugin.basic.http.HttpReturn;
import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.ScenarioContext; import org.bench4q.agent.scenario.ScenarioContext;
import org.bench4q.agent.scenario.VUser; import org.bench4q.agent.scenario.VUser;
import org.bench4q.agent.scenario.behavior.Behavior;
import org.bench4q.agent.test.TestBase_Parameterization;
import org.bench4q.agent.test.plugin.Test_HBasePlugin; import org.bench4q.agent.test.plugin.Test_HBasePlugin;
import org.bench4q.share.helper.TestHelper; import org.bench4q.share.helper.TestHelper;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -19,7 +28,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" }) @ContextConfiguration(locations = { "classpath:application-context.xml" })
public class Test_VUser { public class Test_VUser extends TestBase_Parameterization {
static final String TEST_CASE = "${csvProvider0:userName}$#okOrNot${file.separator}";
@Test @Test
public void testExtractRunTimeParamsWithiNullPluginReturn() { public void testExtractRunTimeParamsWithiNullPluginReturn() {
@ -94,17 +104,58 @@ public class Test_VUser {
} }
private static Scenario getTestScenario() { private static Scenario getTestScenario() {
return Scenario.scenarioBuilderWithCompile(Test_HBasePlugin.buildScenario(10)); return Scenario.scenarioBuilderWithCompile(Test_HBasePlugin
.buildScenario(10));
} }
public static VUser createVUser(Scenario scenario, UUID testId) { public static VUser createVUser(Scenario scenario, UUID testId) {
return new VUser(ScenarioContext.buildScenarioContext(testId, scenario, return new VUser(ScenarioContext.buildScenarioContext(testId, scenario,
10)); 10), 1);
} }
@Test @Test
public void test_run() { public void test_reassamblyParameters() throws Exception {
VUser vUser = createAWorker(); UUID testId = UUID.randomUUID();
vUser.run(); final String paramKey = "userName";
final String fileName = "param1";
final String fieldNames = paramKey + ", password";
createFileAndWriteContent(testId, fileName,
"chen, 123" + System.getProperty("line.separator") + "kkk, 321");
final BehaviorModel behaviorModel = BehaviorModel.UserBehaviorBuilder(
1, "beh1", "http", new ArrayList<ParameterModel>() {
private static final long serialVersionUID = 1L;
{
add(ParameterModel.createParameter(paramKey, TEST_CASE));
}
});
RunScenarioModel runScenarioModel = buildRunScenarioModelWith(
new ArrayList<UsePluginModel>() {
private static final long serialVersionUID = 1L;
{
add(buildCsvProviderWith("csvProvider0", fileName,
fieldNames));
}
}, behaviorModel);
Scenario scenario = Scenario
.scenarioBuilderWithCompile(runScenarioModel);
VUser vUser = createVUser(scenario, testId);
Map<String, Object> plugins = new HashMap<String, Object>();
TestHelper
.invokePrivate(vUser, "preparePlugins", new Class<?>[] {
Scenario.class, Map.class }, new Object[] { scenario,
plugins });
assertEquals(2, plugins.size());
assertTrue(plugins.containsKey("csvProvider0"));
CsvProvider csvProvider = (CsvProvider) plugins.get("csvProvider0");
csvProvider.doNext();
@SuppressWarnings("unchecked")
Map<String, String> params = (Map<String, String>) TestHelper
.invokePrivate(vUser, "reassamblyParameters", new Class<?>[] {
Behavior.class, Map.class }, new Object[] {
scenario.getAllBehaviors().get(0), plugins });
assertEquals(1, params.size());
assertTrue(params.containsKey(paramKey));
assertEquals("chen1okOrNot\\", params.get(paramKey));
dropFiles(testId);
} }
} }