Run scenario service added.

This commit is contained in:
Zhen Tang 2013-06-28 17:59:38 +08:00
parent 92fb5fb76f
commit cc8bc43f27
12 changed files with 349 additions and 88 deletions

View File

@ -4,9 +4,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.bench4q.agent.api.model.ParameterModel;
import org.bench4q.agent.api.model.RunScenarioModel;
import org.bench4q.agent.api.model.RunScenarioResultModel;
import org.bench4q.agent.api.model.TestBriefStatusModel; import org.bench4q.agent.api.model.TestBriefStatusModel;
import org.bench4q.agent.api.model.TestDetailModel; import org.bench4q.agent.api.model.TestDetailModel;
import org.bench4q.agent.api.model.TestDetailStatusModel; import org.bench4q.agent.api.model.TestDetailStatusModel;
import org.bench4q.agent.api.model.UsePluginModel;
import org.bench4q.agent.api.model.UserBehaviorModel;
import org.bench4q.agent.plugin.BehaviorResult; import org.bench4q.agent.plugin.BehaviorResult;
import org.bench4q.agent.scenario.Parameter; import org.bench4q.agent.scenario.Parameter;
import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.Scenario;
@ -17,6 +22,7 @@ import org.bench4q.agent.scenario.UserBehavior;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@ -35,63 +41,66 @@ public class TestController {
this.scenarioEngine = scenarioEngine; this.scenarioEngine = scenarioEngine;
} }
@RequestMapping(value = "/run", method = RequestMethod.GET) @RequestMapping(value = "/run", method = RequestMethod.POST)
@ResponseBody @ResponseBody
public UUID run() { public RunScenarioResultModel run(
@RequestBody RunScenarioModel runScenarioModel) {
Scenario scenario = new Scenario(); Scenario scenario = new Scenario();
scenario.setUsePlugins(new UsePlugin[runScenarioModel.getUsePlugins()
scenario.setUsePlugins(new UsePlugin[2]); .size()]);
scenario.getUsePlugins()[0] = new UsePlugin(); scenario.setUserBehaviors(new UserBehavior[runScenarioModel
scenario.getUsePlugins()[0].setId("http"); .getUserBehaviors().size()]);
scenario.getUsePlugins()[0].setName("Http"); int i = 0;
scenario.getUsePlugins()[0].setParameters(new Parameter[0]); int j = 0;
for (i = 0; i < runScenarioModel.getUsePlugins().size(); i++) {
scenario.getUsePlugins()[1] = new UsePlugin(); UsePluginModel usePluginModel = runScenarioModel.getUsePlugins()
scenario.getUsePlugins()[1].setId("timer"); .get(i);
scenario.getUsePlugins()[1].setName("ConstantTimer"); UsePlugin usePlugin = new UsePlugin();
scenario.getUsePlugins()[1].setParameters(new Parameter[0]); usePlugin.setId(usePluginModel.getId());
usePlugin.setName(usePluginModel.getName());
scenario.setUserBehaviors(new UserBehavior[3]); usePlugin.setParameters(new Parameter[usePluginModel
scenario.getUserBehaviors()[0] = new UserBehavior(); .getParameters().size()]);
scenario.getUserBehaviors()[0].setUse("http"); scenario.getUsePlugins()[i] = usePlugin;
scenario.getUserBehaviors()[0].setName("Get"); for (j = 0; j < usePluginModel.getParameters().size(); j++) {
scenario.getUserBehaviors()[0].setParameters(new Parameter[1]); ParameterModel parameterModel = usePluginModel.getParameters()
scenario.getUserBehaviors()[0].getParameters()[0] = new Parameter(); .get(j);
scenario.getUserBehaviors()[0].getParameters()[0].setKey("url"); Parameter parameter = new Parameter();
scenario.getUserBehaviors()[0].getParameters()[0] parameter.setKey(parameterModel.getKey());
.setValue("http://localhost:6565"); parameter.setValue(parameterModel.getValue());
scenario.getUsePlugins()[i].getParameters()[j] = parameter;
scenario.getUserBehaviors()[2] = new UserBehavior(); }
scenario.getUserBehaviors()[2].setUse("timer"); }
scenario.getUserBehaviors()[2].setName("Sleep"); for (i = 0; i < runScenarioModel.getUserBehaviors().size(); i++) {
scenario.getUserBehaviors()[2].setParameters(new Parameter[1]); UserBehaviorModel userBehaviorModel = runScenarioModel
scenario.getUserBehaviors()[2].getParameters()[0] = new Parameter(); .getUserBehaviors().get(i);
scenario.getUserBehaviors()[2].getParameters()[0].setKey("time"); UserBehavior userBehavior = new UserBehavior();
scenario.getUserBehaviors()[2].getParameters()[0].setValue("1000"); userBehavior.setName(userBehaviorModel.getName());
userBehavior.setUse(userBehaviorModel.getUse());
scenario.getUserBehaviors()[1] = new UserBehavior(); userBehavior.setParameters(new Parameter[userBehaviorModel
scenario.getUserBehaviors()[1].setUse("http"); .getParameters().size()]);
scenario.getUserBehaviors()[1].setName("Post"); scenario.getUserBehaviors()[i] = userBehavior;
scenario.getUserBehaviors()[1].setParameters(new Parameter[2]); for (j = 0; j < userBehaviorModel.getParameters().size(); j++) {
scenario.getUserBehaviors()[1].getParameters()[0] = new Parameter(); ParameterModel parameterModel = userBehaviorModel
scenario.getUserBehaviors()[1].getParameters()[0].setKey("url"); .getParameters().get(j);
scenario.getUserBehaviors()[1].getParameters()[0] Parameter parameter = new Parameter();
.setValue("http://localhost:6565"); parameter.setKey(parameterModel.getKey());
scenario.getUserBehaviors()[1].getParameters()[1] = new Parameter(); parameter.setValue(parameterModel.getValue());
scenario.getUserBehaviors()[1].getParameters()[1].setKey("content"); scenario.getUserBehaviors()[i].getParameters()[j] = parameter;
scenario.getUserBehaviors()[1].getParameters()[1] }
.setValue("Hello,world!");
UUID uuid = UUID.randomUUID();
this.getScenarioEngine().runScenario(uuid, scenario, 200);
return uuid;
} }
@RequestMapping(value = "/detail/{uuid}", method = RequestMethod.GET) RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel();
runScenarioResultModel.setRunId(UUID.randomUUID());
this.getScenarioEngine().runScenario(runScenarioResultModel.getRunId(),
scenario, runScenarioModel.getTotalCount());
return runScenarioResultModel;
}
@RequestMapping(value = "/detail/{runId}", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public TestDetailStatusModel detail(@PathVariable UUID uuid) { public TestDetailStatusModel detail(@PathVariable UUID runId) {
ScenarioContext scenarioContext = this.getScenarioEngine() ScenarioContext scenarioContext = this.getScenarioEngine()
.getRunningTests().get(uuid); .getRunningTests().get(runId);
TestDetailStatusModel testStatusModel = new TestDetailStatusModel(); TestDetailStatusModel testStatusModel = new TestDetailStatusModel();
testStatusModel.setStartDate(scenarioContext.getStartDate()); testStatusModel.setStartDate(scenarioContext.getStartDate());
testStatusModel.setTestDetailModels(new ArrayList<TestDetailModel>()); testStatusModel.setTestDetailModels(new ArrayList<TestDetailModel>());
@ -134,11 +143,11 @@ public class TestController {
return testStatusModel; return testStatusModel;
} }
@RequestMapping(value = "/brief/{uuid}", method = RequestMethod.GET) @RequestMapping(value = "/brief/{runId}", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public TestBriefStatusModel brief(@PathVariable UUID uuid) { public TestBriefStatusModel brief(@PathVariable UUID runId) {
ScenarioContext scenarioContext = this.getScenarioEngine() ScenarioContext scenarioContext = this.getScenarioEngine()
.getRunningTests().get(uuid); .getRunningTests().get(runId);
TestBriefStatusModel testBriefStatusModel = new TestBriefStatusModel(); TestBriefStatusModel testBriefStatusModel = new TestBriefStatusModel();
testBriefStatusModel.setStartDate(scenarioContext.getStartDate()); testBriefStatusModel.setStartDate(scenarioContext.getStartDate());
int failCount = 0; int failCount = 0;

View File

@ -0,0 +1,29 @@
package org.bench4q.agent.api.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "parameter")
public class ParameterModel {
private String key;
private String value;
@XmlElement
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@XmlElement
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,43 @@
package org.bench4q.agent.api.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "runScenario")
public class RunScenarioModel {
private int totalCount;
private List<UsePluginModel> usePlugins;
private List<UserBehaviorModel> userBehaviors;
@XmlElement
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
@XmlElementWrapper(name = "usePlugins")
@XmlElement(name = "usePlugin")
public List<UsePluginModel> getUsePlugins() {
return usePlugins;
}
public void setUsePlugins(List<UsePluginModel> usePlugins) {
this.usePlugins = usePlugins;
}
@XmlElementWrapper(name = "userBehaviors")
@XmlElement(name = "userBehavior")
public List<UserBehaviorModel> getUserBehaviors() {
return userBehaviors;
}
public void setUserBehaviors(List<UserBehaviorModel> userBehaviors) {
this.userBehaviors = userBehaviors;
}
}

View File

@ -0,0 +1,21 @@
package org.bench4q.agent.api.model;
import java.util.UUID;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "runScenarioResult")
public class RunScenarioResultModel {
private UUID runId;
@XmlElement
public UUID getRunId() {
return runId;
}
public void setRunId(UUID runId) {
this.runId = runId;
}
}

View File

@ -0,0 +1,43 @@
package org.bench4q.agent.api.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "usePlugin")
public class UsePluginModel {
private String id;
private String name;
private List<ParameterModel> parameters;
@XmlElement
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElementWrapper(name = "parameters")
@XmlElement(name = "parameter")
public List<ParameterModel> getParameters() {
return parameters;
}
public void setParameters(List<ParameterModel> parameters) {
this.parameters = parameters;
}
}

View File

@ -0,0 +1,42 @@
package org.bench4q.agent.api.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "userBehavior")
public class UserBehaviorModel {
private String use;
private String name;
private List<ParameterModel> parameters;
@XmlElement
public String getUse() {
return use;
}
public void setUse(String use) {
this.use = use;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElementWrapper(name = "parameters")
@XmlElement(name = "parameter")
public List<ParameterModel> getParameters() {
return parameters;
}
public void setParameters(List<ParameterModel> parameters) {
this.parameters = parameters;
}
}

View File

@ -1,14 +1,9 @@
package org.bench4q.agent.scenario; package org.bench4q.agent.scenario;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "parameter")
public class Parameter { public class Parameter {
private String key; private String key;
private String value; private String value;
@XmlElement
public String getKey() { public String getKey() {
return key; return key;
} }
@ -17,7 +12,6 @@ public class Parameter {
this.key = key; this.key = key;
} }
@XmlElement
public String getValue() { public String getValue() {
return value; return value;
} }

View File

@ -1,16 +1,9 @@
package org.bench4q.agent.scenario; package org.bench4q.agent.scenario;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "scenario")
public class Scenario { public class Scenario {
private UsePlugin[] usePlugins; private UsePlugin[] usePlugins;
private UserBehavior[] userBehaviors; private UserBehavior[] userBehaviors;
@XmlElementWrapper(name = "plugins")
@XmlElement(name = "plugin")
public UsePlugin[] getUsePlugins() { public UsePlugin[] getUsePlugins() {
return usePlugins; return usePlugins;
} }
@ -19,8 +12,6 @@ public class Scenario {
this.usePlugins = usePlugins; this.usePlugins = usePlugins;
} }
@XmlElementWrapper(name = "userBehaviors")
@XmlElement(name = "userBehavior")
public UserBehavior[] getUserBehaviors() { public UserBehavior[] getUserBehaviors() {
return userBehaviors; return userBehaviors;
} }

View File

@ -46,7 +46,7 @@ public class ScenarioEngine {
return this.getRunningTests().get(uuid); return this.getRunningTests().get(uuid);
} }
public void runScenario(UUID uuid, final Scenario scenario, int totalCount) { public void runScenario(UUID runId, final Scenario scenario, int totalCount) {
try { try {
ScenarioContext scenarioContext = new ScenarioContext(); ScenarioContext scenarioContext = new ScenarioContext();
scenarioContext.setScenario(scenario); scenarioContext.setScenario(scenario);
@ -60,7 +60,7 @@ public class ScenarioEngine {
final List<BehaviorResult> ret = Collections final List<BehaviorResult> ret = Collections
.synchronizedList(new ArrayList<BehaviorResult>()); .synchronizedList(new ArrayList<BehaviorResult>());
scenarioContext.setResults(ret); scenarioContext.setResults(ret);
this.getRunningTests().put(uuid, scenarioContext); this.getRunningTests().put(runId, scenarioContext);
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
public void run() { public void run() {
ret.addAll(doRunScenario(scenario)); ret.addAll(doRunScenario(scenario));

View File

@ -1,16 +1,10 @@
package org.bench4q.agent.scenario; package org.bench4q.agent.scenario;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "usePlugin")
public class UsePlugin { public class UsePlugin {
private String id; private String id;
private String name; private String name;
private Parameter[] parameters; private Parameter[] parameters;
@XmlElement
public String getId() { public String getId() {
return id; return id;
} }
@ -19,7 +13,6 @@ public class UsePlugin {
this.id = id; this.id = id;
} }
@XmlElement
public String getName() { public String getName() {
return name; return name;
} }
@ -28,8 +21,6 @@ public class UsePlugin {
this.name = name; this.name = name;
} }
@XmlElementWrapper(name = "parameters")
@XmlElement(name = "parameter")
public Parameter[] getParameters() { public Parameter[] getParameters() {
return parameters; return parameters;
} }

View File

@ -1,16 +1,10 @@
package org.bench4q.agent.scenario; package org.bench4q.agent.scenario;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "userBehavior")
public class UserBehavior { public class UserBehavior {
private String use; private String use;
private String name; private String name;
private Parameter[] parameters; private Parameter[] parameters;
@XmlElement
public String getUse() { public String getUse() {
return use; return use;
} }
@ -19,7 +13,6 @@ public class UserBehavior {
this.use = use; this.use = use;
} }
@XmlElement
public String getName() { public String getName() {
return name; return name;
} }
@ -28,8 +21,6 @@ public class UserBehavior {
this.name = name; this.name = name;
} }
@XmlElementWrapper(name = "parameters")
@XmlElement(name = "parameter")
public Parameter[] getParameters() { public Parameter[] getParameters() {
return parameters; return parameters;
} }

View File

@ -0,0 +1,107 @@
package org.bench4q.agent.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import org.bench4q.agent.api.model.ParameterModel;
import org.bench4q.agent.api.model.RunScenarioModel;
import org.bench4q.agent.api.model.UsePluginModel;
import org.bench4q.agent.api.model.UserBehaviorModel;
import org.junit.Test;
public class RunScenarioTest {
@Test
public void testRunScenario() {
try {
RunScenarioModel runScenarioModel = new RunScenarioModel();
runScenarioModel.setTotalCount(300);
runScenarioModel.setUsePlugins(new ArrayList<UsePluginModel>());
runScenarioModel
.setUserBehaviors(new ArrayList<UserBehaviorModel>());
UsePluginModel httpUsePluginModel = new UsePluginModel();
httpUsePluginModel.setId("http");
httpUsePluginModel.setName("Http");
httpUsePluginModel.setParameters(new ArrayList<ParameterModel>());
UsePluginModel timerUsePluginModel = new UsePluginModel();
timerUsePluginModel.setId("timer");
timerUsePluginModel.setName("ConstantTimer");
timerUsePluginModel.setParameters(new ArrayList<ParameterModel>());
runScenarioModel.getUsePlugins().add(httpUsePluginModel);
runScenarioModel.getUsePlugins().add(timerUsePluginModel);
UserBehaviorModel getUserBehaviorModel = new UserBehaviorModel();
getUserBehaviorModel.setName("Get");
getUserBehaviorModel.setUse("http");
getUserBehaviorModel.setParameters(new ArrayList<ParameterModel>());
ParameterModel parameterModelOne = new ParameterModel();
parameterModelOne.setKey("url");
parameterModelOne.setValue("http://localhost:6565");
getUserBehaviorModel.getParameters().add(parameterModelOne);
runScenarioModel.getUserBehaviors().add(getUserBehaviorModel);
UserBehaviorModel timerUserBehaviorModel = new UserBehaviorModel();
timerUserBehaviorModel.setName("Sleep");
timerUserBehaviorModel.setUse("timer");
timerUserBehaviorModel
.setParameters(new ArrayList<ParameterModel>());
ParameterModel parameterModelTwo = new ParameterModel();
parameterModelTwo.setKey("time");
parameterModelTwo.setValue("1000");
timerUserBehaviorModel.getParameters().add(parameterModelTwo);
runScenarioModel.getUserBehaviors().add(timerUserBehaviorModel);
UserBehaviorModel postUserBehaviorModel = new UserBehaviorModel();
postUserBehaviorModel.setName("Post");
postUserBehaviorModel.setUse("http");
postUserBehaviorModel
.setParameters(new ArrayList<ParameterModel>());
ParameterModel parameterModelThree = new ParameterModel();
parameterModelThree.setKey("url");
parameterModelThree.setValue("http://localhost:6565");
postUserBehaviorModel.getParameters().add(parameterModelThree);
ParameterModel parameterModelFour = new ParameterModel();
parameterModelFour.setKey("content");
parameterModelFour.setValue("Hello,world!");
postUserBehaviorModel.getParameters().add(parameterModelFour);
runScenarioModel.getUserBehaviors().add(postUserBehaviorModel);
Marshaller marshaller = JAXBContext.newInstance(
runScenarioModel.getClass()).createMarshaller();
;
StringWriter stringWriter = new StringWriter();
marshaller.marshal(runScenarioModel, stringWriter);
String url = "http://localhost:6565/test/run";
String content = stringWriter.toString();
System.out.println(content);
URL target = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) target
.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/xml");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
httpURLConnection.getOutputStream());
outputStreamWriter.write(content);
outputStreamWriter.flush();
outputStreamWriter.close();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream()));
int temp = -1;
StringBuffer stringBuffer = new StringBuffer();
while ((temp = bufferedReader.read()) != -1) {
stringBuffer.append((char) temp);
}
bufferedReader.close();
System.out.println(stringBuffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}