refactor and let the parameterization can do the right thing

refactor and let the parameterization can do the right thing
This commit is contained in:
coderfengyun 2014-03-24 16:29:38 +08:00
parent fed4cf7dd4
commit 103153f737
33 changed files with 171 additions and 2253 deletions

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -1,66 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<runScenario>
<pages>
<page>
<batches>
<batch>
<behaviors>
<behaviorModel>
<id>0</id>
<name>Get</name>
<parameters>
<parameter>
<key>url</key>
<value>
http://www.baidu.com
</value>
</parameter>
<parameter>
<key>parameters</key>
<value></value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
</behaviorModel>
</behaviors>
<childId>2</childId>
<id>0</id>
<parentId>-1</parentId>
</batch>
<batch>
<behaviors>
<behaviorModel>
<id>0</id>
<name>Sleep</name>
<parameters>
<parameter>
<key>time</key>
<value>230</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
<use>timer</use>
</behaviorModel>
</behaviors>
<childId>-1</childId>
<id>1</id>
<parentId>-1</parentId>
</batch>
</batches>
</page>
</pages>
<poolSize>0</poolSize>
<usePlugins>
<usePlugin>
<id>http</id>
<name>Http</name>
<parameters />
</usePlugin>
<usePlugin>
<id>timer</id>
<name>ConstantTimer</name>
<parameters />
</usePlugin>
</usePlugins>
</runScenario>

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1 @@
row1;10;11~row2;20;21~row3,30,31~

View File

@ -0,0 +1,27 @@
package org.bench4q.agent.parameterization.impl;
import java.util.UUID;
import org.bench4q.agent.Main;
public abstract class BasePara {
private UUID testId;
protected UUID getTestId() {
return testId;
}
private void setTestId(UUID testId) {
this.testId = testId;
}
public BasePara(UUID testId) {
this.setTestId(testId);
}
public String getParamFileFullPath(String paramName) {
String FILE_SEPARATOR = System.getProperty("file.separator");
return Main.SCENARIO_PARAMETERS_FOLDER + FILE_SEPARATOR
+ testId.toString() + FILE_SEPARATOR + paramName + ".txt";
}
}

View File

@ -12,12 +12,17 @@ import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
public class Para_Table {
public class Para_Table extends BasePara {
public final int cacheCap = 5000;// 1 sequence 2 random
public final int cacheSize = 1000;// 1 sequence 2 random
public final int readCharSize = 10000;
public ConcurrentHashMap<String, Reader> readerMap = new ConcurrentHashMap<String, Para_Table.Reader>();
public Para_Table(UUID testId) {
super(testId);
}
public abstract class Reader {
public final Table t;
@ -115,15 +120,14 @@ public class Para_Table {
char splitChar;
char lineChar;
public FileTable(String sourceValue, int firstRow, char splitChar,
public FileTable(String paramFilePath, int firstRow, char splitChar,
char lineChar) {
super(sourceValue, firstRow);
super(paramFilePath, firstRow);
this.splitChar = splitChar;
this.lineChar = lineChar;
try {
createBFR();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@ -180,36 +184,6 @@ public class Para_Table {
}
public class StringTable extends Table {
char splitChar;
char lineChar;
public StringTable(String sourceValue, int firstRow, char splitChar,
char lineChar) {
super(sourceValue, firstRow);
this.splitChar = splitChar;
this.lineChar = lineChar;
}
@Override
public List<TableRow> readRows() {
String readStr = this.sourceValue;
String[] tokens = readStr.split(String.valueOf(lineChar));
List<TableRow> result = new ArrayList<TableRow>();
for (String line : tokens) {
TableRow tempRow = new TableRow();
String[] columns = line.split(String.valueOf(splitChar));
for (String column : columns) {
tempRow.AddCell(column);
}
result.add(tempRow);
}
return result;
}
}
public class TableRow {
public List<String> cells = new ArrayList<String>();
@ -223,45 +197,39 @@ public class Para_Table {
// java.lang.String, java.lang.String, java.lang.String, java.lang.String)
public String getTableColumnValue(UUID id,
HashMap<String, Object> objCache, String source,
String sourceValue, String firstRow, String nextRow,
String splitChar, String lineChar, String column) {
HashMap<String, Object> objCache, String paramName,
String firstRow, String nextRow, String columnSplitor,
String rowSplitor, String column) {
int fRow = Integer.parseInt(firstRow);
char sChar = splitChar.charAt(0);
char lChar = lineChar.charAt(0);
char sChar = columnSplitor.charAt(0);
char lChar = rowSplitor.charAt(0);
int col = Integer.parseInt(column);
TableRow resultRow = null;
Table table = null;
Reader reader = null;
if (objCache.containsKey(sourceValue) == true) {
resultRow = (TableRow) objCache.get(sourceValue);
if (objCache.containsKey(paramName) == true) {
resultRow = (TableRow) objCache.get(paramName);
return resultRow.cells.get(col);
}
if (this.readerMap.containsKey(sourceValue)) {
reader = readerMap.get(sourceValue);
if (this.readerMap.containsKey(paramName)) {
reader = readerMap.get(paramName);
} else {
if (source.equals("file")) {
table = new FileTable(sourceValue, fRow, sChar, lChar);
} else if (source.equals("input")) {
table = new StringTable(sourceValue, fRow, sChar, lChar);
}
table = new FileTable(getParamFileFullPath(paramName), fRow, sChar,
lChar);
if (nextRow.equals("random")) {
ArrayBlockingQueue<TableRow> queue = new ArrayBlockingQueue<Para_Table.TableRow>(
cacheCap);
reader = new RandomReader(queue, table);
} else if (nextRow.equals("sequence")) {
ArrayBlockingQueue<TableRow> queue = new ArrayBlockingQueue<Para_Table.TableRow>(
cacheCap);
reader = new SequenceReader(queue, table);
}
readerMap.put(sourceValue, reader);
readerMap.put(paramName, reader);
}
resultRow = reader.nextRow();
objCache.put(sourceValue, resultRow);
objCache.put(paramName, resultRow);
return resultRow.cells.get(col);
}

View File

@ -2,7 +2,6 @@
<root>
<property name="source" type="option">
<value>file</value>
<value>input</value>
</property>
<property name="sourceValue" type="string">
</property>

View File

@ -13,27 +13,29 @@ import org.bench4q.agent.Main;
import org.bench4q.agent.parameterization.SessionObject;
public class ParamCoordinator implements SessionObject {
private UUID testRunId;
private UUID id;
private UUID id = UUID.randomUUID();
private ParametersFactory paramterFactory;
private Set<String> usedClassName = new HashSet<String>();
private Map<String, Object> objMap = new HashMap<String, Object>();
private Map<String, String> runtimeParamMap = new HashMap<String, String>();
private Map<String, Object> cacheObjMap = new HashMap<String, Object>();
private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock();
private Logger logger = Logger.getLogger(ParamCoordinator.class);
private ParametersFactory getParamterFactory() {
return paramterFactory;
}
private void setParamterFactory(ParametersFactory paramterFactory) {
this.paramterFactory = paramterFactory;
}
private ParamCoordinator() {
}
protected UUID getRunId() {
return testRunId;
}
public ParamCoordinator(UUID testRunId, UUID controllerId) {
public ParamCoordinator(ParametersFactory parametersFactory) {
this();
this.id = controllerId;
this.testRunId = testRunId;
this.setParamterFactory(parametersFactory);
}
private String getParameter(ParamFormatParser paraFormat) {
@ -54,18 +56,14 @@ public class ParamCoordinator implements SessionObject {
return result;
}
private static String getCurrentPackageFullName() {
return ParamFormatParser.class.getName().substring(0,
ParamFormatParser.class.getName().lastIndexOf('.'));
}
private static String buildFullPath(String simpleClassName) {
return getCurrentPackageFullName() + "." + simpleClassName;
return ParamFormatParser.class.getName().substring(0,
ParamFormatParser.class.getName().lastIndexOf('.'))
+ "." + simpleClassName;
}
private String getParameterInThread(String name, String className,
String functionName, Object[] args) {
boolean hasThisClass = false;
mapRWLock.readLock().lock();
hasThisClass = objMap.containsKey(className);
@ -124,8 +122,8 @@ public class ParamCoordinator implements SessionObject {
private String getParameterCrossThread(String name, String className,
String functionName, Object[] args) {
try {
Object instance = ParametersFactory.getInstance()
.getObjWithGuardExist(className);
Object instance = this.getParamterFactory().getObjWithGuardExist(
className);
Class<?>[] argTypeArr = new Class<?>[args.length + 2];
Object[] totalArgs = prepareTypeArrayAndArgArray(args, argTypeArr);
Method m = instance.getClass().getMethod(functionName, argTypeArr);
@ -191,7 +189,7 @@ public class ParamCoordinator implements SessionObject {
for (String xx : usedClassName) {
release(xx);
}
ParametersFactory.getInstance().cleanUp();
this.getParamterFactory().cleanUp();
cleanUp();
}
@ -204,8 +202,8 @@ public class ParamCoordinator implements SessionObject {
private void release(String className) {
try {
Object instance = ParametersFactory.getInstance()
.getObjWithGuardExist(className);
Object instance = this.getParamterFactory().getObjWithGuardExist(
className);
Method m = instance.getClass().getMethod("unreg", id.getClass());
m.invoke(instance, id);
} catch (Exception ex) {

View File

@ -16,6 +16,7 @@ public class ParamFormatParser {
private String argsString;
private String paramType;
private List<String> args;
private static String[] argsArray;
@XmlAttribute(name = "name")
public String getName() {
@ -71,15 +72,17 @@ public class ParamFormatParser {
}
public String[] getArgsArray() {
return this.getArgs().toArray(new String[] {});
if (argsArray == null)
argsArray = this.getArgs().toArray(new String[] {});
return argsArray;
}
private ParamFormatParser() {
}
public static ParamFormatParser parseInputParameter(String inputContent) {
ParamFormatParser result = (ParamFormatParser) MarshalHelper.tryUnmarshal(
ParamFormatParser.class, inputContent);
ParamFormatParser result = (ParamFormatParser) MarshalHelper
.tryUnmarshal(ParamFormatParser.class, inputContent);
result.setArgs(ParameterParser.buildNField(result.getArgsString()));
return result;
}

View File

@ -13,7 +13,6 @@ public class ParameterThreadOption {
private List<String> justCross = Arrays.asList("crossThread");
private ParameterThreadOption() {
optionMap.put("Para_File", all);
optionMap.put("Para_IteratorNumber", justCross);
optionMap.put("Para_RandomNumber", all);

View File

@ -1,21 +1,29 @@
package org.bench4q.agent.parameterization.impl;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.log4j.Logger;
import org.bench4q.agent.Main;
public class ParametersFactory {
private static final ParametersFactory instance = new ParametersFactory();
private Map<String, Object> objMap = new HashMap<String, Object>();
private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock();
private UUID testId;
private ParametersFactory() {
private UUID getTestId() {
return testId;
}
public static ParametersFactory getInstance() {
return instance;
private void setTestId(UUID testId) {
this.testId = testId;
}
public ParametersFactory(UUID testId) {
this.setTestId(testId);
}
public void cleanUp() {
@ -49,16 +57,19 @@ public class ParametersFactory {
MyFileClassLoader cl = new MyFileClassLoader(
Main.USER_DEFINED_PARAMS_FOLDER);
Class<?> cls = cl.loadClass(className);
Object instance = cls.newInstance();
Constructor<?> contructor = cls
.getConstructor(new Class[] { UUID.class });
Object instance = contructor.newInstance(new Object[] { this
.getTestId() });
mapRWLock.writeLock().lock();
objMap.put(className, instance);
return true;
} catch (Exception ex) {
System.out.println(ex.getMessage());
Logger.getLogger(ParametersFactory.class).error(ex, ex);
return false;
} finally {
mapRWLock.writeLock().unlock();
if (mapRWLock.writeLock().isHeldByCurrentThread())
mapRWLock.writeLock().unlock();
}
}
}

View File

@ -9,6 +9,7 @@ import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.datacollector.impl.ScenarioResultCollector;
import org.bench4q.agent.parameterization.impl.ParametersFactory;
public class ScenarioContext {
private static final long keepAliveTime = 10;
@ -18,6 +19,7 @@ public class ScenarioContext {
private Scenario scenario;
private boolean finished;
private DataCollector dataStatistics;
private ParametersFactory parameterFactory;
public Date getStartDate() {
return startDate;
@ -67,6 +69,14 @@ public class ScenarioContext {
this.dataStatistics = dataStatistics;
}
public ParametersFactory getParameterFactory() {
return parameterFactory;
}
private void setParameterFactory(ParametersFactory parameterFactory) {
this.parameterFactory = parameterFactory;
}
public static ScenarioContext buildScenarioContext(UUID testId,
final Scenario scenario, int poolSize) {
ScenarioContext scenarioContext = new ScenarioContext();
@ -78,6 +88,7 @@ public class ScenarioContext {
scenarioContext.setStartDate(new Date(System.currentTimeMillis()));
scenarioContext.setExecutorService(executor);
scenarioContext.setDataStatistics(new ScenarioResultCollector(testId));
scenarioContext.setParameterFactory(new ParametersFactory(testId));
return scenarioContext;
}

View File

@ -82,7 +82,7 @@ public class ScenarioEngine {
while (!scenarioContext.isFinished()) {
scenarioContext.getExecutor().execute(
new Worker(scenarioContext, new ParamCoordinator(
runId, UUID.randomUUID())));
scenarioContext.getParameterFactory())));
}
}
});

View File

@ -3,13 +3,15 @@ package org.bench4q.agent.test.parameterization;
import java.util.UUID;
import org.bench4q.agent.parameterization.impl.ParamCoordinator;
import org.bench4q.agent.parameterization.impl.ParametersFactory;
import org.bench4q.share.helper.TestHelper;
public class TEST_HelloThread extends Thread {
ParamCoordinator ic;
public TEST_HelloThread() {
ic = new ParamCoordinator(UUID.randomUUID(), UUID.randomUUID());
UUID testId = UUID.randomUUID();
ic = new ParamCoordinator(new ParametersFactory(testId));
}
public void run() {
@ -56,11 +58,9 @@ public class TEST_HelloThread extends Thread {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// private String name;
}

View File

@ -2,12 +2,16 @@ package org.bench4q.agent.test.parameterization;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.bench4q.agent.parameterization.impl.ParamCoordinator;
import org.bench4q.agent.parameterization.impl.Para_Table;
import org.bench4q.agent.parameterization.impl.ParamFormatParser;
import org.bench4q.agent.parameterization.impl.ParametersFactory;
import org.bench4q.share.helper.TestHelper;
import org.junit.After;
import org.junit.Test;
@ -19,7 +23,10 @@ public class Test_InstanceCotroller {
*/
// private static final String OLD_TEST_CASE =
// "<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file,ScenarioParameters\\param1.txt,0,sequence,;,~,2\" />";
private static final String NEW_TEST_CASE = "<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file;ScenarioParameters\\param1.txt;0;sequence;\\;;~;2\" />";
// private static final String NEW_TEST_CASE =
// "<parameters name=\"useNamePassword\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"file;ScenarioParameters\\param1.txt;0;sequence;\\;;~;2\" />";
private static final String file_CONTENT = "row1;10;11~row2;20;21~row3,30,31~";
private static final String Test_CASE1 = "<parameters name=\"param1\" class=\"Para_Table\" type=\"crossThread\" method=\"getTableColumnValue\" args=\"param1;0;sequence;\\;;~;2\" />";
@After
public void cleanUp() {
@ -28,26 +35,37 @@ public class Test_InstanceCotroller {
@Test
public void testGetParam() throws Exception {
Para_Table table = new Para_Table();
UUID testId = UUID.randomUUID();
Para_Table table = new Para_Table(testId);
createParamFileFor(table.getParamFileFullPath("param1"));
String ret = table.getTableColumnValue(java.util.UUID.randomUUID(),
new HashMap<String, Object>(), "file",
"ScenarioParameters\\param1.txt", "0", "sequence", ";", "~",
"2");
new HashMap<String, Object>(), "param1", "0", "sequence", ";",
"~", "2");
System.out.println(ret);
assertEquals(11, Integer.parseInt(ret));
}
private void createParamFileFor(String paramFileFullPath)
throws IOException {
File file = new File(paramFileFullPath);
TestHelper.createFileIfNotExist(file);
FileUtils.writeStringToFile(file, file_CONTENT);
}
@Test
public void testGetParam2() throws Exception {
ParamCoordinator ic = new ParamCoordinator(UUID.randomUUID(),
UUID.randomUUID());
String passwordName = ic.getParam(NEW_TEST_CASE);
UUID testId = UUID.randomUUID();
ParametersFactory parametersFactory = new ParametersFactory(testId);
ParamCoordinator ic = new ParamCoordinator(parametersFactory);
createParamFileFor(new Para_Table(testId)
.getParamFileFullPath("param1"));
String passwordName = ic.getParam(Test_CASE1);
System.out.println(passwordName);
assertNotNull(passwordName);
assertEquals(11, Integer.parseInt(passwordName));
ParamCoordinator instanceControler = new ParamCoordinator(
UUID.randomUUID(), UUID.randomUUID());
String password2 = instanceControler.getParam(NEW_TEST_CASE);
ParamCoordinator ic2 = new ParamCoordinator(parametersFactory);
String password2 = ic2.getParam(Test_CASE1);
System.out.println(password2);
assertNotNull(password2);
assertEquals(Integer.parseInt(passwordName) + 10,
@ -57,10 +75,13 @@ public class Test_InstanceCotroller {
@Test
public void testGetParameter() throws Exception {
ParamCoordinator paracCoor = new ParamCoordinator(UUID.randomUUID(),
UUID.randomUUID());
UUID testId = UUID.randomUUID();
createParamFileFor(new Para_Table(testId)
.getParamFileFullPath("param1"));
ParamCoordinator paracCoor = new ParamCoordinator(
new ParametersFactory(testId));
ParamFormatParser paramFormatParser = ParamFormatParser
.parseInputParameter(NEW_TEST_CASE);
.parseInputParameter(Test_CASE1);
String result = (String) TestHelper.invokePrivate(paracCoor,
"getParameter", new Class[] { ParamFormatParser.class },
new Object[] { paramFormatParser });
@ -70,10 +91,13 @@ public class Test_InstanceCotroller {
@Test
public void testGetParameterCrossThread() throws Exception {
ParamCoordinator paracCoor = new ParamCoordinator(UUID.randomUUID(),
UUID.randomUUID());
UUID testId = UUID.randomUUID();
createParamFileFor(new Para_Table(testId)
.getParamFileFullPath("param1"));
ParamCoordinator paracCoor = new ParamCoordinator(
new ParametersFactory(testId));
ParamFormatParser paramFormatParser = ParamFormatParser
.parseInputParameter(NEW_TEST_CASE);
.parseInputParameter(Test_CASE1);
String result = (String) TestHelper.invokePrivate(
paracCoor,
"getParameterCrossThread",

View File

@ -26,8 +26,9 @@ public class TestHelper {
if (file.exists()) {
return;
}
if (!file.getParentFile().exists()) {
file.mkdirs();
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
try {
file.createNewFile();