use the new RunScenarioModel, and can pass the old tests as well as my

newest added one
This commit is contained in:
coderfengyun 2013-12-19 16:38:50 +08:00
parent b0ff48e44c
commit bfa9e7966d
11 changed files with 202 additions and 58 deletions

View File

@ -0,0 +1,39 @@
package org.bench4q.master.helper;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.scriptrecord.BatchBehavior;
import org.bench4q.share.models.agent.scriptrecord.UserBehaviorModel;
import ModelTest.Page;
import ModelTest.RunScenarioModelNew;
public class RunScenarioModelHelper {
public static List<BatchBehavior> getBatches(
RunScenarioModelNew runScenarioModelNew) {
List<BatchBehavior> result = new ArrayList<BatchBehavior>();
for (Page page : runScenarioModelNew.getPages()) {
result.addAll(page.getBatches());
}
return result;
}
public static UserBehaviorModel createUserBehaviorModel(int id, String use,
String name, List<ParameterModel> parameterModels) {
UserBehaviorModel userBehaviorModel = new UserBehaviorModel();
userBehaviorModel.setUse(use);
userBehaviorModel.setId(id);
userBehaviorModel.setName(name);
userBehaviorModel.setParameters(parameterModels);
return userBehaviorModel;
}
public static ParameterModel createParameterModel(String key, String value) {
ParameterModel parameterModel = new ParameterModel();
parameterModel.setKey(key);
parameterModel.setValue(value);
return parameterModel;
}
}

View File

@ -8,23 +8,30 @@ import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.apache.log4j.Logger;
import org.bench4q.master.helper.MarshalHelper;
import org.bench4q.master.helper.RunScenarioModelHelper;
import org.bench4q.master.scriptrecord.httpcapture.generator.ChildrenUrl;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchBehavior;
import org.bench4q.share.models.agent.scriptrecord.BehaviorBaseModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
public class Bench4qTestScriptAdapter implements IScriptAdapter {
private RunScenarioModel runScenarioModel;
private List<ChildrenUrl> childrenUrls = new ArrayList<ChildrenUrl>();
import ModelTest.Page;
import ModelTest.RunScenarioModelNew;
public RunScenarioModel getRunScenarioModel() {
public class Bench4qTestScriptAdapter implements IScriptAdapter {
private int pageCount;
private RunScenarioModelNew runScenarioModel;
private List<ChildrenUrl> childrenUrls = new ArrayList<ChildrenUrl>();
private static Logger logger = Logger
.getLogger(Bench4qTestScriptAdapter.class);
public RunScenarioModelNew getRunScenarioModel() {
return runScenarioModel;
}
private void setRunScenarioModel(RunScenarioModel runScenarioModel) {
private void setRunScenarioModel(RunScenarioModelNew runScenarioModel) {
this.runScenarioModel = runScenarioModel;
}
@ -36,27 +43,46 @@ public class Bench4qTestScriptAdapter implements IScriptAdapter {
this.childrenUrls = childrenUrls;
}
public Bench4qTestScriptAdapter(RunScenarioModel runScenarioModel) {
private int getPageCount() {
return pageCount;
}
private void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public Bench4qTestScriptAdapter(RunScenarioModelNew runScenarioModel) {
this.runScenarioModel = runScenarioModel;
this.setChildrenUrls(new ArrayList<ChildrenUrl>());
this.setPageCount(0);
initWithOnePage();
}
private void initWithOnePage() {
addPage();
}
public void appendUsePluginsToScenario(List<UsePluginModel> usePluginModels) {
this.runScenarioModel.setUsePlugins(usePluginModels);
}
public void addPage() {
this.setPageCount(this.getPageCount() + 1);
this.getRunScenarioModel().getPages().add(new Page());
assert (pageCountEqualWithPageSizeInScenario());
}
public void insertUserBehaviorsToScenario(BehaviorBaseModel model) {
List<BatchBehavior> batches = this.getRunScenarioModel().getBatches();
List<BatchBehavior> batches = this.getCurrentPage().getBatches();
int parentBatchId = -1;
assert (batches != null);
parentBatchId = getUrlParentBatch(model);
if (isIndependent(parentBatchId)) {
batches.add(createBatchBehavior(model, batches.size()));
batches.add(createBatchBehaviorWithProperBatchId(model));
return;
}
if (isChildBatchAbsent(batches, parentBatchId)) {
BatchBehavior batchBehavior = createBatchBehavior(model,
batches.size());
BatchBehavior batchBehavior = createBatchBehaviorWithProperBatchId(model);
batchBehavior.setParentId(parentBatchId);
batches.add(batchBehavior);
batches.get(parentBatchId).setChildId(batches.size() - 1);
@ -66,10 +92,25 @@ public class Bench4qTestScriptAdapter implements IScriptAdapter {
.add(model);
}
private BatchBehavior createBatchBehavior(BehaviorBaseModel model,
int batchId) {
private Page getCurrentPage() {
if (!pageCountEqualWithPageSizeInScenario()) {
logger.error("The page count is not equal with the size of scenario pages");
return new Page();
}
return this.getRunScenarioModel().getPages()
.get(this.getPageCount() - 1);
}
private boolean pageCountEqualWithPageSizeInScenario() {
return this.getPageCount() == this.getRunScenarioModel().getPages()
.size();
}
private BatchBehavior createBatchBehaviorWithProperBatchId(
BehaviorBaseModel model) {
BatchBehavior batchBehavior = new BatchBehavior();
batchBehavior.setId(batchId);
batchBehavior.setId(RunScenarioModelHelper.getBatches(
this.runScenarioModel).size());
batchBehavior.setParentId(-1);
batchBehavior.setChildId(-1);
List<BehaviorBaseModel> behaviors = new ArrayList<BehaviorBaseModel>();
@ -117,10 +158,10 @@ public class Bench4qTestScriptAdapter implements IScriptAdapter {
}
public int getParentBatchIdWithParentUrl(String url) {
for (int batchId = 0; batchId < this.getRunScenarioModel().getBatches()
.size(); batchId++) {
BatchBehavior batch = this.getRunScenarioModel().getBatches()
.get(batchId);
List<BatchBehavior> batchesInScenario = RunScenarioModelHelper
.getBatches(this.getRunScenarioModel());
for (int batchId = 0; batchId < batchesInScenario.size(); batchId++) {
BatchBehavior batch = batchesInScenario.get(batchId);
for (BehaviorBaseModel behavior : batch.getBehaviors()) {
for (ParameterModel parameter : behavior.getParameters()) {
if (parameter.getKey().equals("url")
@ -149,11 +190,11 @@ public class Bench4qTestScriptAdapter implements IScriptAdapter {
public void setText(String text) {
if (text.isEmpty()) {
this.setRunScenarioModel(new RunScenarioModel());
this.setRunScenarioModel(new RunScenarioModelNew());
return;
}
this.runScenarioModel = (RunScenarioModel) MarshalHelper.unmarshal(
RunScenarioModel.class, text);
this.runScenarioModel = (RunScenarioModelNew) MarshalHelper.unmarshal(
RunScenarioModelNew.class, text);
}
}

View File

@ -8,7 +8,7 @@ import java.util.concurrent.Executors;
import javax.swing.JTextArea;
import org.apache.log4j.Logger;
import org.bench4q.share.models.agent.RunScenarioModel;
import ModelTest.RunScenarioModelNew;
public class HttpCapture {
private int localport;
@ -93,7 +93,7 @@ public class HttpCapture {
e.printStackTrace();
}
this.currentTest = new Test(this.proxy, new Bench4qTestScriptAdapter(
new RunScenarioModel()), generator);
new RunScenarioModelNew()), generator);
}
public void startProxyServer() throws IOException, Utils.UserException {

View File

@ -3,13 +3,14 @@ package org.bench4q.master.scriptrecord.httpcapture;
import java.util.List;
import org.bench4q.master.scriptrecord.httpcapture.generator.ChildrenUrl;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorBaseModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import ModelTest.RunScenarioModelNew;
public interface IScriptAdapter {
public abstract RunScenarioModel getRunScenarioModel();
public abstract RunScenarioModelNew getRunScenarioModel();
public abstract void appendUsePluginsToScenario(
List<UsePluginModel> usePluginModels);
@ -26,4 +27,6 @@ public interface IScriptAdapter {
public abstract int getParentBatchIdWithParentUrl(String url);
public abstract boolean isInChildrenUrl(String url);
public abstract void addPage();
}

View File

@ -29,9 +29,10 @@ import org.bench4q.master.scriptrecord.httpcapture.Param;
import org.bench4q.master.scriptrecord.httpcapture.ProxyServer;
import org.bench4q.master.scriptrecord.httpcapture.Utils;
import org.bench4q.master.scriptrecord.httpcapture.Utils.UserException;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorBaseModel;
import ModelTest.RunScenarioModelNew;
public abstract class AbstractCodeGenerator implements IScriptGenerator,
ProxyServer.Observer, Runnable {
private Config config = Config.getConfig();
@ -130,7 +131,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
public boolean doSave(String path, String fileName) {
String name = fileName;
int dotPos = fileName.indexOf(".");
RunScenarioModel runScenarioModel = this.scriptAdapter
RunScenarioModelNew runScenarioModel = this.scriptAdapter
.getRunScenarioModel();
FileWriter fileWriter;
@ -330,6 +331,9 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
public void processResponse(HttpRequestHeader header, byte[] requestBody,
byte[] response) throws Exception {
if (isFirstRequest()) {
}
if (this.ignoreNextResponse) {
log.debug("Ignoring response");
this.ignoreNextResponse = false;
@ -369,7 +373,6 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
while (!(Thread.interrupted())) {
synchronized (this.outstandingInserts) {
this.outstandingInserts.wait();
while (this.outstandingInserts.size() > 0) {
BehaviorBaseModel behaviorModel = this.outstandingInserts
.removeFirst();

View File

@ -1,5 +1,6 @@
package ModelTest;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
@ -22,4 +23,7 @@ public class Page {
this.batches = batches;
}
public Page() {
this.setBatches(new ArrayList<BatchBehavior>());
}
}

View File

@ -46,5 +46,6 @@ public class RunScenarioModelNew {
public RunScenarioModelNew() {
this.setUsePlugins(new ArrayList<UsePluginModel>());
this.setPages(new ArrayList<Page>());
}
}

View File

@ -0,0 +1,46 @@
package org.bench4q.master.test.recordscript;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.master.helper.RunScenarioModelHelper;
import org.bench4q.master.scriptrecord.httpcapture.Bench4qTestScriptAdapter;
import org.bench4q.share.models.agent.ParameterModel;
import org.junit.Test;
import ModelTest.RunScenarioModelNew;
public class TestBench4qTestScriptAdapter {
private Bench4qTestScriptAdapter bench4qTestScriptAdapter;
private Bench4qTestScriptAdapter getBench4qTestScriptAdapter() {
return bench4qTestScriptAdapter;
}
private void setBench4qTestScriptAdapter(
Bench4qTestScriptAdapter bench4qTestScriptAdapter) {
this.bench4qTestScriptAdapter = bench4qTestScriptAdapter;
}
public TestBench4qTestScriptAdapter() {
this.setBench4qTestScriptAdapter(new Bench4qTestScriptAdapter(
new RunScenarioModelNew()));
}
@Test
public void testInsertUserBehaviorsToScenario() {
List<ParameterModel> params = new ArrayList<ParameterModel>();
params.add(RunScenarioModelHelper.createParameterModel("url",
"www.baidu.com"));
this.getBench4qTestScriptAdapter().insertUserBehaviorsToScenario(
RunScenarioModelHelper.createUserBehaviorModel(0, "http",
"get", params));
assertEquals(
1,
RunScenarioModelHelper.getBatches(
this.getBench4qTestScriptAdapter()
.getRunScenarioModel()).size());
}
}

View File

@ -17,12 +17,13 @@ import org.bench4q.master.scriptrecord.httpcapture.generator.Bench4qCodeGenerato
import org.bench4q.master.scriptrecord.httpcapture.generator.ContentDecoder;
import org.bench4q.master.scriptrecord.httpcapture.generator.IScriptGenerator;
import org.bench4q.master.scriptrecord.httpcapture.generator.ResponseParser;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.junit.Test;
import ModelTest.RunScenarioModelNew;
public class TestCodeGenerator extends TestRecordBase {
private Bench4qTestScriptAdapter scriptAdapter = new Bench4qTestScriptAdapter(
new RunScenarioModel());
new RunScenarioModelNew());
private Bench4qCodeGenerator codeGenerator = new Bench4qCodeGenerator(
this.scriptAdapter);
public static String dealWithSerie = "RecordScriptTestCase/gzip/gzipBaiduIR";

View File

@ -5,15 +5,17 @@ import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.master.helper.RunScenarioModelHelper;
import org.bench4q.master.scriptrecord.httpcapture.Bench4qTestScriptAdapter;
import org.bench4q.master.scriptrecord.httpcapture.IScriptAdapter;
import org.bench4q.master.scriptrecord.httpcapture.generator.ChildrenUrl;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchBehavior;
import org.bench4q.share.models.agent.scriptrecord.UserBehaviorModel;
import org.junit.Test;
import ModelTest.RunScenarioModelNew;
public class TestParentRequest {
private IScriptAdapter adapater;
private static String parentUrl = "www.apache.com";
@ -31,7 +33,7 @@ public class TestParentRequest {
}
public TestParentRequest() {
this.setAdapater(new Bench4qTestScriptAdapter(new RunScenarioModel()));
this.setAdapater(new Bench4qTestScriptAdapter(new RunScenarioModelNew()));
}
@Test
@ -39,11 +41,11 @@ public class TestParentRequest {
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(parentUrl));
assertNotNull(this.getAdapater().getRunScenarioModel());
assertNotNull(this.getAdapater().getRunScenarioModel().getBatches());
assertNotNull(this.getAdapater().getRunScenarioModel().getBatches()
.get(0));
assertTrue(this.getAdapater().getRunScenarioModel().getBatches().get(0)
.getId() == 0);
List<BatchBehavior> batches = RunScenarioModelHelper.getBatches(this
.getAdapater().getRunScenarioModel());
assertNotNull(batches);
assertNotNull(batches.get(0));
assertTrue(batches.get(0).getId() == 0);
}
@ -58,17 +60,19 @@ public class TestParentRequest {
createUserBehavior(independentUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl2));
RunScenarioModel runScenarioModel = this.getAdapater()
RunScenarioModelNew runScenarioModel = this.getAdapater()
.getRunScenarioModel();
assertNotNull(runScenarioModel);
assertNotNull(runScenarioModel.getBatches());
assertTrue(runScenarioModel.getBatches().size() == 3);
assertTrue(runScenarioModel.getBatches().get(1).getBehaviors().size() == 1);
assertTrue(runScenarioModel.getBatches().get(2).getBehaviors().size() == 1);
assertTrue(runScenarioModel.getBatches().get(1).getBehaviors().get(0)
.getParameters().get(0).getValue().equals(independentUrl));
assertTrue(runScenarioModel.getBatches().get(2).getBehaviors().get(0)
.getParameters().get(0).getValue().equals(independentUrl2));
List<BatchBehavior> batches = RunScenarioModelHelper
.getBatches(runScenarioModel);
assertNotNull(batches);
assertTrue(batches.size() == 3);
assertTrue(batches.get(1).getBehaviors().size() == 1);
assertTrue(batches.get(2).getBehaviors().size() == 1);
assertTrue(batches.get(1).getBehaviors().get(0).getParameters().get(0)
.getValue().equals(independentUrl));
assertTrue(batches.get(2).getBehaviors().get(0).getParameters().get(0)
.getValue().equals(independentUrl2));
}
private UserBehaviorModel createUserBehavior(String url) {
@ -93,13 +97,14 @@ public class TestParentRequest {
.add(createChildrenUrl(childUrl, 0));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(childUrl));
RunScenarioModel runScenarioModel = this.getAdapater()
RunScenarioModelNew runScenarioModel = this.getAdapater()
.getRunScenarioModel();
assertNotNull(runScenarioModel);
assertNotNull(runScenarioModel.getBatches());
assertNotNull(runScenarioModel.getBatches().get(0));
assertNotNull(runScenarioModel.getBatches().get(1));
List<BatchBehavior> batches = runScenarioModel.getBatches();
List<BatchBehavior> batches = RunScenarioModelHelper
.getBatches(runScenarioModel);
assertNotNull(batches);
assertNotNull(batches.get(0));
assertNotNull(batches.get(1));
assertTrue(batches.get(1).getId() == 1);
assertEquals(batches.get(0).getChildId(), batches.get(1).getId());
assertEquals(batches.get(1).getParentId(), batches.get(0).getId());
@ -124,8 +129,8 @@ public class TestParentRequest {
createUserBehavior(childUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl));
List<BatchBehavior> batches = this.getAdapater().getRunScenarioModel()
.getBatches();
List<BatchBehavior> batches = RunScenarioModelHelper.getBatches(this
.getAdapater().getRunScenarioModel());
assertTrue(batches.get(1).getBehaviors().size() == 1);
assertTrue(batches.get(2).getBehaviors().size() == 1);
assertTrue(batches.get(1).getBehaviors().get(0).getParameters().get(0)
@ -148,8 +153,8 @@ public class TestParentRequest {
createUserBehavior(childUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(childUrl2));
List<BatchBehavior> batches = this.getAdapater().getRunScenarioModel()
.getBatches();
List<BatchBehavior> batches = RunScenarioModelHelper.getBatches(this
.getAdapater().getRunScenarioModel());
System.out.println(batches.size());
assertTrue(batches.size() == 2);
assertTrue(batches.get(0).getBehaviors().get(0).getParameters().get(0)
@ -176,7 +181,8 @@ public class TestParentRequest {
createUserBehavior(independentUrl));
this.getAdapater().insertUserBehaviorsToScenario(
createUserBehavior(independentUrl2));
assertTrue(this.getAdapater().getRunScenarioModel().getBatches().size() == 4);
assertTrue(RunScenarioModelHelper.getBatches(
this.getAdapater().getRunScenarioModel()).size() == 4);
}
@Test

View File

@ -7,8 +7,8 @@ import org.bench4q.master.scriptrecord.httpcapture.Bench4qTestScriptAdapter;
import org.bench4q.master.scriptrecord.httpcapture.IScriptAdapter;
import org.bench4q.master.scriptrecord.httpcapture.generator.Bench4qCodeGenerator;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.UserBehaviorModel;
import ModelTest.RunScenarioModelNew;
public abstract class TestRecordBase {
private IScriptAdapter adapter;
@ -34,7 +34,7 @@ public abstract class TestRecordBase {
}
protected void init() {
this.setAdpater(new Bench4qTestScriptAdapter(new RunScenarioModel()));
this.setAdpater(new Bench4qTestScriptAdapter(new RunScenarioModelNew()));
this.setCodeGenerator(new Bench4qCodeGenerator(this.getAdpater()));
}