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.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.TestDetailModel;
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.scenario.Parameter;
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.stereotype.Controller;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@ -35,63 +41,66 @@ public class TestController {
this.scenarioEngine = scenarioEngine;
}
@RequestMapping(value = "/run", method = RequestMethod.GET)
@RequestMapping(value = "/run", method = RequestMethod.POST)
@ResponseBody
public UUID run() {
public RunScenarioResultModel run(
@RequestBody RunScenarioModel runScenarioModel) {
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[1]);
scenario.getUserBehaviors()[0].getParameters()[0] = new Parameter();
scenario.getUserBehaviors()[0].getParameters()[0].setKey("url");
scenario.getUserBehaviors()[0].getParameters()[0]
.setValue("http://localhost:6565");
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");
scenario.getUserBehaviors()[1] = new UserBehavior();
scenario.getUserBehaviors()[1].setUse("http");
scenario.getUserBehaviors()[1].setName("Post");
scenario.getUserBehaviors()[1].setParameters(new Parameter[2]);
scenario.getUserBehaviors()[1].getParameters()[0] = new Parameter();
scenario.getUserBehaviors()[1].getParameters()[0].setKey("url");
scenario.getUserBehaviors()[1].getParameters()[0]
.setValue("http://localhost:6565");
scenario.getUserBehaviors()[1].getParameters()[1] = new Parameter();
scenario.getUserBehaviors()[1].getParameters()[1].setKey("content");
scenario.getUserBehaviors()[1].getParameters()[1]
.setValue("Hello,world!");
UUID uuid = UUID.randomUUID();
this.getScenarioEngine().runScenario(uuid, scenario, 200);
return uuid;
scenario.setUsePlugins(new UsePlugin[runScenarioModel.getUsePlugins()
.size()]);
scenario.setUserBehaviors(new UserBehavior[runScenarioModel
.getUserBehaviors().size()]);
int i = 0;
int j = 0;
for (i = 0; i < runScenarioModel.getUsePlugins().size(); i++) {
UsePluginModel usePluginModel = runScenarioModel.getUsePlugins()
.get(i);
UsePlugin usePlugin = new UsePlugin();
usePlugin.setId(usePluginModel.getId());
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;
}
}
for (i = 0; i < runScenarioModel.getUserBehaviors().size(); i++) {
UserBehaviorModel userBehaviorModel = runScenarioModel
.getUserBehaviors().get(i);
UserBehavior userBehavior = new UserBehavior();
userBehavior.setName(userBehaviorModel.getName());
userBehavior.setUse(userBehaviorModel.getUse());
userBehavior.setParameters(new Parameter[userBehaviorModel
.getParameters().size()]);
scenario.getUserBehaviors()[i] = userBehavior;
for (j = 0; j < userBehaviorModel.getParameters().size(); j++) {
ParameterModel parameterModel = userBehaviorModel
.getParameters().get(j);
Parameter parameter = new Parameter();
parameter.setKey(parameterModel.getKey());
parameter.setValue(parameterModel.getValue());
scenario.getUserBehaviors()[i].getParameters()[j] = parameter;
}
}
@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
public TestDetailStatusModel detail(@PathVariable UUID uuid) {
public TestDetailStatusModel detail(@PathVariable UUID runId) {
ScenarioContext scenarioContext = this.getScenarioEngine()
.getRunningTests().get(uuid);
.getRunningTests().get(runId);
TestDetailStatusModel testStatusModel = new TestDetailStatusModel();
testStatusModel.setStartDate(scenarioContext.getStartDate());
testStatusModel.setTestDetailModels(new ArrayList<TestDetailModel>());
@ -134,11 +143,11 @@ public class TestController {
return testStatusModel;
}
@RequestMapping(value = "/brief/{uuid}", method = RequestMethod.GET)
@RequestMapping(value = "/brief/{runId}", method = RequestMethod.GET)
@ResponseBody
public TestBriefStatusModel brief(@PathVariable UUID uuid) {
public TestBriefStatusModel brief(@PathVariable UUID runId) {
ScenarioContext scenarioContext = this.getScenarioEngine()
.getRunningTests().get(uuid);
.getRunningTests().get(runId);
TestBriefStatusModel testBriefStatusModel = new TestBriefStatusModel();
testBriefStatusModel.setStartDate(scenarioContext.getStartDate());
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;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "parameter")
public class Parameter {
private String key;
private String value;
@XmlElement
public String getKey() {
return key;
}
@ -17,7 +12,6 @@ public class Parameter {
this.key = key;
}
@XmlElement
public String getValue() {
return value;
}

View File

@ -1,16 +1,9 @@
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 {
private UsePlugin[] usePlugins;
private UserBehavior[] userBehaviors;
@XmlElementWrapper(name = "plugins")
@XmlElement(name = "plugin")
public UsePlugin[] getUsePlugins() {
return usePlugins;
}
@ -19,8 +12,6 @@ public class Scenario {
this.usePlugins = usePlugins;
}
@XmlElementWrapper(name = "userBehaviors")
@XmlElement(name = "userBehavior")
public UserBehavior[] getUserBehaviors() {
return userBehaviors;
}

View File

@ -46,7 +46,7 @@ public class ScenarioEngine {
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 {
ScenarioContext scenarioContext = new ScenarioContext();
scenarioContext.setScenario(scenario);
@ -60,7 +60,7 @@ public class ScenarioEngine {
final List<BehaviorResult> ret = Collections
.synchronizedList(new ArrayList<BehaviorResult>());
scenarioContext.setResults(ret);
this.getRunningTests().put(uuid, scenarioContext);
this.getRunningTests().put(runId, scenarioContext);
Runnable runnable = new Runnable() {
public void run() {
ret.addAll(doRunScenario(scenario));

View File

@ -1,16 +1,10 @@
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 {
private String id;
private String name;
private Parameter[] parameters;
@XmlElement
public String getId() {
return id;
}
@ -19,7 +13,6 @@ public class UsePlugin {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
@ -28,8 +21,6 @@ public class UsePlugin {
this.name = name;
}
@XmlElementWrapper(name = "parameters")
@XmlElement(name = "parameter")
public Parameter[] getParameters() {
return parameters;
}

View File

@ -1,16 +1,10 @@
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 {
private String use;
private String name;
private Parameter[] parameters;
@XmlElement
public String getUse() {
return use;
}
@ -19,7 +13,6 @@ public class UserBehavior {
this.use = use;
}
@XmlElement
public String getName() {
return name;
}
@ -28,8 +21,6 @@ public class UserBehavior {
this.name = name;
}
@XmlElementWrapper(name = "parameters")
@XmlElement(name = "parameter")
public Parameter[] getParameters() {
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();
}
}
}