now agent can run scenario with a batch

This commit is contained in:
coderfengyun 2013-12-10 20:56:48 +08:00
parent 839e157412
commit b4dc21b576
8 changed files with 65 additions and 17 deletions

41
Scripts/forBatch.xml Normal file
View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<runScenario>
<batches>
<batch>
<behaviors>
<userBehavior>
<id>1</id>
<name>Get</name>
<parameters>
<parameter>
<key>url</key>
<value>http://localhost:8080/Bench4QTestCase/testcase.html
</value>
</parameter>
<parameter>
<key>parameters</key>
<value></value>
</parameter>
</parameters>
<use>http</use>
</userBehavior>
</behaviors>
<childId>-1</childId>
<id>0</id>
<parentId>-1</parentId>
</batch>
</batches>
<poolSize>0</poolSize>
<usePlugins>
<usePlugin>
<id>http</id>
<name>Http</name>
<parameters />
</usePlugin>
<usePlugin>
<id>timer</id>
<name>ConstantTimer</name>
<parameters />
</usePlugin>
</usePlugins>
</runScenario>

View File

@ -25,9 +25,9 @@ import org.bench4q.agent.api.model.RunScenarioResultModel;
import org.bench4q.agent.api.model.StopTestModel; import org.bench4q.agent.api.model.StopTestModel;
import org.bench4q.agent.api.model.TestBehaviorsBriefModel; import org.bench4q.agent.api.model.TestBehaviorsBriefModel;
import org.bench4q.agent.api.model.UsePluginModel; import org.bench4q.agent.api.model.UsePluginModel;
import org.bench4q.agent.api.model.behavior.Batch;
import org.bench4q.agent.api.model.behavior.BehaviorBaseModel; import org.bench4q.agent.api.model.behavior.BehaviorBaseModel;
import org.bench4q.agent.datacollector.impl.DetailStatusCodeResult; import org.bench4q.agent.datacollector.impl.DetailStatusCodeResult;
import org.bench4q.agent.scenario.Batch;
import org.bench4q.agent.scenario.Parameter; import org.bench4q.agent.scenario.Parameter;
import org.bench4q.agent.scenario.ScenarioNew; import org.bench4q.agent.scenario.ScenarioNew;
import org.bench4q.agent.scenario.ScenarioContext; import org.bench4q.agent.scenario.ScenarioContext;

View File

@ -4,9 +4,12 @@ import java.util.List;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.bench4q.agent.api.model.behavior.BehaviorBaseModel; import org.bench4q.agent.api.model.behavior.BehaviorBaseModel;
import org.bench4q.agent.api.model.behavior.TimerBehaviorModel;
import org.bench4q.agent.api.model.behavior.UserBehaviorModel;
@XmlRootElement(name = "batch") @XmlRootElement(name = "batch")
public class BatchBehavior { public class BatchBehavior {
@ -43,7 +46,9 @@ public class BatchBehavior {
} }
@XmlElementWrapper(name = "behaviors") @XmlElementWrapper(name = "behaviors")
@XmlElement @XmlElements(value = {
@XmlElement(name = "userBehavior", type = UserBehaviorModel.class),
@XmlElement(name = "timerBehavior", type = TimerBehaviorModel.class) })
public List<BehaviorBaseModel> getBehaviors() { public List<BehaviorBaseModel> getBehaviors() {
return behaviors; return behaviors;
} }

View File

@ -9,7 +9,6 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "runScenario") @XmlRootElement(name = "runScenario")
public class RunScenarioModelNew { public class RunScenarioModelNew {
private int poolSize; private int poolSize;
private List<UsePluginModel> usePlugins; private List<UsePluginModel> usePlugins;
private List<BatchBehavior> batches; private List<BatchBehavior> batches;
@ -34,7 +33,7 @@ public class RunScenarioModelNew {
} }
@XmlElementWrapper(name = "batches") @XmlElementWrapper(name = "batches")
@XmlElement @XmlElement(name = "batch")
public List<BatchBehavior> getBatches() { public List<BatchBehavior> getBatches() {
return batches; return batches;
} }

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.api.model.behavior; package org.bench4q.agent.scenario;
import org.bench4q.agent.scenario.behavior.Behavior; import org.bench4q.agent.scenario.behavior.Behavior;

View File

@ -8,7 +8,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.agent.api.model.behavior.Batch;
import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.plugin.result.HttpReturn; import org.bench4q.agent.plugin.result.HttpReturn;

View File

@ -1,6 +1,5 @@
package org.bench4q.agent.scenario; package org.bench4q.agent.scenario;
import org.bench4q.agent.api.model.behavior.Batch;
public class ScenarioNew { public class ScenarioNew {
private UsePlugin[] usePlugins; private UsePlugin[] usePlugins;

View File

@ -4,14 +4,14 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import static org.junit.Assert.*;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.bench4q.agent.api.model.RunScenarioModelOld; import org.bench4q.agent.api.model.RunScenarioModelNew;
import Communication.HttpRequester; import Communication.HttpRequester;
public class TestWithScriptFile { public class TestWithScriptFile {
@ -37,24 +37,28 @@ public class TestWithScriptFile {
} }
public TestWithScriptFile() { public TestWithScriptFile() {
this.setFilePath("scripts" + System.getProperty("file.separator") this.setFilePath("Scripts" + System.getProperty("file.separator")
+ "ScriptSuzou.xml"); + "forBatch.xml");
this.setHttpRequester(new HttpRequester()); this.setHttpRequester(new HttpRequester());
} }
public void testWithScript() throws JAXBException { public void testWithScript() throws JAXBException {
File file = new File(this.getFilePath()); File file = new File(this.getFilePath());
if (!file.exists()) { if (!file.exists()) {
System.out.println("There's no this script!");
return; return;
} }
String scriptContent; String scriptContent;
try { try {
scriptContent = FileUtils.readFileToString(file); scriptContent = FileUtils.readFileToString(file);
RunScenarioModelOld runScenarioModel = extractRunScenarioModel(scriptContent); RunScenarioModelNew runScenarioModel = extractRunScenarioModel(scriptContent);
if (runScenarioModel == null) { if (runScenarioModel == null) {
System.out.println("can't execute an unvalid script"); System.out.println("can't execute an unvalid script");
return; return;
} }
assertTrue(runScenarioModel.getBatches().size() > 0);
assertTrue(runScenarioModel.getBatches().get(0).getBehaviors()
.size() > 0);
runScenarioModel.setPoolSize(load); runScenarioModel.setPoolSize(load);
this.getHttpRequester().sendPostXml(this.url, this.getHttpRequester().sendPostXml(this.url,
@ -64,24 +68,25 @@ public class TestWithScriptFile {
} }
} }
private RunScenarioModelOld extractRunScenarioModel(String scriptContent) { private RunScenarioModelNew extractRunScenarioModel(String scriptContent) {
try { try {
Unmarshaller unmarshaller = JAXBContext.newInstance( Unmarshaller unmarshaller = JAXBContext.newInstance(
RunScenarioModelOld.class).createUnmarshaller(); RunScenarioModelNew.class).createUnmarshaller();
return (RunScenarioModelOld) unmarshaller return (RunScenarioModelNew) unmarshaller
.unmarshal(new ByteArrayInputStream(scriptContent .unmarshal(new ByteArrayInputStream(scriptContent
.getBytes())); .getBytes()));
} catch (JAXBException e) { } catch (JAXBException e) {
System.out.println("model unmarshal has an exception!"); System.out.println("model unmarshal has an exception!");
e.printStackTrace();
return null; return null;
} }
} }
private String marshalRunScenarioModel(RunScenarioModelOld model) private String marshalRunScenarioModel(RunScenarioModelNew model)
throws JAXBException { throws JAXBException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JAXBContext.newInstance(RunScenarioModelOld.class).createMarshaller() JAXBContext.newInstance(RunScenarioModelNew.class).createMarshaller()
.marshal(model, outputStream); .marshal(model, outputStream);
return outputStream.toString(); return outputStream.toString();
} }