Merge code
This commit is contained in:
commit
b2b754c429
|
@ -1,4 +1,4 @@
|
||||||
Bench4Q-Agent
|
Bench4Q-Agent
|
||||||
=============
|
=============
|
||||||
|
|
||||||
Agent Node of Bench4Q
|
Agent Node of Bench4Q
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package org.bench4q.agent;
|
package org.bench4q.agent;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AgentServer agentServer = new AgentServer(6565);
|
AgentServer agentServer = new AgentServer(6565);
|
||||||
agentServer.start();
|
agentServer.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,8 +79,8 @@ public class HomeController {
|
||||||
scenario.getUserBehaviors()[2].getParameters()[0] = new Parameter();
|
scenario.getUserBehaviors()[2].getParameters()[0] = new Parameter();
|
||||||
scenario.getUserBehaviors()[2].getParameters()[0].setKey("time");
|
scenario.getUserBehaviors()[2].getParameters()[0].setKey("time");
|
||||||
scenario.getUserBehaviors()[2].getParameters()[0].setValue("1000");
|
scenario.getUserBehaviors()[2].getParameters()[0].setValue("1000");
|
||||||
|
|
||||||
this.getScenarioEngine().runScenario(scenario, 100);
|
this.getScenarioEngine().runScenario(scenario, 100);
|
||||||
return "It works!";
|
return "It works!";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package org.bench4q.agent.plugin;
|
||||||
|
|
||||||
|
public class BehaviorInfo {
|
||||||
|
private String name;
|
||||||
|
private String[] parameters;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getParameters() {
|
||||||
|
return parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParameters(String[] parameters) {
|
||||||
|
this.parameters = parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package org.bench4q.agent.plugin;
|
||||||
|
|
||||||
|
public class PluginInfo {
|
||||||
|
private String name;
|
||||||
|
private String parameters[];
|
||||||
|
private BehaviorInfo[] behaviors;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getParameters() {
|
||||||
|
return parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParameters(String[] parameters) {
|
||||||
|
this.parameters = parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BehaviorInfo[] getBehaviors() {
|
||||||
|
return behaviors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBehaviors(BehaviorInfo[] behaviors) {
|
||||||
|
this.behaviors = behaviors;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package org.bench4q.agent.plugin;
|
package org.bench4q.agent.plugin;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -11,7 +12,6 @@ import javassist.CtClass;
|
||||||
import javassist.CtConstructor;
|
import javassist.CtConstructor;
|
||||||
import javassist.CtMethod;
|
import javassist.CtMethod;
|
||||||
import javassist.Modifier;
|
import javassist.Modifier;
|
||||||
import javassist.NotFoundException;
|
|
||||||
import javassist.bytecode.CodeAttribute;
|
import javassist.bytecode.CodeAttribute;
|
||||||
import javassist.bytecode.LocalVariableAttribute;
|
import javassist.bytecode.LocalVariableAttribute;
|
||||||
import javassist.bytecode.MethodInfo;
|
import javassist.bytecode.MethodInfo;
|
||||||
|
@ -74,6 +74,77 @@ public class PluginManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<PluginInfo> getPluginInfo() {
|
||||||
|
try {
|
||||||
|
Map<String, Class<?>> plugins = this.getPlugins();
|
||||||
|
List<PluginInfo> ret = new ArrayList<PluginInfo>();
|
||||||
|
for (Class<?> plugin : plugins.values()) {
|
||||||
|
PluginInfo pluginInfo = new PluginInfo();
|
||||||
|
ClassPool classPool = ClassPool.getDefault();
|
||||||
|
CtClass ctClass = classPool.get(plugin.getCanonicalName());
|
||||||
|
pluginInfo.setName(((Plugin) ctClass
|
||||||
|
.getAnnotation(Plugin.class)).value());
|
||||||
|
pluginInfo.setParameters(this.getParameterNames(ctClass
|
||||||
|
.getConstructors()[0]));
|
||||||
|
CtMethod[] behaviors = this.getBehaviors(ctClass);
|
||||||
|
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
|
||||||
|
.getParameterNames(behaviorMethod));
|
||||||
|
pluginInfo.getBehaviors()[i] = behaviorInfo;
|
||||||
|
}
|
||||||
|
ret.add(pluginInfo);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String[] getParameterNames(CtBehavior behavior) {
|
||||||
|
try {
|
||||||
|
MethodInfo methodInfo = behavior.getMethodInfo();
|
||||||
|
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
|
||||||
|
LocalVariableAttribute localVariableAttribute = (LocalVariableAttribute) codeAttribute
|
||||||
|
.getAttribute(LocalVariableAttribute.tag);
|
||||||
|
int parameterCount = behavior.getParameterTypes().length;
|
||||||
|
String parameterNames[] = new String[parameterCount];
|
||||||
|
int i;
|
||||||
|
int pos = Modifier.isStatic(behavior.getModifiers()) ? 0 : 1;
|
||||||
|
for (i = 0; i < parameterCount; i++) {
|
||||||
|
parameterNames[i] = localVariableAttribute
|
||||||
|
.variableName(i + pos);
|
||||||
|
}
|
||||||
|
return parameterNames;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private CtMethod[] getBehaviors(CtClass plugin) {
|
||||||
|
try {
|
||||||
|
CtMethod[] ctMethods = plugin.getMethods();
|
||||||
|
List<CtMethod> ret = new ArrayList<CtMethod>();
|
||||||
|
int i = 0;
|
||||||
|
for (i = 0; i < ctMethods.length; i++) {
|
||||||
|
if (ctMethods[i].hasAnnotation(Behavior.class)) {
|
||||||
|
ret.add(ctMethods[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret.toArray(new CtMethod[0]);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Object initializePlugin(Class<?> plugin,
|
public Object initializePlugin(Class<?> plugin,
|
||||||
Map<String, String> parameters) {
|
Map<String, String> parameters) {
|
||||||
try {
|
try {
|
||||||
|
@ -92,24 +163,22 @@ public class PluginManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object[] prepareParameters(CtBehavior constructor,
|
private Object[] prepareParameters(CtBehavior behavior,
|
||||||
Map<String, String> parameters) throws NotFoundException {
|
Map<String, String> parameters) {
|
||||||
MethodInfo methodInfo = constructor.getMethodInfo();
|
try {
|
||||||
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
|
String[] parameterNames = this.getParameterNames(behavior);
|
||||||
LocalVariableAttribute localVariableAttribute = (LocalVariableAttribute) codeAttribute
|
Object values[] = new Object[parameterNames.length];
|
||||||
.getAttribute(LocalVariableAttribute.tag);
|
int i = 0;
|
||||||
int parameterCount = constructor.getParameterTypes().length;
|
for (i = 0; i < parameterNames.length; i++) {
|
||||||
String parameterNames[] = new String[parameterCount];
|
values[i] = this.getTypeConverter().convert(
|
||||||
Object values[] = new Object[parameterCount];
|
parameters.get(parameterNames[i]),
|
||||||
int i;
|
behavior.getParameterTypes()[i].getName());
|
||||||
int pos = Modifier.isStatic(constructor.getModifiers()) ? 0 : 1;
|
}
|
||||||
for (i = 0; i < parameterCount; i++) {
|
return values;
|
||||||
parameterNames[i] = localVariableAttribute.variableName(i + pos);
|
} catch (Exception e) {
|
||||||
values[i] = this.getTypeConverter().convert(
|
e.printStackTrace();
|
||||||
parameters.get(parameterNames[i]),
|
return null;
|
||||||
constructor.getParameterTypes()[i].getName());
|
|
||||||
}
|
}
|
||||||
return values;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object doBehavior(Object plugin, String behaviorName,
|
public Object doBehavior(Object plugin, String behaviorName,
|
||||||
|
|
|
@ -6,16 +6,21 @@ import org.bench4q.agent.plugin.Plugin;
|
||||||
@Plugin("Http")
|
@Plugin("Http")
|
||||||
public class HttpPlugin {
|
public class HttpPlugin {
|
||||||
public HttpPlugin() {
|
public HttpPlugin() {
|
||||||
|
System.out.println("init http plugin");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Behavior("Get")
|
@Behavior("Get")
|
||||||
public void get(String url, String content) {
|
public void get(String url, String content) {
|
||||||
|
System.out.println("get");
|
||||||
|
System.out.println("url:" + url);
|
||||||
|
System.out.println("content:" + content);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Behavior("Post")
|
@Behavior("Post")
|
||||||
public void post(String url, String content, int code) {
|
public void post(String url, String content, int code) {
|
||||||
|
System.out.println("get");
|
||||||
|
System.out.println("url:" + url);
|
||||||
|
System.out.println("content:" + content);
|
||||||
|
System.out.println("code:" + code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,11 @@ import org.bench4q.agent.plugin.Plugin;
|
||||||
@Plugin("ConstantTimer")
|
@Plugin("ConstantTimer")
|
||||||
public class ConstantTimerPlugin {
|
public class ConstantTimerPlugin {
|
||||||
public ConstantTimerPlugin() {
|
public ConstantTimerPlugin() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Behavior("Sleep")
|
@Behavior("Sleep")
|
||||||
public void sleep(int time) {
|
public void sleep(int time) {
|
||||||
|
System.out.println("sleep:" + time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
|
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
|
||||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||||
<context:component-scan base-package="org.bench4q" />
|
<context:component-scan base-package="org.bench4q" />
|
||||||
<mvc:annotation-driven />
|
<mvc:annotation-driven />
|
||||||
</beans>
|
</beans>
|
||||||
|
|
Loading…
Reference in New Issue