test service added.

This commit is contained in:
Zhen Tang 2013-06-27 17:43:04 +08:00
parent 7344b4e3e1
commit d41cb8d32a
12 changed files with 211 additions and 104 deletions

View File

@ -1,11 +1,5 @@
package org.bench4q.agent.api;
import org.bench4q.agent.scenario.Parameter;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.ScenarioEngine;
import org.bench4q.agent.scenario.UsePlugin;
import org.bench4q.agent.scenario.UserBehavior;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -14,71 +8,11 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/")
public class HomeController {
private ScenarioEngine scenarioEngine;
private ScenarioEngine getScenarioEngine() {
return scenarioEngine;
}
@Autowired
private void setScenarioEngine(ScenarioEngine scenarioEngine) {
this.scenarioEngine = scenarioEngine;
}
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String index() {
Scenario scenario = new Scenario();
scenario.setUsePlugins(new UsePlugin[2]);
scenario.getUsePlugins()[0] = new UsePlugin();
scenario.getUsePlugins()[0].setId("http");
scenario.getUsePlugins()[0].setName("Http");
scenario.getUsePlugins()[0].setParameters(new Parameter[0]);
scenario.getUsePlugins()[1] = new UsePlugin();
scenario.getUsePlugins()[1].setId("timer");
scenario.getUsePlugins()[1].setName("ConstantTimer");
scenario.getUsePlugins()[1].setParameters(new Parameter[0]);
scenario.setUserBehaviors(new UserBehavior[3]);
scenario.getUserBehaviors()[0] = new UserBehavior();
scenario.getUserBehaviors()[0].setUse("http");
scenario.getUserBehaviors()[0].setName("Get");
scenario.getUserBehaviors()[0].setParameters(new Parameter[2]);
scenario.getUserBehaviors()[0].getParameters()[0] = new Parameter();
scenario.getUserBehaviors()[0].getParameters()[0].setKey("url");
scenario.getUserBehaviors()[0].getParameters()[0].setValue("localhost");
scenario.getUserBehaviors()[0].getParameters()[1] = new Parameter();
scenario.getUserBehaviors()[0].getParameters()[1].setKey("content");
scenario.getUserBehaviors()[0].getParameters()[1]
.setValue("Hello,world!");
scenario.getUserBehaviors()[1] = new UserBehavior();
scenario.getUserBehaviors()[1].setUse("http");
scenario.getUserBehaviors()[1].setName("Post");
scenario.getUserBehaviors()[1].setParameters(new Parameter[3]);
scenario.getUserBehaviors()[1].getParameters()[0] = new Parameter();
scenario.getUserBehaviors()[1].getParameters()[0].setKey("url");
scenario.getUserBehaviors()[1].getParameters()[0].setValue("localhost");
scenario.getUserBehaviors()[1].getParameters()[1] = new Parameter();
scenario.getUserBehaviors()[1].getParameters()[1].setKey("content");
scenario.getUserBehaviors()[1].getParameters()[1]
.setValue("Hello,world!");
scenario.getUserBehaviors()[1].getParameters()[2] = new Parameter();
scenario.getUserBehaviors()[1].getParameters()[2].setKey("code");
scenario.getUserBehaviors()[1].getParameters()[2].setValue("404");
scenario.getUserBehaviors()[2] = new UserBehavior();
scenario.getUserBehaviors()[2].setUse("timer");
scenario.getUserBehaviors()[2].setName("Sleep");
scenario.getUserBehaviors()[2].setParameters(new Parameter[1]);
scenario.getUserBehaviors()[2].getParameters()[0] = new Parameter();
scenario.getUserBehaviors()[2].getParameters()[0].setKey("time");
scenario.getUserBehaviors()[2].getParameters()[0].setValue("1000");
this.getScenarioEngine().runScenario(scenario, 100);
return "It works!";
}
}

View File

@ -6,9 +6,9 @@ import java.util.List;
import org.bench4q.agent.api.model.BehaviorInfoModel;
import org.bench4q.agent.api.model.PluginInfoListModel;
import org.bench4q.agent.api.model.PluginInfoModel;
import org.bench4q.agent.plugin.BehaviorInfo;
import org.bench4q.agent.plugin.PluginInfo;
import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.plugin.metadata.BehaviorInfo;
import org.bench4q.agent.plugin.metadata.PluginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

View File

@ -0,0 +1,87 @@
package org.bench4q.agent.api;
import java.util.List;
import org.bench4q.agent.plugin.BehaviorResult;
import org.bench4q.agent.scenario.Parameter;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.ScenarioEngine;
import org.bench4q.agent.scenario.UsePlugin;
import org.bench4q.agent.scenario.UserBehavior;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/test")
public class TestController {
private ScenarioEngine scenarioEngine;
private ScenarioEngine getScenarioEngine() {
return scenarioEngine;
}
@Autowired
private void setScenarioEngine(ScenarioEngine scenarioEngine) {
this.scenarioEngine = scenarioEngine;
}
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String run() {
Scenario scenario = new Scenario();
scenario.setUsePlugins(new UsePlugin[2]);
scenario.getUsePlugins()[0] = new UsePlugin();
scenario.getUsePlugins()[0].setId("http");
scenario.getUsePlugins()[0].setName("Http");
scenario.getUsePlugins()[0].setParameters(new Parameter[0]);
scenario.getUsePlugins()[1] = new UsePlugin();
scenario.getUsePlugins()[1].setId("timer");
scenario.getUsePlugins()[1].setName("ConstantTimer");
scenario.getUsePlugins()[1].setParameters(new Parameter[0]);
scenario.setUserBehaviors(new UserBehavior[3]);
scenario.getUserBehaviors()[0] = new UserBehavior();
scenario.getUserBehaviors()[0].setUse("http");
scenario.getUserBehaviors()[0].setName("Get");
scenario.getUserBehaviors()[0].setParameters(new Parameter[2]);
scenario.getUserBehaviors()[0].getParameters()[0] = new Parameter();
scenario.getUserBehaviors()[0].getParameters()[0].setKey("url");
scenario.getUserBehaviors()[0].getParameters()[0].setValue("localhost");
scenario.getUserBehaviors()[0].getParameters()[1] = new Parameter();
scenario.getUserBehaviors()[0].getParameters()[1].setKey("content");
scenario.getUserBehaviors()[0].getParameters()[1]
.setValue("Hello,world!");
scenario.getUserBehaviors()[1] = new UserBehavior();
scenario.getUserBehaviors()[1].setUse("http");
scenario.getUserBehaviors()[1].setName("Post");
scenario.getUserBehaviors()[1].setParameters(new Parameter[3]);
scenario.getUserBehaviors()[1].getParameters()[0] = new Parameter();
scenario.getUserBehaviors()[1].getParameters()[0].setKey("url");
scenario.getUserBehaviors()[1].getParameters()[0].setValue("localhost");
scenario.getUserBehaviors()[1].getParameters()[1] = new Parameter();
scenario.getUserBehaviors()[1].getParameters()[1].setKey("content");
scenario.getUserBehaviors()[1].getParameters()[1]
.setValue("Hello,world!");
scenario.getUserBehaviors()[1].getParameters()[2] = new Parameter();
scenario.getUserBehaviors()[1].getParameters()[2].setKey("code");
scenario.getUserBehaviors()[1].getParameters()[2].setValue("404");
scenario.getUserBehaviors()[2] = new UserBehavior();
scenario.getUserBehaviors()[2].setUse("timer");
scenario.getUserBehaviors()[2].setName("Sleep");
scenario.getUserBehaviors()[2].setParameters(new Parameter[1]);
scenario.getUserBehaviors()[2].getParameters()[0] = new Parameter();
scenario.getUserBehaviors()[2].getParameters()[0].setKey("time");
scenario.getUserBehaviors()[2].getParameters()[0].setValue("1000");
List<BehaviorResult> list = this.getScenarioEngine().runScenario(
scenario, 100);
return list.toString();
}
}

View File

@ -0,0 +1,34 @@
package org.bench4q.agent.plugin;
import java.util.Date;
public class BehaviorResult {
private Date startDate;
private Date endDate;
private boolean success;
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}

View File

@ -16,6 +16,10 @@ import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;
import org.bench4q.agent.plugin.annotation.Behavior;
import org.bench4q.agent.plugin.annotation.Plugin;
import org.bench4q.agent.plugin.metadata.BehaviorInfo;
import org.bench4q.agent.plugin.metadata.PluginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -29,7 +33,7 @@ public class PluginManager {
public PluginManager(ClassHelper classHelper, TypeConverter typeConverter) {
this.setClassHelper(classHelper);
this.setTypeConverter(typeConverter);
this.setPlugins(this.findPlugins("org.bench4q.agent.plugin"));
this.setPlugins(this.loadPlugins("org.bench4q.agent.plugin"));
}
private ClassHelper getClassHelper() {
@ -56,7 +60,7 @@ public class PluginManager {
this.plugins = plugins;
}
private Map<String, Class<?>> findPlugins(String packageName) {
public Map<String, Class<?>> loadPlugins(String packageName) {
try {
List<String> classNames = this.getClassHelper().getClassNames(
packageName, true);

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin;
package org.bench4q.agent.plugin.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin;
package org.bench4q.agent.plugin.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@ -1,7 +1,10 @@
package org.bench4q.agent.plugin.http;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.Plugin;
import java.util.Date;
import org.bench4q.agent.plugin.BehaviorResult;
import org.bench4q.agent.plugin.annotation.Behavior;
import org.bench4q.agent.plugin.annotation.Plugin;
@Plugin("Http")
public class HttpPlugin {
@ -10,17 +13,33 @@ public class HttpPlugin {
}
@Behavior("Get")
public void get(String url, String content) {
System.out.println("get");
System.out.println("url:" + url);
System.out.println("content:" + content);
public BehaviorResult get(String url, String content) {
BehaviorResult behaviorResult = new BehaviorResult();
behaviorResult.setStartDate(new Date(System.currentTimeMillis()));
try {
System.out.println("get");
System.out.println("url:" + url);
System.out.println("content:" + content);
behaviorResult.setEndDate(new Date(System.currentTimeMillis()));
behaviorResult.setSuccess(true);
} catch (Exception e) {
e.printStackTrace();
behaviorResult.setEndDate(new Date(System.currentTimeMillis()));
behaviorResult.setSuccess(true);
}
return behaviorResult;
}
@Behavior("Post")
public void post(String url, String content, int code) {
public BehaviorResult post(String url, String content, int code) {
BehaviorResult behaviorResult = new BehaviorResult();
behaviorResult.setStartDate(new Date(System.currentTimeMillis()));
System.out.println("get");
System.out.println("url:" + url);
System.out.println("content:" + content);
System.out.println("code:" + code);
behaviorResult.setEndDate(new Date(System.currentTimeMillis()));
behaviorResult.setSuccess(true);
return behaviorResult;
}
}

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin;
package org.bench4q.agent.plugin.metadata;
public class BehaviorInfo {
private String name;

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin;
package org.bench4q.agent.plugin.metadata;
public class PluginInfo {
private String name;

View File

@ -1,7 +1,10 @@
package org.bench4q.agent.plugin.timer;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.Plugin;
import java.util.Date;
import org.bench4q.agent.plugin.BehaviorResult;
import org.bench4q.agent.plugin.annotation.Behavior;
import org.bench4q.agent.plugin.annotation.Plugin;
@Plugin("ConstantTimer")
public class ConstantTimerPlugin {
@ -10,7 +13,19 @@ public class ConstantTimerPlugin {
}
@Behavior("Sleep")
public void sleep(int time) {
System.out.println("sleep:" + time);
public BehaviorResult sleep(int time) {
BehaviorResult behaviorResult = new BehaviorResult();
behaviorResult.setStartDate(new Date(System.currentTimeMillis()));
try {
System.out.println("sleep:" + time);
Thread.sleep(time);
behaviorResult.setEndDate(new Date(System.currentTimeMillis()));
behaviorResult.setSuccess(true);
} catch (Exception e) {
e.printStackTrace();
behaviorResult.setEndDate(new Date(System.currentTimeMillis()));
behaviorResult.setSuccess(false);
}
return behaviorResult;
}
}

View File

@ -1,10 +1,14 @@
package org.bench4q.agent.scenario;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.bench4q.agent.plugin.BehaviorResult;
import org.bench4q.agent.plugin.PluginManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -22,27 +26,35 @@ public class ScenarioEngine {
this.pluginManager = pluginManager;
}
public void runScenario(final Scenario scenario, int threadCount) {
ExecutorService executorService = Executors
.newFixedThreadPool(threadCount);
int i;
Runnable runnable = new Runnable() {
public void run() {
doRunScenario(scenario);
public List<BehaviorResult> runScenario(final Scenario scenario,
int threadCount) {
try {
ExecutorService executorService = Executors
.newFixedThreadPool(threadCount);
int i;
final List<BehaviorResult> ret = new ArrayList<BehaviorResult>();
Runnable runnable = new Runnable() {
public void run() {
ret.addAll(doRunScenario(scenario));
}
};
for (i = 0; i < threadCount; i++) {
executorService.execute(runnable);
}
};
for (i = 0; i < threadCount; i++) {
executorService.execute(runnable);
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
return ret;
} catch (Exception e) {
e.printStackTrace();
return null;
}
executorService.shutdown();
}
public void runScenario(Scenario scenario) {
this.doRunScenario(scenario);
public List<BehaviorResult> runScenario(Scenario scenario) {
return this.doRunScenario(scenario);
}
private void doRunScenario(Scenario scenario) {
System.out.println("Running");
private List<BehaviorResult> doRunScenario(Scenario scenario) {
ScenarioContext scenarioContext = new ScenarioContext();
scenarioContext.setPlugins(new HashMap<String, Object>());
for (UsePlugin usePlugin : scenario.getUsePlugins()) {
@ -57,6 +69,8 @@ public class ScenarioEngine {
pluginClass, initParameters);
scenarioContext.getPlugins().put(pluginId, plugin);
}
List<BehaviorResult> ret = new ArrayList<BehaviorResult>();
for (UserBehavior userBehavior : scenario.getUserBehaviors()) {
Object plugin = scenarioContext.getPlugins().get(
userBehavior.getUse());
@ -66,9 +80,9 @@ public class ScenarioEngine {
behaviorParameters
.put(parameter.getKey(), parameter.getValue());
}
this.getPluginManager().doBehavior(plugin, behaviorName,
behaviorParameters);
ret.add((BehaviorResult) this.getPluginManager().doBehavior(plugin,
behaviorName, behaviorParameters));
}
System.out.println("Finished");
return ret;
}
}