litte step towads parameterization

litte step towads parameterization
This commit is contained in:
coderfengyun 2014-04-04 17:30:12 +08:00
parent cc0cdd834d
commit f35e5964b3
32 changed files with 361 additions and 57 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,32 @@
package org.bench4q.agent.parameterization;
public enum UpdateStrategy {
ONCE, EACH_ITERATION, EACH_OCCURRENCE
ONCE, EACH_ITERATION, EACH_OCCURRENCE;
public static interface UpdateRestriction {
public boolean isSatisfiedBy(UpdateStrategy updateStrategy);
}
public static class OnceRestriction implements UpdateRestriction {
public boolean isSatisfiedBy(UpdateStrategy updateStrategy) {
return doesMeet(ONCE, updateStrategy);
}
}
public static class IterationRestriction implements UpdateRestriction {
public boolean isSatisfiedBy(UpdateStrategy updateStrategy) {
return doesMeet(EACH_ITERATION, updateStrategy);
}
}
public static class OccurrenceRestriction implements UpdateRestriction {
public boolean isSatisfiedBy(UpdateStrategy updateStrategy) {
return doesMeet(EACH_OCCURRENCE, updateStrategy);
}
}
private static boolean doesMeet(UpdateStrategy expected,
UpdateStrategy actual) {
return expected == actual;
}
}

View File

@ -24,4 +24,6 @@ public abstract class BasePara {
return Main.SCENARIO_PARAMETERS_FOLDER + FILE_SEPARATOR
+ testId.toString() + FILE_SEPARATOR + paramName + ".txt";
}
public abstract String getCurrentValue();
}

View File

@ -0,0 +1,50 @@
package org.bench4q.agent.parameterization.impl;
import java.io.File;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.bench4q.agent.parameterization.ParameterizedClass;
import org.bench4q.agent.parameterization.PickOrder;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.utils.ParameterParser;
@ParameterizedClass(name = "Para_File")
public class Para_File extends BasePara {
private String currentValue;
public String getCurrentValue() {
return currentValue;
}
private void setCurrentValue(String currentValue) {
this.currentValue = currentValue;
}
public Para_File(UUID testId, PickOrder pickOrder,
UpdateStrategy updateStrategy, String paramName) {
super(testId);
initialValue(paramName);
}
private void initialValue(String paramName) {
// In a sequential way
File paramFile = new File(getParamFileFullPath(paramName));
List<String> nField = null;
try {
String content = FileUtils.readFileToString(paramFile);
nField = ParameterParser.buildNField(content);
} catch (Exception e) {
e.printStackTrace();
}
Random random = new Random();
if (nField.size() == 0) {
throw new NullPointerException(
"The parameter file has no valid content");
}
this.setCurrentValue(nField.get(random.nextInt(nField.size())
% nField.size()));
}
}

View File

@ -30,10 +30,10 @@ public class Para_Table extends BasePara {
}
public abstract class Reader {
public final Table t;
public final Table table;
public Reader(Table table) {
this.t = table;
this.table = table;
}
public abstract TableRow nextRow();
@ -55,7 +55,7 @@ public class Para_Table extends BasePara {
if (queue.size() == 0) {
synchronized (queue) {
if (queue.size() == 0) {
List<TableRow> rows = t.readRows();
List<TableRow> rows = table.readRows();
int n = rows.size();
Random r = new Random();
for (int i = 0; i < n; i++) {
@ -90,7 +90,7 @@ public class Para_Table extends BasePara {
if (queue.size() == 0) {
synchronized (queue) {
if (queue.size() == 0) {
List<TableRow> rows = t.readRows();
List<TableRow> rows = table.readRows();
queue.addAll(rows);
}
}
@ -242,4 +242,9 @@ public class Para_Table extends BasePara {
public void unreg(UUID id) {
}
@Override
public String getCurrentValue() {
return null;
}
}

View File

@ -2,6 +2,7 @@ package org.bench4q.agent.parameterization.impl;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantReadWriteLock;
@ -13,7 +14,9 @@ import org.bench4q.agent.helper.ClassHelper;
import org.bench4q.agent.helper.ClassLoadRestriction;
import org.bench4q.agent.parameterization.ParameterizedClass;
import org.bench4q.agent.parameterization.PickOrder;
import org.bench4q.agent.parameterization.SessionObject;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.parameterization.UpdateStrategy.UpdateRestriction;
import org.bench4q.agent.scenario.DefinedParameter;
public class ParameterizationManager {
@ -83,7 +86,7 @@ public class ParameterizationManager {
}
public VUserContext buildVUserContext() {
public SessionObject buildVUserContext() {
return new VUserContext(this);
}
@ -142,12 +145,42 @@ public class ParameterizationManager {
*
* @param definedParameter
*/
public Map<String, BasePara> createParaInstancesWith(
DefinedParameter[] definedParameters,
UpdateRestriction updateRestriction) {
Map<String, BasePara> result = new LinkedHashMap<String, BasePara>();
if (definedParameters == null || definedParameters.length == 0) {
return result;
}
for (DefinedParameter definedParameter : definedParameters) {
try {
UpdateStrategy updateStrategy = UpdateStrategy
.valueOf(definedParameter.getUpdateStrategy());
PickOrder pickOrder = PickOrder.valueOf(definedParameter
.getPickOrder());
if (!updateRestriction.isSatisfiedBy(updateStrategy)) {
continue;
}
result.put(
definedParameter.getName(),
createInstanceWith(definedParameter.getType(),
definedParameter.getName(), pickOrder,
updateStrategy));
} catch (Exception e) {
Logger.getLogger(ParameterizationManager.class).error(e, e);
}
}
return result;
}
public boolean createParaInstanceWith(DefinedParameter definedParameter) {
try {
this.getParameterMap().put(
definedParameter.getName(),
loadClassWith(definedParameter.getType(), PickOrder
.valueOf(definedParameter.getPickOrder()),
createInstanceWith(definedParameter.getType(),
definedParameter.getName(), PickOrder
.valueOf(definedParameter.getPickOrder()),
UpdateStrategy.valueOf(definedParameter
.getUpdateStrategy())));
@ -158,20 +191,37 @@ public class ParameterizationManager {
}
}
private Object loadClassWith(String name, PickOrder pickOrder,
UpdateStrategy updateStrategy) {
if (!this.getParaClasses().containsKey(name)) {
throw new NullPointerException("There's no class with " + name);
private BasePara createInstanceWith(String className, String paramName,
PickOrder pickOrder, UpdateStrategy updateStrategy) {
if (!this.getParaClasses().containsKey(className)) {
throw new NullPointerException("There's no class with " + className);
}
Class<?> clazz = this.getParaClasses().get(name);
Class<?> clazz = this.getParaClasses().get(className);
try {
Constructor<?> constructor = clazz.getConstructor(UUID.class,
PickOrder.class, UpdateStrategy.class);
return constructor.newInstance(this.getTestId(), pickOrder,
updateStrategy);
PickOrder.class, UpdateStrategy.class, String.class);
return (BasePara) constructor.newInstance(this.getTestId(),
pickOrder, updateStrategy, paramName);
} catch (Exception e) {
throw new NullPointerException(
"There's no constructor with three params");
"There's no constructor with four params whose className is "
+ className);
}
}
/**
*
* @param paramName
* @return the currentValue of the parameter
* @throws NullPointerException
* if not find the parameter with name paramName
*/
public String getParameterValue(String paramName) {
if (!this.getParameterMap().containsKey(paramName)) {
throw new NullPointerException("There is not parameter with name "
+ paramName);
}
BasePara para = (BasePara) this.getParameterMap().get(paramName);
return para.getCurrentValue();
}
}

View File

@ -33,9 +33,9 @@ public class VUserContext implements SessionObject {
private VUserContext() {
}
public VUserContext(ParameterizationManager parametersFactory) {
public VUserContext(ParameterizationManager parameterizationManager) {
this();
this.setParamterFactory(parametersFactory);
this.setParamterFactory(parameterizationManager);
}
private String getParameter(ParamFormatParser paraFormat) {

View File

@ -0,0 +1,44 @@
package org.bench4q.agent.parameterization.impl;
import java.util.Map;
import org.apache.log4j.Logger;
import org.bench4q.agent.parameterization.SessionObject;
public class VuserContextNew implements SessionObject {
private Map<String, BasePara> staticParameters;
private Map<String, BasePara> getStaticParameters() {
return staticParameters;
}
private void setStaticParameters(Map<String, BasePara> staticParameters) {
this.staticParameters = staticParameters;
}
public VuserContextNew(Map<String, BasePara> initStaticParameters) {
this.setStaticParameters(initStaticParameters);
}
public String getParam(String name) {
try {
return this.getStaticParameters().get(name).getCurrentValue();
} catch (Exception e) {
Logger.getLogger(VuserContextNew.class).error(e, e);
return name;
}
}
public void saveRuntimeParam(String name, String value) {
}
public void doCleanUp() {
}
public void saveRuntimeParams(Map<String, String> runTimeParams) {
}
}

View File

@ -1,6 +1,9 @@
package org.bench4q.agent.scenario;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
@ -9,11 +12,16 @@ import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.datacollector.impl.ScenarioResultCollector;
import org.bench4q.agent.parameterization.SessionObject;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.parameterization.UpdateStrategy.UpdateRestriction;
import org.bench4q.agent.parameterization.impl.BasePara;
import org.bench4q.agent.parameterization.impl.ParameterizationManager;
import org.bench4q.agent.parameterization.impl.VUserContext;
import org.bench4q.agent.parameterization.impl.VuserContextNew;
public class ScenarioContext {
private static final long keepAliveTime = 10;
Map<String, BasePara> updateOnceParameters;
private Date startDate;
private Date endDate;
private ThreadPoolExecutor executor;
@ -78,7 +86,17 @@ public class ScenarioContext {
this.parameterFactory = parameterFactory;
}
private Map<String, BasePara> getUpdateOnceParameters() {
return updateOnceParameters;
}
private void setUpdateOnceParameters(
Map<String, BasePara> updateOnceParameters) {
this.updateOnceParameters = updateOnceParameters;
}
private ScenarioContext() {
this.setUpdateOnceParameters(new HashMap<String, BasePara>());
}
public static ScenarioContext buildScenarioContext(UUID testId,
@ -94,20 +112,33 @@ public class ScenarioContext {
scenarioContext.setDataStatistics(new ScenarioResultCollector(testId));
scenarioContext.setParameterFactory(new ParameterizationManager(testId,
scenario.getDefinedParameters().length));
scenarioContext.prepareForParameterization();
scenarioContext.initForUpdateOnce();
return scenarioContext;
}
private void prepareForParameterization() {
if (scenario.getDefinedParameters() == null)
return;
for (DefinedParameter dP : this.getScenario().getDefinedParameters()) {
// TODO:
this.getParameterFactory().createParaInstanceWith(dP);
}
private void initForUpdateOnce() {
this.getUpdateOnceParameters().putAll(
initForParameterization(new UpdateStrategy.OnceRestriction()));
}
public VUserContext buildVUserContext() {
return this.getParameterFactory().buildVUserContext();
private Map<String, BasePara> initForParameterization(
UpdateRestriction updateRestriction) {
if (scenario.getDefinedParameters() == null)
return new LinkedHashMap<String, BasePara>();
return this.getParameterFactory().createParaInstancesWith(
scenario.getDefinedParameters(), updateRestriction);
}
public SessionObject buildVUserContext() {
Map<String, BasePara> initParameters = new HashMap<String, BasePara>();
initParameters.putAll(getUpdateOnceParameters());
initParameters.putAll(initForUpdateEachIteration());
VuserContextNew vuserContextNew = new VuserContextNew(initParameters);
return vuserContextNew;
}
private Map<String, BasePara> initForUpdateEachIteration() {
return initForParameterization(new UpdateStrategy.IterationRestriction());
}
}

View File

@ -2,14 +2,23 @@ package org.bench4q.agent.test.parameterization;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.bench4q.agent.parameterization.PickOrder;
import org.bench4q.agent.parameterization.UpdateStrategy;
import org.bench4q.agent.parameterization.impl.BasePara;
import org.bench4q.agent.parameterization.impl.ParameterizationManager;
import org.bench4q.agent.scenario.DefinedParameter;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.ScenarioContext;
import org.bench4q.agent.scenario.VUser;
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.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
@ -21,17 +30,21 @@ public class Test_ParameterManager {
@Test
public void test_createParaInstanceWith() throws Exception {
UUID testId = UUID.randomUUID();
String paramName = "param1";
ParameterizationManager parameterizationManager = new ParameterizationManager(
UUID.randomUUID(), 1);
testId, 1);
createFileAndWriteContent(testId, paramName);
@SuppressWarnings("unchecked")
Map<String, Object> parameterMapBeforeCreate = (Map<String, Object>) TestHelper
.invokePrivate(parameterizationManager, "getParameterMap",
null, null);
assertEquals(0, parameterMapBeforeCreate.size());
assertTrue(parameterizationManager
.createParaInstanceWith(new DefinedParameter("param1",
"Para_Table", PickOrder.SEQUENTIAL.name(),
UpdateStrategy.EACH_ITERATION.name())));
.createParaInstanceWith(new DefinedParameter(paramName,
"Para_File", PickOrder.SEQUENTIAL.name(),
UpdateStrategy.ONCE.name())));
@SuppressWarnings("unchecked")
Map<String, Object> parameterMapAfterCreate = (Map<String, Object>) TestHelper
.invokePrivate(parameterizationManager, "getParameterMap",
@ -39,6 +52,109 @@ public class Test_ParameterManager {
assertEquals(1, parameterMapAfterCreate.size());
}
@Test
public void test_createParaInstancesWith_Null() throws IOException {
UUID testId = UUID.randomUUID();
final String paramName = "param1";
ParameterizationManager parameterizationManager = new ParameterizationManager(
testId, 1);
createFileAndWriteContent(testId, paramName);
Map<String, BasePara> parameterMap = parameterizationManager
.createParaInstancesWith(null,
new UpdateStrategy.OnceRestriction());
assertEquals(0, parameterMap.size());
}
@Test
public void test_createParaInstancesWith_ONCE() throws IOException {
UUID testId = UUID.randomUUID();
final String paramName = "param1";
ParameterizationManager parameterizationManager = new ParameterizationManager(
testId, 1);
createFileAndWriteContent(testId, paramName);
Map<String, BasePara> parameterMap = parameterizationManager
.createParaInstancesWith(
new DefinedParameter[] { new DefinedParameter(
paramName, "Para_File", PickOrder.SEQUENTIAL
.toString(), UpdateStrategy.ONCE
.toString()) },
new UpdateStrategy.OnceRestriction());
assertEquals(1, parameterMap.size());
assertEquals("12", parameterMap.get(paramName).getCurrentValue());
}
@Test
public void test_createParaInstanceWith_UpdateStrategy_Once()
throws Exception {
UUID testId = UUID.randomUUID();
String paramName = "param1";
createFileAndWriteContent(testId, paramName);
Scenario scenario = Scenario
.scenarioBuilder(buildATestScenarioModelWith(new DefinedParameter(
paramName, "Para_File", PickOrder.SEQUENTIAL.name(),
UpdateStrategy.ONCE.name())));
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext(
testId, scenario, 1);
VUser vUser = new VUser(scenarioContext);
String v1_firstValue = vUser.getParam(paramName);
String v1_secondValue = vUser.getParam(paramName);
assertEquals("12", v1_firstValue);
assertEquals(v1_firstValue, v1_secondValue);
VUser vUser2 = new VUser(scenarioContext);
String v2_firstValue = vUser2.getParam(paramName);
String v2_secondValue = vUser2.getParam(paramName);
assertEquals(v1_firstValue, v2_firstValue);
assertEquals(v2_firstValue, v2_secondValue);
}
private void createFileAndWriteContent(UUID testId, String paramName)
throws IOException {
File paramFile = new File((new BasePara(testId) {
@Override
public String getCurrentValue() {
return null;
}
}).getParamFileFullPath(paramName));
TestHelper.createFileIfNotExist(paramFile);
FileUtils.writeStringToFile(paramFile, "12;13;14;15");
}
@Test
public void test_createParaInstanceWith_UpdateStrategy_EACH_ITERATION()
throws IOException {
UUID testId = UUID.randomUUID();
String paramName = "param1";
createFileAndWriteContent(testId, paramName);
Scenario scenario = Scenario
.scenarioBuilder(buildATestScenarioModelWith(new DefinedParameter(
paramName, "Para_File", PickOrder.SEQUENTIAL.name(),
UpdateStrategy.EACH_ITERATION.name())));
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext(
testId, scenario, 1);
VUser vUser = new VUser(scenarioContext);
String v1_firstValue = vUser.getParam(paramName);
String v1_secondeValue = vUser.getParam(paramName);
assertNotEquals(paramName, v1_firstValue);
assertEquals(v1_firstValue, v1_secondeValue);
VUser vUser2 = new VUser(scenarioContext);
String v2_firstValue = vUser2.getParam(paramName);
String v2_secondValue = vUser2.getParam(paramName);
assertNotEquals(paramName, v2_firstValue);
assertEquals(v2_firstValue, v2_secondValue);
assertNotEquals(v1_firstValue, v2_firstValue);
}
private RunScenarioModel buildATestScenarioModelWith(
DefinedParameter definedParameter) {
RunScenarioModel runScenarioModel = new RunScenarioModel();
runScenarioModel.getDefinedParameters().add(
new DefinedParameterModel(definedParameter.getName(),
definedParameter.getType(), definedParameter
.getPickOrder(), definedParameter
.getUpdateStrategy()));
return runScenarioModel;
}
@Test
public void test_getParaClasses() throws Exception {
ParameterizationManager parameterizationManager = new ParameterizationManager(
@ -47,7 +163,7 @@ public class Test_ParameterManager {
Map<String, Class<?>> paraClasses = (Map<String, Class<?>>) TestHelper
.invokePrivate(parameterizationManager, "getParaClasses", null,
null);
assertEquals(6, paraClasses.size());
assertEquals(7, paraClasses.size());
assertTrue(paraClasses.containsKey("Para_Table"));
assertTrue(paraClasses.containsKey("Para_GetEletronicCombine"));
assertTrue(paraClasses.containsKey("Para_IteratorNumber"));
@ -55,4 +171,5 @@ public class Test_ParameterManager {
assertTrue(paraClasses.containsKey("Para_UniqueNumber"));
assertTrue(paraClasses.containsKey("Para_UserNameAndPassword"));
}
}