diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java index 736b1032..13e660b2 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java @@ -275,13 +275,13 @@ public class TestController { @RequestMapping(value = "/clean/{runId}", method = RequestMethod.GET) @ResponseBody public CleanTestResultModel clean(@PathVariable UUID runId) { - ScenarioContext scenarioContext = this.getScenarioEngine() - .getRunningTests().get(runId); - if (scenarioContext == null) { - return null; - } - scenarioContext.getExecutor().shutdownNow(); - this.getScenarioEngine().getRunningTests().remove(runId); + // ScenarioContext scenarioContext = this.getScenarioEngine() + // .getRunningTests().get(runId); + // if (scenarioContext == null) { + // return null; + // } + // scenarioContext.getExecutor().shutdownNow(); + // this.getScenarioEngine().getRunningTests().remove(runId); System.gc(); CleanTestResultModel cleanTestResultModel = new CleanTestResultModel(); cleanTestResultModel.setSuccess(true); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/ClassHelper.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/helper/ClassHelper.java similarity index 83% rename from Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/ClassHelper.java rename to Bench4Q-Agent/src/main/java/org/bench4q/agent/helper/ClassHelper.java index 950bab2f..3c8330fa 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/ClassHelper.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/helper/ClassHelper.java @@ -1,4 +1,4 @@ -package org.bench4q.agent.plugin; +package org.bench4q.agent.helper; import java.io.File; import java.io.IOException; @@ -6,16 +6,38 @@ import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @Component public class ClassHelper { - public List getClassNames(String packageName, + public Map> loadClasses(String packageName, + ClassLoadRestriction classLoadRestriction) { + Map> name2ClassMap = new HashMap>(); + try { + + for (String className : getClassNames(packageName, true)) { + Class clazz = Class.forName(className); + if (classLoadRestriction.accept(clazz)) { + name2ClassMap.put(classLoadRestriction.getAcceptKey(clazz), + clazz); + } + } + return name2ClassMap; + } catch (Exception e) { + Logger.getLogger(ClassHelper.class).error(e, e); + return null; + } + } + + private List getClassNames(String packageName, boolean searchInChildPackage) { try { List classNames = new ArrayList(); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/helper/ClassLoadRestriction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/helper/ClassLoadRestriction.java new file mode 100644 index 00000000..d189199a --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/helper/ClassLoadRestriction.java @@ -0,0 +1,7 @@ +package org.bench4q.agent.helper; + +public interface ClassLoadRestriction { + public boolean accept(Class classUnderTest); + + public String getAcceptKey(Class classUnderTest); +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/ParameterizedClass.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/ParameterizedClass.java new file mode 100644 index 00000000..42df643f --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/ParameterizedClass.java @@ -0,0 +1,12 @@ +package org.bench4q.agent.parameterization; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface ParameterizedClass { + String name(); +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/PickOrder.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/PickOrder.java new file mode 100644 index 00000000..a7eaaab1 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/PickOrder.java @@ -0,0 +1,5 @@ +package org.bench4q.agent.parameterization; + +public enum PickOrder { + UNIQUE, SEQUENTIAL, RANDOM +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/UpdateStrategy.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/UpdateStrategy.java new file mode 100644 index 00000000..3bdc959f --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/UpdateStrategy.java @@ -0,0 +1,5 @@ +package org.bench4q.agent.parameterization; + +public enum UpdateStrategy { + ONCE, EACH_ITERATION, EACH_OCCURRENCE +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_GetEletronicCombine.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_GetEletronicCombine.java index 16295ae5..02fe6814 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_GetEletronicCombine.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_GetEletronicCombine.java @@ -6,21 +6,24 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.bench4q.agent.parameterization.ParameterizedClass; + +@ParameterizedClass(name = "Para_GetEletronicCombine") public class Para_GetEletronicCombine { - - public class eletronicParas - { + + public class eletronicParas { String beginUser; String endUser; String beginTime; String endTime; } + public Map useInstanceMap = new HashMap(); @SuppressWarnings("deprecation") Date currentTimeLoop = new Date("2014-01-01"); long currentUser = 10001; ReentrantReadWriteLock timeRWLock = new ReentrantReadWriteLock(); - + public Para_GetEletronicCombine() { } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_IteratorNumber.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_IteratorNumber.java index e4d42743..3fba2303 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_IteratorNumber.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_IteratorNumber.java @@ -3,6 +3,9 @@ package org.bench4q.agent.parameterization.impl; import java.util.Map; import java.util.UUID; +import org.bench4q.agent.parameterization.ParameterizedClass; + +@ParameterizedClass(name = "Para_IteratorNumber") public class Para_IteratorNumber { public Long iteratorNum = new Long(0); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_RandomNumber.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_RandomNumber.java index f0b6f360..c953c6c4 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_RandomNumber.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_RandomNumber.java @@ -3,26 +3,29 @@ package org.bench4q.agent.parameterization.impl; import java.util.Random; import java.util.UUID; +import org.bench4q.agent.parameterization.ParameterizedClass; + +@ParameterizedClass(name = "Para_RandomNumber") public class Para_RandomNumber { - - public String getRandomIntegerNumber(UUID id,String begin, String end, String stringformat) - { - + + public String getRandomIntegerNumber(UUID id, String begin, String end, + String stringformat) { + Random r = new Random(); int beginNum = Integer.parseInt(begin); int endNum = Integer.parseInt(end); - - int result = r.nextInt(endNum-beginNum) +beginNum; - return String.format(stringformat,result); + + int result = r.nextInt(endNum - beginNum) + beginNum; + return String.format(stringformat, result); } - - public String getRandomDouble(UUID id , String begin, String end , String stringformat) - { + + public String getRandomDouble(UUID id, String begin, String end, + String stringformat) { Random r = new Random(); double beginNum = Integer.parseInt(begin); double endNum = Integer.parseInt(end); - - double result = r.nextDouble()*(endNum-beginNum) +beginNum; + + double result = r.nextDouble() * (endNum - beginNum) + beginNum; return String.format(stringformat, result); } } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_Table.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_Table.java index 08b69a6a..7f296862 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_Table.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_Table.java @@ -12,14 +12,20 @@ import java.util.UUID; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ConcurrentHashMap; -public class Para_Table extends BasePara { +import org.bench4q.agent.parameterization.ParameterizedClass; +import org.bench4q.agent.parameterization.PickOrder; +import org.bench4q.agent.parameterization.UpdateStrategy; +@ParameterizedClass(name = "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 readerMap = new ConcurrentHashMap(); - public Para_Table(UUID testId) { + public Para_Table(UUID testId, PickOrder pickOrder, + UpdateStrategy updateStrategy) { + // TODO: Use the pickOrder and updateStrategy to create this parameter super(testId); } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_UniqueNumber.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_UniqueNumber.java index 24bcb320..32307d8b 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_UniqueNumber.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_UniqueNumber.java @@ -2,11 +2,13 @@ package org.bench4q.agent.parameterization.impl; import java.util.UUID; +import org.bench4q.agent.parameterization.ParameterizedClass; + +@ParameterizedClass(name = "Para_UniqueNumber") public class Para_UniqueNumber { int currentNumber = 0; - - synchronized String getNumber(UUID id) - { + + synchronized String getNumber(UUID id) { return String.valueOf(currentNumber++); } } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_UserNameAndPassword.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_UserNameAndPassword.java index 736c83ec..ddba5570 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_UserNameAndPassword.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_UserNameAndPassword.java @@ -5,6 +5,9 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.bench4q.agent.parameterization.ParameterizedClass; + +@ParameterizedClass(name = "Para_UserNameAndPassword") public class Para_UserNameAndPassword { String[] Password = new String[] { "a", "b", "c", "d", "e" }; String[] userName = new String[] { "http://www.baidu.com/", diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/ParameterThreadOption.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/ParameterThreadOption.java deleted file mode 100644 index d479b9cc..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/ParameterThreadOption.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.bench4q.agent.parameterization.impl; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class ParameterThreadOption { - public final ParameterThreadOption instance = new ParameterThreadOption(); - Map> optionMap = new HashMap>(); - private List all = Arrays.asList("crossThread", "inThread"); - // private List justIn = Arrays.asList("inThread"); - private List justCross = Arrays.asList("crossThread"); - - private ParameterThreadOption() { - optionMap.put("Para_File", all); - optionMap.put("Para_IteratorNumber", justCross); - optionMap.put("Para_RandomNumber", all); - optionMap.put("Para_UniqueNumber", all); - optionMap.put("Para_UserNameAndPassword", justCross); - } - - public List getThreadOption(String classname) { - if (this.optionMap.containsKey(classname) == false) - return all; - return optionMap.get(classname); - } -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/ParameterizationManager.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/ParameterizationManager.java new file mode 100644 index 00000000..dc9d0cc5 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/ParameterizationManager.java @@ -0,0 +1,177 @@ +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; +import org.bench4q.agent.helper.ApplicationContextHelper; +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.UpdateStrategy; +import org.bench4q.agent.scenario.DefinedParameter; + +public class ParameterizationManager { + private static final String PARAMETER_PACKAGE = "org.bench4q.agent.parameterization.impl"; + private Map objMap = new HashMap(); + private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock(); + + private Map> paraClasses; + private Map parameterMap; + private ClassHelper classHelper; + + private UUID testId; + + private UUID getTestId() { + return testId; + } + + private void setTestId(UUID testId) { + this.testId = testId; + } + + private ClassHelper getClassHelper() { + return classHelper; + } + + private void setClassHelper(ClassHelper classHelper) { + this.classHelper = classHelper; + } + + private Map getParameterMap() { + return parameterMap; + } + + private void setParameterMap(Map parameterMap) { + this.parameterMap = parameterMap; + } + + private Map> getParaClasses() { + return paraClasses; + } + + private void setParaClasses(Map> paraClasses) { + this.paraClasses = paraClasses; + } + + public ParameterizationManager() { + this.setClassHelper(ApplicationContextHelper.getContext().getBean( + ClassHelper.class)); + this.setParaClasses(this.getClassHelper().loadClasses( + PARAMETER_PACKAGE, new ClassLoadRestriction() { + public boolean accept(Class classUnderTest) { + return classUnderTest + .isAnnotationPresent(ParameterizedClass.class); + } + + public String getAcceptKey(Class classUnderTest) { + return classUnderTest.getAnnotation( + ParameterizedClass.class).name(); + } + })); + } + + public ParameterizationManager(UUID testId, int parameterCount) { + this(); + this.setTestId(testId); + this.setParameterMap(new HashMap(parameterCount)); + + } + + public VUserContext buildVUserContext() { + return new VUserContext(this); + } + + public void cleanUp() { + this.objMap.clear(); + this.getParameterMap().clear(); + } + + public Object getObjWithGuardExist(String className) { + Object result = null; + className = ParamFormatParser.buildFullPath(className); + if (!guardClassExist(className)) + throw new NullPointerException("Not find " + className); + mapRWLock.readLock().lock(); + result = objMap.get(className); + mapRWLock.readLock().unlock(); + return result; + } + + private boolean guardClassExist(String className) { + return containObj(className) ? true : createObj(className); + } + + private boolean containObj(String className) { + boolean ret = false; + mapRWLock.readLock().lock(); + ret = objMap.containsKey(className); + mapRWLock.readLock().unlock(); + return ret; + } + + private boolean createObj(String fullClassName) { + try { + UserDefinedClassLoader cl = new UserDefinedClassLoader( + Main.USER_DEFINED_PARAMS_FOLDER); + Class cls = cl.loadClass(fullClassName); + Constructor contructor = cls.getConstructor(new Class[] { + UUID.class, PickOrder.class, UpdateStrategy.class }); + Object instance = contructor.newInstance(new Object[] { + this.getTestId(), PickOrder.SEQUENTIAL, + UpdateStrategy.EACH_ITERATION }); + mapRWLock.writeLock().lock(); + objMap.put(fullClassName, instance); + return true; + } catch (Exception ex) { + Logger.getLogger(ParameterizationManager.class).error(ex, ex); + return false; + } finally { + if (mapRWLock.writeLock().isHeldByCurrentThread()) + mapRWLock.writeLock().unlock(); + } + } + + /** + * For creating parameter with parameterName as the index + * + * @param definedParameter + */ + public boolean createParaInstanceWith(DefinedParameter definedParameter) { + try { + this.getParameterMap().put( + definedParameter.getName(), + loadClassWith(definedParameter.getType(), PickOrder + .valueOf(definedParameter.getPickOrder()), + UpdateStrategy.valueOf(definedParameter + .getUpdateStrategy()))); + + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + private Object loadClassWith(String name, PickOrder pickOrder, + UpdateStrategy updateStrategy) { + if (!this.getParaClasses().containsKey(name)) { + throw new NullPointerException("There's no class with " + name); + } + Class clazz = this.getParaClasses().get(name); + try { + Constructor constructor = clazz.getConstructor(UUID.class, + PickOrder.class, UpdateStrategy.class); + return constructor.newInstance(this.getTestId(), pickOrder, + updateStrategy); + } catch (Exception e) { + throw new NullPointerException( + "There's no constructor with three params"); + } + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/ParametersFactory.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/ParametersFactory.java deleted file mode 100644 index e280a949..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/ParametersFactory.java +++ /dev/null @@ -1,85 +0,0 @@ -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; -import org.bench4q.agent.scenario.DefinedParameter; - -public class ParametersFactory { - private Map objMap = new HashMap(); - private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock(); - private UUID testId; - - private UUID getTestId() { - return testId; - } - - private void setTestId(UUID testId) { - this.testId = testId; - } - - public ParametersFactory(UUID testId) { - this.setTestId(testId); - } - - public VUserContext buildVUserContext() { - return new VUserContext(this); - } - - public void cleanUp() { - this.objMap.clear(); - } - - public Object getObjWithGuardExist(String className) { - Object result = null; - className = ParamFormatParser.buildFullPath(className); - if (!guardClassExist(className)) - throw new NullPointerException("Not find " + className); - mapRWLock.readLock().lock(); - result = objMap.get(className); - mapRWLock.readLock().unlock(); - return result; - } - - private boolean guardClassExist(String className) { - return containObj(className) ? true : createObj(className); - } - - private boolean containObj(String className) { - boolean ret = false; - mapRWLock.readLock().lock(); - ret = objMap.containsKey(className); - mapRWLock.readLock().unlock(); - return ret; - } - - private boolean createObj(String className) { - try { - UserDefinedClassLoader cl = new UserDefinedClassLoader( - Main.USER_DEFINED_PARAMS_FOLDER); - Class cls = cl.loadClass(className); - 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) { - Logger.getLogger(ParametersFactory.class).error(ex, ex); - return false; - } finally { - if (mapRWLock.writeLock().isHeldByCurrentThread()) - mapRWLock.writeLock().unlock(); - } - } - - public void createParaInstanceWith(DefinedParameter dP) { - - } -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/VUserContext.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/VUserContext.java index d1e5665e..9816d637 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/VUserContext.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/VUserContext.java @@ -14,7 +14,7 @@ import org.bench4q.agent.parameterization.SessionObject; public class VUserContext implements SessionObject { private UUID id = UUID.randomUUID(); - private ParametersFactory paramterFactory; + private ParameterizationManager paramterFactory; private Set usedClassName = new HashSet(); private Map objMap = new HashMap(); private Map runtimeParamMap = new HashMap(); @@ -22,18 +22,18 @@ public class VUserContext implements SessionObject { private ReentrantReadWriteLock mapRWLock = new ReentrantReadWriteLock(); private Logger logger = Logger.getLogger(VUserContext.class); - private ParametersFactory getParamterFactory() { + private ParameterizationManager getParamterFactory() { return paramterFactory; } - private void setParamterFactory(ParametersFactory paramterFactory) { + private void setParamterFactory(ParameterizationManager paramterFactory) { this.paramterFactory = paramterFactory; } private VUserContext() { } - public VUserContext(ParametersFactory parametersFactory) { + public VUserContext(ParameterizationManager parametersFactory) { this(); this.setParamterFactory(parametersFactory); } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/PluginManager.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/PluginManager.java index bff701af..922e9fe9 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/PluginManager.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/PluginManager.java @@ -4,17 +4,19 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; +import org.bench4q.agent.helper.ClassHelper; +import org.bench4q.agent.helper.ClassLoadRestriction; import org.bench4q.agent.share.DealWithLog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class PluginManager { + private static final String PLUGIN_PACKAGE_NAME = "org.bench4q.agent.plugin"; private ClassHelper classHelper; private TypeConverter typeConverter; private Map> plugins; @@ -24,7 +26,17 @@ public class PluginManager { public PluginManager(ClassHelper classHelper, TypeConverter typeConverter) { this.setClassHelper(classHelper); this.setTypeConverter(typeConverter); - this.setPlugins(this.loadPlugins("org.bench4q.agent.plugin")); + this.setPlugins(this.getClassHelper().loadClasses(PLUGIN_PACKAGE_NAME, + new ClassLoadRestriction() { + public boolean accept(Class classUnderTest) { + return classUnderTest.isAnnotationPresent(Plugin.class); + } + + public String getAcceptKey(Class classUnderTest) { + return classUnderTest.getAnnotation(Plugin.class) + .value(); + } + })); } private ClassHelper getClassHelper() { @@ -51,24 +63,6 @@ public class PluginManager { this.plugins = plugins; } - private Map> loadPlugins(String packageName) { - try { - List classNames = this.getClassHelper().getClassNames( - packageName, true); - Map> ret = new HashMap>(); - for (String className : classNames) { - Class plugin = Class.forName(className); - if (plugin.isAnnotationPresent(Plugin.class)) { - ret.put(plugin.getAnnotation(Plugin.class).value(), plugin); - } - } - return ret; - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - public List getPluginInfo() { try { Map> plugins = this.getPlugins(); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java index 6219e740..8a86cafd 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java @@ -9,7 +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; +import org.bench4q.agent.parameterization.impl.ParameterizationManager; import org.bench4q.agent.parameterization.impl.VUserContext; public class ScenarioContext { @@ -20,7 +20,7 @@ public class ScenarioContext { private Scenario scenario; private boolean finished; private DataCollector dataStatistics; - private ParametersFactory parameterFactory; + private ParameterizationManager parameterFactory; public Date getStartDate() { return startDate; @@ -70,11 +70,11 @@ public class ScenarioContext { this.dataStatistics = dataStatistics; } - public ParametersFactory getParameterFactory() { + public ParameterizationManager getParameterFactory() { return parameterFactory; } - private void setParameterFactory(ParametersFactory parameterFactory) { + private void setParameterFactory(ParameterizationManager parameterFactory) { this.parameterFactory = parameterFactory; } @@ -92,7 +92,8 @@ public class ScenarioContext { scenarioContext.setStartDate(new Date(System.currentTimeMillis())); scenarioContext.setExecutorService(executor); scenarioContext.setDataStatistics(new ScenarioResultCollector(testId)); - scenarioContext.setParameterFactory(new ParametersFactory(testId)); + scenarioContext.setParameterFactory(new ParameterizationManager(testId, + scenario.getDefinedParameters().length)); scenarioContext.prepareForParameterization(); return scenarioContext; } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/SessionContext.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/SessionContext.java deleted file mode 100644 index ac9813bf..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/SessionContext.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.bench4q.agent.scenario; - -import java.util.HashMap; -import java.util.Map; - -public class SessionContext { - Map variables; - - private Map getVariables() { - return variables; - } - - private void setVariables(Map variables) { - this.variables = variables; - } - - public SessionContext() { - this.setVariables(new HashMap()); - } - - public void addAVariable(String entryName, String entryValue) { - this.getVariables().put(entryName, entryValue); - } - - public String getVariable(String entryName) { - return this.getVariable(entryName); - } -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/VUser.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/VUser.java index 817fffaf..4f18db4d 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/VUser.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/VUser.java @@ -60,9 +60,11 @@ public class VUser implements Runnable { private void doCleanUp() { this.sessionObject.doCleanUp(); + this.setSessionObject(null); + this.setScenarioContext(null); } - public void doRunScenario(ScenarioContext context) { + private void doRunScenario(ScenarioContext context) { Map plugins = new LinkedHashMap(); preparePlugins(context.getScenario(), plugins); for (int i = 0; i < context.getScenario().getPages().length; i++) { diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/TEST_HelloThread.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/TEST_HelloThread.java index a9ff0e04..1184b9e6 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/TEST_HelloThread.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/TEST_HelloThread.java @@ -3,7 +3,7 @@ package org.bench4q.agent.test.parameterization; import java.util.UUID; import org.bench4q.agent.parameterization.impl.VUserContext; -import org.bench4q.agent.parameterization.impl.ParametersFactory; +import org.bench4q.agent.parameterization.impl.ParameterizationManager; import org.bench4q.share.helper.TestHelper; public class TEST_HelloThread extends Thread { @@ -11,7 +11,7 @@ public class TEST_HelloThread extends Thread { public TEST_HelloThread() { UUID testId = UUID.randomUUID(); - ic = new VUserContext(new ParametersFactory(testId)); + ic = new VUserContext(new ParameterizationManager(testId, 4)); } public void run() { diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_MyFileClassLoader.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_MyFileClassLoader.java deleted file mode 100644 index e23a3144..00000000 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_MyFileClassLoader.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.bench4q.agent.test.parameterization; - -import static org.junit.Assert.*; - -import java.lang.reflect.Method; - -import org.bench4q.agent.parameterization.impl.UserDefinedClassLoader; -import org.junit.Test; - -public class Test_MyFileClassLoader { - private String Test_ClassPath = "E:\\j2ee_proj\\skythink\\WebContent\\WEB-INF\\classes\\"; - - @Test - public void test() throws ClassNotFoundException, InstantiationException, - IllegalAccessException { - UserDefinedClassLoader fileClsLoader = new UserDefinedClassLoader(Test_ClassPath); - - Class cls = fileClsLoader - .loadClass("com.cmw.entity.sys.AccordionEntity"); - Object obj = cls.newInstance(); - Method[] mthds = cls.getMethods(); - for (Method mthd : mthds) { - String methodName = mthd.getName(); - System.out.println("mthd.name=" + methodName); - } - System.out.println("obj.class=" + obj.getClass().getName()); - System.out.println("obj.class=" + cls.getClassLoader().toString()); - System.out.println("obj.class=" - + cls.getClassLoader().getParent().toString()); - assertTrue(true); - } - -} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_ParameterManager.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_ParameterManager.java new file mode 100644 index 00000000..abd2dfc5 --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_ParameterManager.java @@ -0,0 +1,58 @@ +package org.bench4q.agent.test.parameterization; + +import static org.junit.Assert.*; + +import java.util.Map; +import java.util.UUID; + +import org.bench4q.agent.parameterization.PickOrder; +import org.bench4q.agent.parameterization.UpdateStrategy; +import org.bench4q.agent.parameterization.impl.ParameterizationManager; +import org.bench4q.agent.scenario.DefinedParameter; +import org.bench4q.share.helper.TestHelper; +import org.junit.Test; +import org.junit.runner.RunWith; +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_ParameterManager { + + @Test + public void test_createParaInstanceWith() throws Exception { + ParameterizationManager parameterizationManager = new ParameterizationManager( + UUID.randomUUID(), 1); + @SuppressWarnings("unchecked") + Map parameterMapBeforeCreate = (Map) 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()))); + @SuppressWarnings("unchecked") + Map parameterMapAfterCreate = (Map) TestHelper + .invokePrivate(parameterizationManager, "getParameterMap", + null, null); + assertEquals(1, parameterMapAfterCreate.size()); + } + + @Test + public void test_getParaClasses() throws Exception { + ParameterizationManager parameterizationManager = new ParameterizationManager( + UUID.randomUUID(), 1); + @SuppressWarnings("unchecked") + Map> paraClasses = (Map>) TestHelper + .invokePrivate(parameterizationManager, "getParaClasses", null, + null); + assertEquals(6, paraClasses.size()); + assertTrue(paraClasses.containsKey("Para_Table")); + assertTrue(paraClasses.containsKey("Para_GetEletronicCombine")); + assertTrue(paraClasses.containsKey("Para_IteratorNumber")); + assertTrue(paraClasses.containsKey("Para_RandomNumber")); + assertTrue(paraClasses.containsKey("Para_UniqueNumber")); + assertTrue(paraClasses.containsKey("Para_UserNameAndPassword")); + } +} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_VUserContext.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_VUserContext.java index 5349da40..6448cd29 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_VUserContext.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/parameterization/Test_VUserContext.java @@ -8,10 +8,12 @@ import java.util.HashMap; 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.VUserContext; 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.agent.parameterization.impl.ParameterizationManager; import org.bench4q.share.helper.TestHelper; import org.junit.After; import org.junit.Test; @@ -36,7 +38,8 @@ public class Test_VUserContext { @Test public void testGetParam() throws Exception { UUID testId = UUID.randomUUID(); - Para_Table table = new Para_Table(testId); + Para_Table table = new Para_Table(testId, PickOrder.SEQUENTIAL, + UpdateStrategy.EACH_ITERATION); createParamFileFor(table.getParamFileFullPath("param1")); String ret = table.getTableColumnValue(java.util.UUID.randomUUID(), new HashMap(), "param1", "0", "sequence", ";", @@ -55,10 +58,11 @@ public class Test_VUserContext { @Test public void testGetParam2() throws Exception { UUID testId = UUID.randomUUID(); - ParametersFactory parametersFactory = new ParametersFactory(testId); + ParameterizationManager parametersFactory = new ParameterizationManager( + testId, 4); VUserContext ic = new VUserContext(parametersFactory); - createParamFileFor(new Para_Table(testId) - .getParamFileFullPath("param1")); + createParamFileFor(new Para_Table(testId, PickOrder.SEQUENTIAL, + UpdateStrategy.EACH_ITERATION).getParamFileFullPath("param1")); String passwordName = ic.getParam(Test_CASE1); System.out.println(passwordName); assertNotNull(passwordName); @@ -81,9 +85,10 @@ public class Test_VUserContext { @Test public void testGetParameter() throws Exception { UUID testId = UUID.randomUUID(); - createParamFileFor(new Para_Table(testId) - .getParamFileFullPath("param1")); - VUserContext paracCoor = new VUserContext(new ParametersFactory(testId)); + createParamFileFor(new Para_Table(testId, PickOrder.SEQUENTIAL, + UpdateStrategy.EACH_ITERATION).getParamFileFullPath("param1")); + VUserContext paracCoor = new VUserContext(new ParameterizationManager( + testId, 4)); ParamFormatParser paramFormatParser = ParamFormatParser .parseInputParameter(Test_CASE1); String result = (String) TestHelper.invokePrivate(paracCoor, @@ -96,9 +101,10 @@ public class Test_VUserContext { @Test public void testGetParameterCrossThread() throws Exception { UUID testId = UUID.randomUUID(); - createParamFileFor(new Para_Table(testId) - .getParamFileFullPath("param1")); - VUserContext paracCoor = new VUserContext(new ParametersFactory(testId)); + createParamFileFor(new Para_Table(testId, PickOrder.SEQUENTIAL, + UpdateStrategy.EACH_ITERATION).getParamFileFullPath("param1")); + VUserContext paracCoor = new VUserContext(new ParameterizationManager( + testId, 4)); ParamFormatParser paramFormatParser = ParamFormatParser .parseInputParameter(Test_CASE1); String result = (String) TestHelper.invokePrivate( diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ClassHelper.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ClassHelper.java new file mode 100644 index 00000000..290f8eb7 --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ClassHelper.java @@ -0,0 +1,30 @@ +package org.bench4q.agent.test.plugin; + +import java.util.Map; + +import org.bench4q.agent.helper.ClassHelper; +import org.bench4q.agent.helper.ClassLoadRestriction; +import org.bench4q.agent.plugin.Plugin; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class Test_ClassHelper { + + @Test + public void test_loadClasses() { + Map> plugins = new ClassHelper().loadClasses( + "org.bench4q.agent.plugin", new ClassLoadRestriction() { + public boolean accept(Class classUnderTest) { + return classUnderTest.isAnnotationPresent(Plugin.class); + } + + public String getAcceptKey(Class classUnderTest) { + return classUnderTest.getAnnotation(Plugin.class) + .value(); + } + }); + assertEquals(5, plugins.size()); + assertEquals(true, plugins.containsKey("Http")); + } +} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_PluginManager.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_PluginManager.java new file mode 100644 index 00000000..5885fa1a --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_PluginManager.java @@ -0,0 +1,19 @@ +package org.bench4q.agent.test.plugin; + +import org.bench4q.agent.helper.ClassHelper; +import org.bench4q.agent.plugin.PluginManager; +import org.bench4q.agent.plugin.TypeConverter; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class Test_PluginManager { + @Test + public void test_getInit() { + PluginManager pluginManager = new PluginManager(new ClassHelper(), + new TypeConverter()); + assertEquals(5, pluginManager.getPlugins().size()); + assertTrue(pluginManager.getPlugins().containsKey( + "org.bench4q.agent.plugin.basic.http.HttpPlugin")); + } +} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java index ce91750a..a578b9dc 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java @@ -8,6 +8,8 @@ import java.util.UUID; 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.ScenarioContext; @@ -57,8 +59,8 @@ public class Test_ScenarioContext { private void createParamFileAndFillParamContent(String param_name, UUID testId) throws IOException { - File parmFile = new File( - new Para_Table(testId).getParamFileFullPath(param_name)); + File parmFile = new File(new Para_Table(testId, PickOrder.SEQUENTIAL, + UpdateStrategy.EACH_ITERATION).getParamFileFullPath(param_name)); TestHelper.createFileIfNotExist(parmFile); FileUtils.writeStringToFile(parmFile, "row1;10;11~row2;20;21~row3,30,31~"); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java index 6985fd74..610e9ade 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java @@ -11,6 +11,7 @@ import org.bench4q.agent.plugin.basic.http.HttpReturn; import org.bench4q.agent.scenario.Scenario; 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.junit.Test; import org.junit.runner.RunWith; @@ -91,6 +92,14 @@ public class Test_VUser { private VUser createAWorker() { return new VUser(ScenarioContext.buildScenarioContext( - UUID.randomUUID(), new Scenario(), 10)); + UUID.randomUUID(), + Scenario.scenarioBuilder(Test_HBasePlugin.buildScenario(10)), + 10)); + } + + @Test + public void test_run() { + VUser vUser = createAWorker(); + vUser.run(); } }