some refactoring.
This commit is contained in:
parent
e6b57ec27a
commit
14e01cb323
|
@ -38,32 +38,45 @@ public class PluginController {
|
|||
PluginInfoListModel pluginInfoListModel = new PluginInfoListModel();
|
||||
pluginInfoListModel.setPlugins(new ArrayList<PluginInfoModel>());
|
||||
for (PluginInfo pluginInfo : pluginInfos) {
|
||||
PluginInfoModel pluginInfoModel = new PluginInfoModel();
|
||||
pluginInfoModel.setName(pluginInfo.getName());
|
||||
pluginInfoModel.setParameters(new ArrayList<ParameterInfoModel>());
|
||||
for (ParameterInfo param : pluginInfo.getParameters()) {
|
||||
ParameterInfoModel model = new ParameterInfoModel();
|
||||
model.setName(param.getName());
|
||||
model.setType(param.getType());
|
||||
pluginInfoModel.getParameters().add(model);
|
||||
}
|
||||
pluginInfoModel.setBehaviors(new ArrayList<BehaviorInfoModel>());
|
||||
for (BehaviorInfo behaviorInfo : pluginInfo.getBehaviors()) {
|
||||
BehaviorInfoModel behaviorInfoModel = new BehaviorInfoModel();
|
||||
behaviorInfoModel.setName(behaviorInfo.getName());
|
||||
behaviorInfoModel
|
||||
.setParameters(new ArrayList<ParameterInfoModel>());
|
||||
for (ParameterInfo param : behaviorInfo.getParameters()) {
|
||||
ParameterInfoModel model = new ParameterInfoModel();
|
||||
model.setName(param.getName());
|
||||
model.setType(param.getType());
|
||||
behaviorInfoModel.getParameters().add(model);
|
||||
}
|
||||
pluginInfoModel.getBehaviors().add(behaviorInfoModel);
|
||||
}
|
||||
PluginInfoModel pluginInfoModel = buildPluginInfoModel(pluginInfo);
|
||||
pluginInfoListModel.getPlugins().add(pluginInfoModel);
|
||||
}
|
||||
return pluginInfoListModel;
|
||||
}
|
||||
|
||||
private PluginInfoModel buildPluginInfoModel(PluginInfo pluginInfo) {
|
||||
PluginInfoModel pluginInfoModel = new PluginInfoModel();
|
||||
pluginInfoModel.setName(pluginInfo.getName());
|
||||
pluginInfoModel.setParameters(new ArrayList<ParameterInfoModel>());
|
||||
for (ParameterInfo param : pluginInfo.getParameters()) {
|
||||
ParameterInfoModel model = buildParameterInfoModel(param);
|
||||
pluginInfoModel.getParameters().add(model);
|
||||
}
|
||||
pluginInfoModel.setBehaviors(new ArrayList<BehaviorInfoModel>());
|
||||
for (BehaviorInfo behaviorInfo : pluginInfo.getBehaviors()) {
|
||||
BehaviorInfoModel behaviorInfoModel = buildBehaviorInfoModel(behaviorInfo);
|
||||
pluginInfoModel.getBehaviors().add(behaviorInfoModel);
|
||||
}
|
||||
return pluginInfoModel;
|
||||
}
|
||||
|
||||
private BehaviorInfoModel buildBehaviorInfoModel(BehaviorInfo behaviorInfo) {
|
||||
BehaviorInfoModel behaviorInfoModel = new BehaviorInfoModel();
|
||||
behaviorInfoModel.setName(behaviorInfo.getName());
|
||||
behaviorInfoModel
|
||||
.setParameters(new ArrayList<ParameterInfoModel>());
|
||||
for (ParameterInfo param : behaviorInfo.getParameters()) {
|
||||
ParameterInfoModel model = buildParameterInfoModel(param);
|
||||
behaviorInfoModel.getParameters().add(model);
|
||||
}
|
||||
return behaviorInfoModel;
|
||||
}
|
||||
|
||||
private ParameterInfoModel buildParameterInfoModel(ParameterInfo param) {
|
||||
ParameterInfoModel model = new ParameterInfoModel();
|
||||
model.setName(param.getName());
|
||||
model.setType(param.getType());
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -63,15 +63,15 @@ public class TestController {
|
|||
usePlugin.setName(usePluginModel.getName());
|
||||
usePlugin.setParameters(new Parameter[usePluginModel
|
||||
.getParameters().size()]);
|
||||
scenario.getUsePlugins()[i] = usePlugin;
|
||||
for (j = 0; j < usePluginModel.getParameters().size(); j++) {
|
||||
ParameterModel parameterModel = usePluginModel.getParameters()
|
||||
.get(j);
|
||||
Parameter parameter = new Parameter();
|
||||
parameter.setKey(parameterModel.getKey());
|
||||
parameter.setValue(parameterModel.getValue());
|
||||
scenario.getUsePlugins()[i].getParameters()[j] = parameter;
|
||||
usePlugin.getParameters()[j] = parameter;
|
||||
}
|
||||
scenario.getUsePlugins()[i] = usePlugin;
|
||||
}
|
||||
for (i = 0; i < runScenarioModel.getUserBehaviors().size(); i++) {
|
||||
UserBehaviorModel userBehaviorModel = runScenarioModel
|
||||
|
|
|
@ -12,6 +12,7 @@ import javassist.CtClass;
|
|||
import javassist.CtConstructor;
|
||||
import javassist.CtMethod;
|
||||
import javassist.Modifier;
|
||||
import javassist.NotFoundException;
|
||||
import javassist.bytecode.CodeAttribute;
|
||||
import javassist.bytecode.LocalVariableAttribute;
|
||||
import javassist.bytecode.MethodInfo;
|
||||
|
@ -90,12 +91,8 @@ public class PluginManager {
|
|||
pluginInfo.setBehaviors(new BehaviorInfo[behaviors.length]);
|
||||
int i = 0;
|
||||
for (i = 0; i < behaviors.length; i++) {
|
||||
BehaviorInfo behaviorInfo = new BehaviorInfo();
|
||||
CtMethod behaviorMethod = behaviors[i];
|
||||
behaviorInfo.setName(((Behavior) behaviorMethod
|
||||
.getAnnotation(Behavior.class)).value());
|
||||
behaviorInfo.setParameters(this
|
||||
.getParameters(behaviorMethod));
|
||||
BehaviorInfo behaviorInfo = buildBehaviorInfo(behaviorMethod);
|
||||
pluginInfo.getBehaviors()[i] = behaviorInfo;
|
||||
}
|
||||
ret.add(pluginInfo);
|
||||
|
@ -107,6 +104,15 @@ public class PluginManager {
|
|||
}
|
||||
}
|
||||
|
||||
private BehaviorInfo buildBehaviorInfo(CtMethod behaviorMethod)
|
||||
throws ClassNotFoundException {
|
||||
BehaviorInfo behaviorInfo = new BehaviorInfo();
|
||||
behaviorInfo.setName(((Behavior) behaviorMethod
|
||||
.getAnnotation(Behavior.class)).value());
|
||||
behaviorInfo.setParameters(this.getParameters(behaviorMethod));
|
||||
return behaviorInfo;
|
||||
}
|
||||
|
||||
private ParameterInfo[] getParameters(CtBehavior behavior) {
|
||||
try {
|
||||
MethodInfo methodInfo = behavior.getMethodInfo();
|
||||
|
@ -118,11 +124,8 @@ public class PluginManager {
|
|||
int i;
|
||||
int pos = Modifier.isStatic(behavior.getModifiers()) ? 0 : 1;
|
||||
for (i = 0; i < parameterCount; i++) {
|
||||
ParameterInfo parameterInfo = new ParameterInfo();
|
||||
parameterInfo.setName(localVariableAttribute.variableName(i
|
||||
+ pos));
|
||||
parameterInfo
|
||||
.setType(behavior.getParameterTypes()[i].getName());
|
||||
ParameterInfo parameterInfo = buildParameterInfo(behavior,
|
||||
localVariableAttribute, i, pos);
|
||||
parameterNames[i] = parameterInfo;
|
||||
}
|
||||
return parameterNames;
|
||||
|
@ -132,6 +135,15 @@ public class PluginManager {
|
|||
}
|
||||
}
|
||||
|
||||
private ParameterInfo buildParameterInfo(CtBehavior behavior,
|
||||
LocalVariableAttribute localVariableAttribute, int i, int pos)
|
||||
throws NotFoundException {
|
||||
ParameterInfo parameterInfo = new ParameterInfo();
|
||||
parameterInfo.setName(localVariableAttribute.variableName(i + pos));
|
||||
parameterInfo.setType(behavior.getParameterTypes()[i].getName());
|
||||
return parameterInfo;
|
||||
}
|
||||
|
||||
private CtMethod[] getBehaviors(CtClass plugin) {
|
||||
try {
|
||||
CtMethod[] ctMethods = plugin.getMethods();
|
||||
|
@ -192,32 +204,11 @@ public class PluginManager {
|
|||
public Object doBehavior(Object plugin, String behaviorName,
|
||||
Map<String, String> parameters) {
|
||||
try {
|
||||
ClassPool classPool = ClassPool.getDefault();
|
||||
CtClass ctClass = classPool.get(plugin.getClass()
|
||||
.getCanonicalName());
|
||||
CtMethod[] ctMethods = ctClass.getMethods();
|
||||
CtMethod ctMethod = null;
|
||||
|
||||
int i = 0;
|
||||
for (i = 0; i < ctMethods.length; i++) {
|
||||
if (ctMethods[i].hasAnnotation(Behavior.class)) {
|
||||
if (((Behavior) ctMethods[i].getAnnotation(Behavior.class))
|
||||
.value().equals(behaviorName)) {
|
||||
ctMethod = ctMethods[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
CtMethod ctMethod = findCtMethod(plugin, behaviorName);
|
||||
if (ctMethod == null) {
|
||||
return null;
|
||||
}
|
||||
Method[] methods = plugin.getClass().getMethods();
|
||||
Method method = null;
|
||||
for (i = 0; i < methods.length; i++) {
|
||||
if (methods[i].getName().equals(ctMethod.getName())) {
|
||||
method = methods[i];
|
||||
}
|
||||
}
|
||||
Method method = findMethod(plugin, ctMethod);
|
||||
if (method == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -228,4 +219,38 @@ public class PluginManager {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Method findMethod(Object plugin, CtMethod ctMethod) {
|
||||
int i;
|
||||
Method[] methods = plugin.getClass().getMethods();
|
||||
Method method = null;
|
||||
for (i = 0; i < methods.length; i++) {
|
||||
if (methods[i].getName().equals(ctMethod.getName())) {
|
||||
method = methods[i];
|
||||
}
|
||||
}
|
||||
return method;
|
||||
}
|
||||
|
||||
private CtMethod findCtMethod(Object plugin, String behaviorName) {
|
||||
try {
|
||||
ClassPool classPool = ClassPool.getDefault();
|
||||
CtClass ctClass = classPool.get(plugin.getClass()
|
||||
.getCanonicalName());
|
||||
CtMethod[] ctMethods = ctClass.getMethods();
|
||||
int i = 0;
|
||||
for (i = 0; i < ctMethods.length; i++) {
|
||||
if (ctMethods[i].hasAnnotation(Behavior.class)) {
|
||||
if (((Behavior) ctMethods[i].getAnnotation(Behavior.class))
|
||||
.value().equals(behaviorName)) {
|
||||
return ctMethods[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package org.bench4q.agent.scenario;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
|
@ -14,9 +13,7 @@ import java.util.concurrent.ExecutorService;
|
|||
import java.util.concurrent.Executors;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.PropertyException;
|
||||
|
||||
import org.bench4q.agent.plugin.Plugin;
|
||||
import org.bench4q.agent.plugin.PluginManager;
|
||||
|
@ -52,19 +49,13 @@ public class ScenarioEngine {
|
|||
public void runScenario(UUID runId, final Scenario scenario, int poolSize,
|
||||
int totalCount) {
|
||||
try {
|
||||
ScenarioContext scenarioContext = new ScenarioContext();
|
||||
scenarioContext.setScenario(scenario);
|
||||
scenarioContext.setTotalCount(totalCount
|
||||
* scenario.getUserBehaviors().length);
|
||||
scenarioContext.setPoolSize(poolSize);
|
||||
scenarioContext.setStartDate(new Date(System.currentTimeMillis()));
|
||||
ExecutorService executorService = Executors
|
||||
.newFixedThreadPool(poolSize);
|
||||
scenarioContext.setExecutorService(executorService);
|
||||
int i;
|
||||
final List<BehaviorResult> ret = Collections
|
||||
.synchronizedList(new ArrayList<BehaviorResult>());
|
||||
scenarioContext.setResults(ret);
|
||||
ScenarioContext scenarioContext = buildScenarioContext(scenario,
|
||||
poolSize, totalCount, executorService, ret);
|
||||
int i;
|
||||
this.getRunningTests().put(runId, scenarioContext);
|
||||
Runnable runnable = new Runnable() {
|
||||
public void run() {
|
||||
|
@ -80,8 +71,67 @@ public class ScenarioEngine {
|
|||
}
|
||||
}
|
||||
|
||||
private ScenarioContext buildScenarioContext(final Scenario scenario,
|
||||
int poolSize, int totalCount, ExecutorService executorService,
|
||||
final List<BehaviorResult> ret) {
|
||||
ScenarioContext scenarioContext = new ScenarioContext();
|
||||
scenarioContext.setScenario(scenario);
|
||||
scenarioContext.setTotalCount(totalCount
|
||||
* scenario.getUserBehaviors().length);
|
||||
scenarioContext.setPoolSize(poolSize);
|
||||
scenarioContext.setStartDate(new Date(System.currentTimeMillis()));
|
||||
scenarioContext.setExecutorService(executorService);
|
||||
scenarioContext.setResults(ret);
|
||||
return scenarioContext;
|
||||
}
|
||||
|
||||
private List<BehaviorResult> doRunScenario(Scenario scenario) {
|
||||
Map<String, Object> plugins = new HashMap<String, Object>();
|
||||
preparePlugins(scenario, plugins);
|
||||
|
||||
List<BehaviorResult> ret = new ArrayList<BehaviorResult>();
|
||||
for (UserBehavior userBehavior : scenario.getUserBehaviors()) {
|
||||
Object plugin = plugins.get(userBehavior.getUse());
|
||||
String behaviorName = userBehavior.getName();
|
||||
Map<String, String> behaviorParameters = prepareBehaviorParameters(userBehavior);
|
||||
Date startDate = new Date(System.currentTimeMillis());
|
||||
boolean success = (Boolean) this.getPluginManager().doBehavior(
|
||||
plugin, behaviorName, behaviorParameters);
|
||||
Date endDate = new Date(System.currentTimeMillis());
|
||||
BehaviorResult result = buildBehaviorResult(userBehavior, plugin,
|
||||
behaviorName, startDate, success, endDate);
|
||||
ret.add(result);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private BehaviorResult buildBehaviorResult(UserBehavior userBehavior,
|
||||
Object plugin, String behaviorName, Date startDate,
|
||||
boolean success, Date endDate) {
|
||||
BehaviorResult result = new BehaviorResult();
|
||||
result.setId(UUID.randomUUID());
|
||||
result.setStartDate(startDate);
|
||||
result.setEndDate(endDate);
|
||||
result.setSuccess(success);
|
||||
result.setResponseTime(result.getEndDate().getTime()
|
||||
- result.getStartDate().getTime());
|
||||
result.setBehaviorName(behaviorName);
|
||||
result.setPluginId(userBehavior.getUse());
|
||||
result.setPluginName(plugin.getClass().getAnnotation(Plugin.class)
|
||||
.value());
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, String> prepareBehaviorParameters(
|
||||
UserBehavior userBehavior) {
|
||||
Map<String, String> behaviorParameters = new HashMap<String, String>();
|
||||
for (Parameter parameter : userBehavior.getParameters()) {
|
||||
behaviorParameters.put(parameter.getKey(), parameter.getValue());
|
||||
}
|
||||
return behaviorParameters;
|
||||
}
|
||||
|
||||
private void preparePlugins(Scenario scenario, Map<String, Object> plugins) {
|
||||
for (UsePlugin usePlugin : scenario.getUsePlugins()) {
|
||||
String pluginId = usePlugin.getId();
|
||||
Class<?> pluginClass = this.getPluginManager().getPlugins()
|
||||
|
@ -94,34 +144,6 @@ public class ScenarioEngine {
|
|||
pluginClass, initParameters);
|
||||
plugins.put(pluginId, plugin);
|
||||
}
|
||||
|
||||
List<BehaviorResult> ret = new ArrayList<BehaviorResult>();
|
||||
for (UserBehavior userBehavior : scenario.getUserBehaviors()) {
|
||||
Object plugin = plugins.get(userBehavior.getUse());
|
||||
String behaviorName = userBehavior.getName();
|
||||
Map<String, String> behaviorParameters = new HashMap<String, String>();
|
||||
for (Parameter parameter : userBehavior.getParameters()) {
|
||||
behaviorParameters
|
||||
.put(parameter.getKey(), parameter.getValue());
|
||||
}
|
||||
Date startDate = new Date(System.currentTimeMillis());
|
||||
boolean success = (Boolean) this.getPluginManager().doBehavior(
|
||||
plugin, behaviorName, behaviorParameters);
|
||||
Date endDate = new Date(System.currentTimeMillis());
|
||||
BehaviorResult result = new BehaviorResult();
|
||||
result.setId(UUID.randomUUID());
|
||||
result.setStartDate(startDate);
|
||||
result.setEndDate(endDate);
|
||||
result.setSuccess(success);
|
||||
result.setResponseTime(result.getEndDate().getTime()
|
||||
- result.getStartDate().getTime());
|
||||
result.setBehaviorName(behaviorName);
|
||||
result.setPluginId(userBehavior.getUse());
|
||||
result.setPluginName(plugin.getClass().getAnnotation(Plugin.class)
|
||||
.value());
|
||||
ret.add(result);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
|
|
Loading…
Reference in New Issue