now, i can get the parameter value from the csv file

now, i can get the parameter value from the csv file
This commit is contained in:
coderfengyun 2014-04-10 17:06:11 +08:00
parent 0d29dd3788
commit bc269cfbfc
9 changed files with 292 additions and 64 deletions

View File

@ -1,9 +1,19 @@
package org.bench4q.agent.plugin; package org.bench4q.agent.plugin;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.bench4q.agent.Main; import org.bench4q.agent.Main;
/**
* This class is the base of parameterization, such as CsvProvider, And
* parameterization Class has one more constructor parameter called testId, And
* it make getting parameter file convenient
*
* @author coderfengyun
*
*/
public abstract class BaseParameterization { public abstract class BaseParameterization {
private UUID testId; private UUID testId;
@ -19,10 +29,18 @@ public abstract class BaseParameterization {
this.setTestId(UUID.fromString(testId)); this.setTestId(UUID.fromString(testId));
} }
protected String getFileFullPath(String simpleFileName) { public String getFileFullPath(String simpleFileName) {
String FILE_SEPARATOR = System.getProperty("file.separator"); String FILE_SEPARATOR = System.getProperty("file.separator");
return Main.SCENARIO_PARAMETERS_FOLDER + FILE_SEPARATOR return Main.SCENARIO_PARAMETERS_FOLDER + FILE_SEPARATOR
+ this.getTestId().toString() + FILE_SEPARATOR + simpleFileName + this.getTestId().toString() + FILE_SEPARATOR + simpleFileName
+ ".txt"; + ".txt";
} }
public abstract String getValue(String var);
public static Map<String, String> buildConstructorInitParams(String testId) {
Map<String, String> initParamsForParameterization = new HashMap<String, String>();
initParamsForParameterization.put("testId", testId);
return initParamsForParameterization;
}
} }

View File

@ -169,12 +169,15 @@ public class PluginManager {
} }
public Object initializePlugin(Class<?> plugin, public Object initializePlugin(Class<?> plugin,
Map<String, String> parameters) { Map<String, String> parameters, Map<String, String> extraParameters) {
try { try {
Constructor<?>[] ctConstructors = plugin.getConstructors(); Constructor<?>[] ctConstructors = plugin.getConstructors();
if (ctConstructors.length != 1) { if (ctConstructors.length != 1) {
return null; return null;
} }
if (needExtraParameters(plugin)) {
parameters.putAll(extraParameters);
}
Constructor<?> constructor = getConstructor(ctConstructors); Constructor<?> constructor = getConstructor(ctConstructors);
Object[] params = prepareParameters(constructor, parameters); Object[] params = prepareParameters(constructor, parameters);
return constructor.newInstance(params); return constructor.newInstance(params);
@ -184,6 +187,10 @@ public class PluginManager {
} }
} }
private boolean needExtraParameters(Class<?> plugin) {
return plugin.getSuperclass().equals(BaseParameterization.class);
}
private Constructor<?> getConstructor(Constructor<?>[] ctConstructors) { private Constructor<?> getConstructor(Constructor<?>[] ctConstructors) {
for (Constructor<?> constructor2 : ctConstructors) { for (Constructor<?> constructor2 : ctConstructors) {
if (constructor2 if (constructor2

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin.basic.csvProvider; package org.bench4q.agent.plugin.basic.csvprovider;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
@ -8,6 +8,7 @@ import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bench4q.agent.plugin.BaseParameterization; import org.bench4q.agent.plugin.BaseParameterization;
@ -20,6 +21,7 @@ import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin(value = "CsvProvider") @Plugin(value = "CsvProvider")
public class CsvProvider extends BaseParameterization { public class CsvProvider extends BaseParameterization {
private static final String ENABLE_CheckBox = "enable"; private static final String ENABLE_CheckBox = "enable";
static final String LINES_GET = "#";
public static final char DEFAULT_SEPARATOR = ','; public static final char DEFAULT_SEPARATOR = ',';
private static final Pattern pattern = Pattern.compile("\\[(\\d+)\\]"); private static final Pattern pattern = Pattern.compile("\\[(\\d+)\\]");
@ -40,7 +42,7 @@ public class CsvProvider extends BaseParameterization {
@Constructor @Constructor
public CsvProvider( public CsvProvider(
String testId, @Parameter(type = SupportTypes.Field, value = "testId") String testId,
@Parameter(type = SupportTypes.Field, value = "fileName") String fileName, @Parameter(type = SupportTypes.Field, value = "fileName") String fileName,
@Parameter(type = SupportTypes.Field, value = "separator") String separator, @Parameter(type = SupportTypes.Field, value = "separator") String separator,
@Parameter(type = SupportTypes.NField, value = "fieldNames") String fieldNames, @Parameter(type = SupportTypes.NField, value = "fieldNames") String fieldNames,
@ -70,7 +72,8 @@ public class CsvProvider extends BaseParameterization {
ENABLE_CheckBox); ENABLE_CheckBox);
this.bigFile = ParameterParser.buildCheckBox(loadAtRunTime).contains( this.bigFile = ParameterParser.buildCheckBox(loadAtRunTime).contains(
ENABLE_CheckBox); ENABLE_CheckBox);
this.commentPrefixes = ParameterParser.buildNField(commentPrefixes); this.commentPrefixes = commentPrefixes == null ? null : ParameterParser
.buildNField(commentPrefixes);
try { try {
if (this.shared || !this.bigFile) { if (this.shared || !this.bigFile) {
this.input = new InputStreamReader(new FileInputStream( this.input = new InputStreamReader(new FileInputStream(
@ -114,6 +117,18 @@ public class CsvProvider extends BaseParameterization {
reset(); reset();
} }
public void close() {
position = -1;
currentValues = null;
if (bigFile && !shared) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void reset() { public void reset() {
position = -1; position = -1;
currentValues = null; currentValues = null;
@ -309,8 +324,38 @@ public class CsvProvider extends BaseParameterization {
return false; return false;
} }
public String getCurrentValue() { @Override
return null; public String getValue(String var) {
if (var.equals(LINES_GET)) {
if (bigFile) {
throw new RuntimeException(
"CsvProvider for file \""
+ filename
+ "\": the number of lines is not available when configured to load lines at runtime.");
} else {
return String.valueOf(allValues.size());
}
} else if (position == -1) {
throw new RuntimeException("Can't get variable " + var
+ " from plug-in CsvProvider for file \"" + filename
+ "\": missing initial call to control \"next\".");
} else if (currentValues == null) {
throw new RuntimeException("End of file \"" + filename
+ "\" reached in CsvProvider.");
} else {
Matcher m;
int i, n;
if (fieldNames != null)
for (i = 0, n = fieldNames.length; i < n; i++)
if (fieldNames[i].equals(var))
return (i < currentValues.length) ? currentValues[i]
: null;
m = pattern.matcher(var);
return m.matches()
&& (i = Integer.parseInt(m.group(1))) < currentValues.length ? currentValues[i]
: null;
}
} }
} }

View File

@ -167,8 +167,11 @@ public class VUser implements Runnable {
for (Parameter parameter : usePlugin.getParameters()) { for (Parameter parameter : usePlugin.getParameters()) {
initParameters.put(parameter.getKey(), parameter.getValue()); 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( Object plugin = this.getPluginManager().initializePlugin(
pluginClass, initParameters); pluginClass, initParameters, extraParameters);
plugins.put(pluginId, plugin); plugins.put(pluginId, plugin);
} }
} }

View File

@ -0,0 +1,34 @@
package org.bench4q.agent.test;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.bench4q.agent.plugin.BaseParameterization;
import org.bench4q.share.helper.TestHelper;
public abstract class TestBase_Parameterization {
private BaseParameterization basePara;
protected void createFileAndWriteContent(UUID testId, String paramName,
String content) throws IOException {
basePara = new BaseParameterization(testId.toString()) {
@Override
public String getValue(String var) {
return null;
}
};
File paramFile = new File(basePara.getFileFullPath(paramName));
TestHelper.createFileIfNotExist(paramFile);
FileUtils.writeStringToFile(paramFile, content);
}
protected void dropFiles(UUID testId) throws IOException {
String fileFullPath = this.basePara.getFileFullPath("test");
String dirPath = fileFullPath.substring(0,
fileFullPath.lastIndexOf(System.getProperty("file.separator")));
File dirFile = new File(dirPath);
FileUtils.deleteDirectory(dirFile);
}
}

View File

@ -2,12 +2,10 @@ package org.bench4q.agent.test.parameterization;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.bench4q.agent.helper.ApplicationContextHelper; import org.bench4q.agent.helper.ApplicationContextHelper;
import org.bench4q.agent.helper.ClassHelper; import org.bench4q.agent.helper.ClassHelper;
import org.bench4q.agent.parameterization.ParameterizationManager; import org.bench4q.agent.parameterization.ParameterizationManager;
@ -18,6 +16,7 @@ import org.bench4q.agent.scenario.DefinedParameter;
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.test.TestBase_Parameterization;
import org.bench4q.share.helper.TestHelper; import org.bench4q.share.helper.TestHelper;
import org.bench4q.share.models.agent.DefinedParameterModel; import org.bench4q.share.models.agent.DefinedParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioModel;
@ -28,7 +27,7 @@ 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_ParameterManager { public class Test_ParameterManager extends TestBase_Parameterization {
@Test @Test
public void test_createParaInstanceWith_ONCE() throws IOException { public void test_createParaInstanceWith_ONCE() throws IOException {
@ -37,7 +36,7 @@ public class Test_ParameterManager {
ParameterizationManager parameterizationManager = new ParameterizationManager( ParameterizationManager parameterizationManager = new ParameterizationManager(
ApplicationContextHelper.getContext() ApplicationContextHelper.getContext()
.getBean(ClassHelper.class)); .getBean(ClassHelper.class));
createFileAndWriteContent(testId, paramName); createFileAndWriteContent(testId, paramName, "12;13;14;15");
BasePara param = parameterizationManager.createInstanceWith(testId, BasePara param = parameterizationManager.createInstanceWith(testId,
"Para_List", paramName, PickOrder.SEQUENTIAL.toString(), "Para_List", paramName, PickOrder.SEQUENTIAL.toString(),
UpdateStrategy.ONCE.toString(), -1); UpdateStrategy.ONCE.toString(), -1);
@ -50,7 +49,7 @@ public class Test_ParameterManager {
throws Exception { throws Exception {
UUID testId = UUID.randomUUID(); UUID testId = UUID.randomUUID();
String paramName = "param1"; String paramName = "param1";
createFileAndWriteContent(testId, paramName); createFileAndWriteContent(testId, paramName, "12;13;14;15");
Scenario scenario = Scenario Scenario scenario = Scenario
.scenarioBuilder(buildATestScenarioModelWith(new DefinedParameter( .scenarioBuilder(buildATestScenarioModelWith(new DefinedParameter(
paramName, "Para_List", PickOrder.SEQUENTIAL.name(), paramName, "Para_List", PickOrder.SEQUENTIAL.name(),
@ -69,30 +68,12 @@ public class Test_ParameterManager {
assertEquals(v2_firstValue, v2_secondValue); assertEquals(v2_firstValue, v2_secondValue);
} }
private void createFileAndWriteContent(UUID testId, String paramName)
throws IOException {
File paramFile = new File((new BasePara(testId, paramName,
UpdateStrategy.EACH_ITERATION, PickOrder.SEQUENTIAL, -1) {
@Override
public String getCurrentValue() {
return null;
}
@Override
protected int getNextIndex(int size) {
return 0;
}
}).getParamFileFullPath(paramName));
TestHelper.createFileIfNotExist(paramFile);
FileUtils.writeStringToFile(paramFile, "12;13;14;15");
}
@Test @Test
public void test_createParaInstanceWith_UpdateStrategy_EACH_ITERATION() public void test_createParaInstanceWith_UpdateStrategy_EACH_ITERATION()
throws IOException { throws IOException {
UUID testId = UUID.randomUUID(); UUID testId = UUID.randomUUID();
String paramName = "param1"; String paramName = "param1";
createFileAndWriteContent(testId, paramName); createFileAndWriteContent(testId, paramName, "12;13;14;15");
Scenario scenario = Scenario Scenario scenario = Scenario
.scenarioBuilder(buildATestScenarioModelWith(new DefinedParameter( .scenarioBuilder(buildATestScenarioModelWith(new DefinedParameter(
paramName, "Para_List", PickOrder.SEQUENTIAL.name(), paramName, "Para_List", PickOrder.SEQUENTIAL.name(),
@ -117,7 +98,7 @@ public class Test_ParameterManager {
throws IOException { throws IOException {
UUID testId = UUID.randomUUID(); UUID testId = UUID.randomUUID();
String paramName = "param1"; String paramName = "param1";
createFileAndWriteContent(testId, paramName); createFileAndWriteContent(testId, paramName, "12;13;14;15");
Scenario scenario = Scenario Scenario scenario = Scenario
.scenarioBuilder(buildATestScenarioModelWith(new DefinedParameter( .scenarioBuilder(buildATestScenarioModelWith(new DefinedParameter(
paramName, "Para_List", PickOrder.SEQUENTIAL.name(), paramName, "Para_List", PickOrder.SEQUENTIAL.name(),

View File

@ -0,0 +1,133 @@
package org.bench4q.agent.test.plugin;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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.test.TestBase_Parameterization;
import org.bench4q.agent.test.scenario.Test_VUser;
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.UsePluginModel;
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_CsvProvider extends TestBase_Parameterization {
private static final String CONTENT = "chentienan,1234"
+ System.getProperty("line.separator") + "wangxiaoer,2345";
@Autowired
private PluginManager pluginManager;
@Test
public void test_InitialRightly_By_PluginManager() throws IOException {
String fileName = "para_csv";
final UUID testId = UUID.randomUUID();
createFileAndWriteContent(testId, fileName, CONTENT);
Map<String, String> parameters = buildInitParametersNotBigFile(fileName);
Map<String, String> extraParameters = new HashMap<String, String>() {
private static final long serialVersionUID = 1L;
{
put("testId", testId.toString());
}
};
CsvProvider csvProvider = (CsvProvider) this.pluginManager
.initializePlugin(
this.pluginManager.getPlugins().get("CsvProvider"),
parameters, extraParameters);
csvProvider.close();
assertNotNull(csvProvider);
dropFiles(testId);
}
private Map<String, String> buildInitParametersNotBigFile(String fileName) {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("fileName", fileName);
parameters.put("separator", ",");
parameters.put("fieldNames", "userName, password");
parameters.put("shared", "Notenable");
parameters.put("loop", "enable");
parameters.put("loadAtRunTime", "Notenable");
parameters.put("commentPrefix", null);
return parameters;
}
private List<ParameterModel> buildParameterModels(Map<String, String> params) {
List<ParameterModel> result = new ArrayList<ParameterModel>();
for (final Entry<String, String> entry : params.entrySet()) {
result.add(new ParameterModel() {
{
setKey(entry.getKey());
setValue(entry.getValue());
}
});
}
return result;
}
@Test
public void test_InitialRightly_By_VUser() {
final String fileName = "param_csv";
RunScenarioModel runScenarioModel = buildRunScenarioModel(fileName);
Scenario scenario = Scenario.scenarioBuilder(runScenarioModel);
VUser vUser = Test_VUser.createVUser(scenario, UUID.randomUUID());
try {
HashMap<String, Object> plugins = new HashMap<String, Object>();
TestHelper.invokePrivate(vUser, "preparePlugins", new Class<?>[] {
Scenario.class, Map.class }, new Object[] { scenario,
plugins });
assertTrue(plugins.containsKey("csvProvider0"));
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
private RunScenarioModel buildRunScenarioModel(final String fileName) {
RunScenarioModel runScenarioModel = new RunScenarioModel();
runScenarioModel.getUsePlugins().add(new UsePluginModel() {
{
setId("csvProvider0");
setName("CsvProvider");
setParameters(buildParameterModels(buildInitParametersNotBigFile(fileName)));
}
});
return runScenarioModel;
}
@Test
public void test_DoGet() throws Exception {
String fileName = "para_csv";
UUID testId = UUID.randomUUID();
createFileAndWriteContent(testId, fileName, CONTENT);
Scenario scenario = Scenario
.scenarioBuilder(buildRunScenarioModel(fileName));
VUser vUser = Test_VUser.createVUser(scenario, testId);
HashMap<String, Object> plugins = new HashMap<String, Object>();
TestHelper
.invokePrivate(vUser, "preparePlugins", new Class<?>[] {
Scenario.class, Map.class }, new Object[] { scenario,
plugins });
CsvProvider csvProvider = (CsvProvider) plugins.get("csvProvider0");
csvProvider.doNext();
assertNotNull(csvProvider.getValue("userName"));
assertEquals("chentienan", csvProvider.getValue("userName"));
dropFiles(testId);
}
}

View File

@ -1,5 +1,6 @@
package org.bench4q.agent.test.plugin; package org.bench4q.agent.test.plugin;
import java.util.HashMap;
import org.bench4q.agent.helper.ClassHelper; import org.bench4q.agent.helper.ClassHelper;
import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.plugin.PluginManager;
@ -23,7 +24,8 @@ public class Test_PluginManager {
PluginManager pluginManager = new PluginManager(new ClassHelper(), PluginManager pluginManager = new PluginManager(new ClassHelper(),
new TypeConverter()); new TypeConverter());
HttpPlugin httpPlugin = (HttpPlugin) pluginManager.initializePlugin( HttpPlugin httpPlugin = (HttpPlugin) pluginManager.initializePlugin(
pluginManager.getPlugins().get("Http"), null); pluginManager.getPlugins().get("Http"), null,
new HashMap<String, String>());
assertNotNull(httpPlugin); assertNotNull(httpPlugin);
} }
@ -36,7 +38,7 @@ public class Test_PluginManager {
WithoutConstructorAnnotationPlugin plugin = (WithoutConstructorAnnotationPlugin) pluginManager WithoutConstructorAnnotationPlugin plugin = (WithoutConstructorAnnotationPlugin) pluginManager
.initializePlugin( .initializePlugin(
pluginManager.getPlugins().get( pluginManager.getPlugins().get(
"WithoutConstructorAnnotation"), null); "WithoutConstructorAnnotation"), null, null);
assertNull(plugin); assertNull(plugin);
} }

View File

@ -5,7 +5,6 @@ import java.util.UUID;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.bench4q.agent.parameterization.SessionObject;
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.Scenario; import org.bench4q.agent.scenario.Scenario;
@ -64,36 +63,42 @@ public class Test_VUser {
} }
} }
@Test // @Test
public void testExtractRunTimeParamsWithOneRunTimeParams() { // public void testExtractRunTimeParamsWithOneRunTimeParams() {
VUser testWorker = createAWorker(); // VUser testWorker = createAWorker();
try { // try {
HttpReturn httpReturn = new HttpReturn(true, 200, 100, "text/html"); // HttpReturn httpReturn = new HttpReturn(true, 200, 100, "text/html");
httpReturn.setRunTimeParams(new HashMap<String, String>() { // httpReturn.setRunTimeParams(new HashMap<String, String>() {
private static final long serialVersionUID = 1L; // private static final long serialVersionUID = 1L;
{ // {
put("key", "value"); // put("key", "value");
} // }
}); // });
TestHelper.invokePrivate(testWorker, "extractRunTimeParams", // TestHelper.invokePrivate(testWorker, "extractRunTimeParams",
new Class[] { PluginReturn.class }, // new Class[] { PluginReturn.class },
new Object[] { httpReturn }); // new Object[] { httpReturn });
SessionObject sessionObject = (SessionObject) TestHelper // SessionObject sessionObject = (SessionObject) TestHelper
.invokePrivate(testWorker, "getSessionObject", null, null); // .invokePrivate(testWorker, "getSessionObject", null, null);
assertEquals( // assertEquals(
"value", // "value",
sessionObject // sessionObject
.getParam("<parameters name=\"key\" class=\"extracted\" />")); // .getParam("<parameters name=\"key\" class=\"extracted\" />"));
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
fail(); // fail();
} // }
// }
private static VUser createAWorker() {
return createVUser(getTestScenario(), UUID.randomUUID());
} }
private VUser createAWorker() { private static Scenario getTestScenario() {
return new VUser(ScenarioContext.buildScenarioContext( return Scenario.scenarioBuilder(Test_HBasePlugin.buildScenario(10));
UUID.randomUUID(), }
Scenario.scenarioBuilder(Test_HBasePlugin.buildScenario(10)),
public static VUser createVUser(Scenario scenario, UUID testId) {
return new VUser(ScenarioContext.buildScenarioContext(testId, scenario,
10)); 10));
} }