refactor and add tests about compiling behavior to instruction

refactor and add tests about compiling behavior to instruction
This commit is contained in:
coderfengyun 2014-09-19 10:59:14 +08:00
parent cd68f1a03a
commit 8eaf0e26b4
27 changed files with 242 additions and 172 deletions

View File

@ -11,6 +11,7 @@ import org.bench4q.agent.scenario.engine.ScenarioContext;
import org.bench4q.agent.scenario.engine.ScenarioEngine;
import org.bench4q.agent.scenario.engine.Schedule;
import org.bench4q.agent.scenario.node.Behavior;
import org.bench4q.agent.scenario.node.PluginImplementBehavior;
import org.bench4q.agent.scenario.node.UserBehavior;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.CleanTestResultModel;
@ -90,8 +91,8 @@ public class TestController {
RunScenarioModel.class, scenarioModel);
System.out.println(scheduleContent);
this.logger.info(scheduleContent);
ScheduleModel scheduleModel = MarshalHelper
.unmarshal(ScheduleModel.class, scheduleContent);
ScheduleModel scheduleModel = MarshalHelper.unmarshal(
ScheduleModel.class, scheduleContent);
this.getScenarioEngine().submitScenario(runId,
Scenario.scenarioCompiler(runScenarioModel),
Schedule.build(scheduleModel), realStartTime);
@ -159,16 +160,19 @@ public class TestController {
if (!(behavior instanceof UserBehavior)) {
continue;
}
PluginImplementBehavior pluginImplementBehavior = (PluginImplementBehavior) behavior;
try {
BehaviorBriefModel briefModel = behavior
BehaviorBriefModel briefModel = pluginImplementBehavior
.getBehaviorBriefResult(scenarioContext
.getDataCollector());
logger.info(behavior.getId());
briefModel
.setBehaviorUrl(behavior.getSpecificParamValue("url"));
logger.info(pluginImplementBehavior.getId());
briefModel.setBehaviorUrl(pluginImplementBehavior
.getSpecificParamValue("url"));
behaviorBriefModels.add(briefModel);
} catch (Exception e) {
logger.info("where behaviorId is " + behavior.getId(), e);
logger.info(
"where behaviorId is "
+ pluginImplementBehavior.getId(), e);
}
}

View File

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

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin.behavior;
package org.bench4q.agent.plugin;
public enum BehaviorType {
USER_BEHAVIOR, TIMER_BEHAVIOR, CONTROL_BEHAVIOR, TEST_BEHAVIOR

View File

@ -10,7 +10,7 @@ import java.util.Map;
import org.apache.log4j.Logger;
import org.bench4q.agent.helper.ClassHelper;
import org.bench4q.agent.helper.ClassLoadRestriction;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.share.helper.ExceptionLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

View File

@ -21,8 +21,8 @@ import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
import org.bench4q.share.exception.Bench4QRunTimeException;

View File

@ -8,8 +8,8 @@ import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
import org.bench4q.agent.utils.types.Table;
import org.bench4q.agent.utils.types.Table.Row;
@ -48,17 +48,17 @@ public class MongoDBPlugin {
try {
List<ServerAddress> hostsList = new ArrayList<ServerAddress>();
String[] hosts = hostName.split(",");
for(String host : hosts)
{
for (String host : hosts) {
hostsList.add(new ServerAddress(host));
}
mongo = new Mongo(hostsList ,
new MongoOptions(new MongoClientOptions.Builder()
.maxWaitTime(1000 * 40).build()));
/*mongo = new Mongo(new ServerAddress(hostName, port),
new MongoOptions(new MongoClientOptions.Builder()
.maxWaitTime(1000 * 40).build()));
mongo = new Mongo(hostsList, new MongoOptions(
new MongoClientOptions.Builder().maxWaitTime(1000 * 40)
.build()));
/*
* mongo = new Mongo(new ServerAddress(hostName, port), new
* MongoOptions(new MongoClientOptions.Builder() .maxWaitTime(1000 *
* 40).build()));
*/
DB db = mongo.getDB(dbName);
DBCollection table = db.getCollection(this.tableUnderTest);
@ -85,14 +85,13 @@ public class MongoDBPlugin {
try {
List<ServerAddress> hostsList = new ArrayList<ServerAddress>();
String[] hosts = hostName.split(",");
for(String host : hosts)
{
for (String host : hosts) {
hostsList.add(new ServerAddress(host));
}
mongo = new Mongo(hostsList ,
new MongoOptions(new MongoClientOptions.Builder()
.maxWaitTime(1000 * 40).build()));
mongo = new Mongo(hostsList, new MongoOptions(
new MongoClientOptions.Builder().maxWaitTime(1000 * 40)
.build()));
DB db = mongo.getDB(dbName);
DBCollection table = db.getCollection(this.tableUnderTest);
Table propertiesTable = Table.buildTable(properties,

View File

@ -4,8 +4,8 @@ import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.ParameterBarn;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("UUID")

View File

@ -6,8 +6,8 @@ import java.io.InputStreamReader;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("CommandLine")

View File

@ -8,8 +8,8 @@ import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.ParameterBarn;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("Context")

View File

@ -15,8 +15,8 @@ import org.bench4q.agent.plugin.ParameterBarn;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.utils.Type.SupportTypes;

View File

@ -41,8 +41,8 @@ import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.basic.http.BatchRequest.BatchItem;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.utils.Type.SupportTypes;
import org.bench4q.agent.utils.types.Table;

View File

@ -4,8 +4,8 @@ import org.bench4q.agent.plugin.ParameterBarn;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin(value = "IteratorNumber")

View File

@ -3,8 +3,8 @@ package org.bench4q.agent.plugin.basic.log;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("Log")

View File

@ -7,8 +7,8 @@ import org.bench4q.agent.plugin.ParameterBarn;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.utils.Type.SupportTypes;

View File

@ -4,8 +4,8 @@ import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin("ConstantTimer")

View File

@ -4,8 +4,8 @@ import org.bench4q.agent.plugin.ParameterBarn;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
@Plugin(value = "UniqueNumber")

View File

@ -63,11 +63,12 @@ public class Scenario {
return Collections.unmodifiableList(behaviors);
}
public void compile() {
public List<Instruction> compile() {
ArrayList<Instruction> result = new ArrayList<Instruction>();
for (Behavior behavior : this.getAllBehaviors()) {
behavior.compile(new ArrayList<Instruction>(),
new Stack<TestInstruction>());
behavior.compile(result, new Stack<TestInstruction>());
}
return result;
}
public static Scenario scenarioBuilderWithCompile(String scenarioContent) {

View File

@ -19,6 +19,7 @@ import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.UsePlugin;
import org.bench4q.agent.scenario.dfa.ParamPart;
import org.bench4q.agent.scenario.node.Behavior;
import org.bench4q.agent.scenario.node.PluginImplementBehavior;
public class VUser implements Runnable {
private ScenarioContext scenarioContext;
@ -85,13 +86,14 @@ public class VUser implements Runnable {
private void doRunBatch(Map<String, Object> plugins, Batch batch,
DataCollector dataCollector) {
for (Behavior behavior : batch.getBehaviors()) {
Object plugin = plugins.get(behavior.getUse());
PluginImplementBehavior pluginBehavior = (PluginImplementBehavior) behavior;
Object plugin = plugins.get(pluginBehavior.getUse());
Date startDate = new Date(System.currentTimeMillis());
PluginReturn pluginReturn = (PluginReturn) this.getPluginManager()
.doBehavior(plugin, behavior.getName(),
reassamblyParameters(behavior, plugins));
.doBehavior(plugin, pluginBehavior.getName(),
reassamblyParameters(pluginBehavior, plugins));
Date endDate = new Date(System.currentTimeMillis());
if (!behavior.shouldBeCount()) {
if (!pluginBehavior.shouldBeCount()) {
continue;
}
dataCollector.add(buildBehaviorResult(behavior, plugin, startDate,
@ -99,8 +101,8 @@ public class VUser implements Runnable {
}
}
private Map<String, String> reassamblyParameters(Behavior behavior,
Map<String, Object> plugins) {
private Map<String, String> reassamblyParameters(
PluginImplementBehavior behavior, Map<String, Object> plugins) {
Map<String, String> behaviorParameters = new HashMap<String, String>();
for (Parameter parameter : behavior.getParameters()) {
behaviorParameters.put(parameter.getKey(),
@ -157,21 +159,24 @@ public class VUser implements Runnable {
Object plugin, Date startDate, PluginReturn pluginReturn,
Date endDate) {
BehaviorResult result = new BehaviorResult();
result.setBehaviorId(behavior.getId());
PluginImplementBehavior pluginImplementBehavior = (PluginImplementBehavior) behavior;
result.setBehaviorId(pluginImplementBehavior.getId());
result.setStartDate(startDate);
result.setEndDate(endDate);
result.setSuccessCount(pluginReturn.getSuccessCount());
result.setFailCount(pluginReturn.getFailCount());
result.setResponseTime(endDate.getTime() - startDate.getTime());
result.setBehaviorName(behavior.getName());
result.setPluginId(behavior.getUse());
result.setBehaviorName(pluginImplementBehavior.getName());
result.setPluginId(pluginImplementBehavior.getUse());
result.setPluginName(plugin.getClass().getAnnotation(Plugin.class)
.value());
result.setShouldBeCountResponseTime(behavior.shouldBeCount());
result.setShouldBeCountResponseTime(pluginImplementBehavior
.shouldBeCount());
if (pluginReturn instanceof HttpReturn) {
HttpReturn httpReturn = (HttpReturn) pluginReturn;
// TODO: this param in result is not Appropriate
result.setBehaviorUrl(behavior.getSpecificParamValue("url"));
result.setBehaviorUrl(pluginImplementBehavior
.getSpecificParamValue("url"));
result.setContentLength(httpReturn.getContentLength());
result.setContentType(httpReturn.getContentType());
result.setStatusCode(httpReturn.getStatusCode());

View File

@ -3,15 +3,11 @@ package org.bench4q.agent.scenario.node;
import java.util.ArrayList;
import java.util.Stack;
import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.scenario.Parameter;
import org.bench4q.agent.scenario.instruction.Instruction;
import org.bench4q.agent.scenario.instruction.TestInstruction;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.ConditionBehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.IfBehaviorModel;
import org.bench4q.share.models.agent.statistics.BehaviorBriefModel;
/**
*
@ -24,60 +20,6 @@ public abstract class Behavior {
public static final String TIMER_BEHAVIOR = "TIMERBEHAVIOR";
public static final String TEST_BEHAVIOR = "TESTBEHAVIOR";
private int id;
private String use;
private String name;
private Parameter[] parameters;
protected Behavior() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUse() {
return use;
}
public void setUse(String use) {
this.use = use;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Parameter[] getParameters() {
return parameters;
}
public void setParameters(Parameter[] parameters) {
this.parameters = parameters;
}
public abstract boolean shouldBeCount();
public abstract BehaviorBriefModel getBehaviorBriefResult(
DataCollector dataStatistics);
public String getSpecificParamValue(String paramName) {
for (Parameter parameter : this.getParameters()) {
if (parameter.getKey().equalsIgnoreCase(paramName)) {
return parameter.getValue();
}
}
return "";
}
/**
* No matter which one to inherit from this class, it should call
* baseCompile in its own compile function.
@ -88,6 +30,16 @@ public abstract class Behavior {
public abstract void compile(ArrayList<Instruction> code,
Stack<TestInstruction> conditions);
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public static Behavior buildWith(BehaviorModel behaviorModel) {
Behavior behavior = null;
if (behaviorModel.getType().equalsIgnoreCase(TIMER_BEHAVIOR)) {
@ -107,7 +59,6 @@ public abstract class Behavior {
throw new RuntimeException("Error type: TestInstruction"
+ behaviorModel.getClass().getName());
}
} else {
throw new IllegalArgumentException(
"The input BehaviorModel's type is not proper");
@ -115,17 +66,4 @@ public abstract class Behavior {
return behavior;
}
protected void buildAndGenerateParameterPart(BehaviorModel behaviorModel) {
this.setName(behaviorModel.getName());
this.setUse(behaviorModel.getUse());
this.setId(behaviorModel.getId());
this.setParameters(new Parameter[behaviorModel.getParameters().size()]);
for (int k = 0; k < behaviorModel.getParameters().size(); k++) {
ParameterModel parameterModel = behaviorModel.getParameters()
.get(k);
this.getParameters()[k] = Parameter.generateParamParts(
parameterModel.getKey(), parameterModel.getValue());
}
}
}

View File

@ -1,20 +1,6 @@
package org.bench4q.agent.scenario.node;
import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.share.models.agent.statistics.BehaviorBriefModel;
public abstract class ConditionedBehavior extends Behavior {
protected ConditionBehavior condition;
@Override
public boolean shouldBeCount() {
return false;
}
@Override
public BehaviorBriefModel getBehaviorBriefResult(
DataCollector dataStatistics) {
return null;
}
}

View File

@ -3,8 +3,13 @@ package org.bench4q.agent.scenario.node;
import java.util.ArrayList;
import java.util.Stack;
import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.scenario.Parameter;
import org.bench4q.agent.scenario.instruction.Instruction;
import org.bench4q.agent.scenario.instruction.TestInstruction;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.statistics.BehaviorBriefModel;
/**
* This class is the base class of those nodes that will be implemented in
@ -17,9 +22,64 @@ import org.bench4q.agent.scenario.instruction.TestInstruction;
*
*/
public abstract class PluginImplementBehavior extends Behavior {
private String use;
private String name;
private Parameter[] parameters;
public String getUse() {
return use;
}
public void setUse(String use) {
this.use = use;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Parameter[] getParameters() {
return parameters;
}
public void setParameters(Parameter[] parameters) {
this.parameters = parameters;
}
public abstract boolean shouldBeCount();
public abstract BehaviorBriefModel getBehaviorBriefResult(
DataCollector dataStatistics);
public String getSpecificParamValue(String paramName) {
for (Parameter parameter : this.getParameters()) {
if (parameter.getKey().equalsIgnoreCase(paramName)) {
return parameter.getValue();
}
}
return "";
}
protected void checkCondition(ArrayList<Instruction> code,
Stack<TestInstruction> conditions) {
code.addAll(conditions);
}
protected void buildAndGenerateParameterPart(BehaviorModel behaviorModel) {
this.setName(behaviorModel.getName());
this.setUse(behaviorModel.getUse());
this.setId(behaviorModel.getId());
this.setParameters(new Parameter[behaviorModel.getParameters().size()]);
for (int k = 0; k < behaviorModel.getParameters().size(); k++) {
ParameterModel parameterModel = behaviorModel.getParameters()
.get(k);
this.getParameters()[k] = Parameter.generateParamParts(
parameterModel.getKey(), parameterModel.getValue());
}
}
}

View File

@ -11,7 +11,7 @@ import org.bench4q.share.exception.Bench4QRunTimeException;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.statistics.BehaviorBriefModel;
public class TimerBehavior extends Behavior {
public class TimerBehavior extends PluginImplementBehavior {
public TimerBehavior(BehaviorModel timerBehaviorModel) {
if (timerBehaviorModel.getType() != Behavior.TIMER_BEHAVIOR) {

View File

@ -3,10 +3,8 @@ package org.bench4q.agent.scenario.node;
import java.util.ArrayList;
import java.util.Stack;
import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.scenario.instruction.Instruction;
import org.bench4q.agent.scenario.instruction.TestInstruction;
import org.bench4q.share.models.agent.statistics.BehaviorBriefModel;
public class TransactionBehavior extends Behavior {
@Override
@ -15,15 +13,4 @@ public class TransactionBehavior extends Behavior {
// TODO:
}
@Override
public boolean shouldBeCount() {
return true;
}
@Override
public BehaviorBriefModel getBehaviorBriefResult(
DataCollector dataStatistics) {
return null;
}
}

View File

@ -37,8 +37,9 @@ public class UserBehavior extends PluginImplementBehavior {
@Override
public BehaviorBriefModel getBehaviorBriefResult(
DataCollector dataStatistics) {
return (BehaviorBriefModel) dataStatistics
BehaviorBriefModel result = (BehaviorBriefModel) dataStatistics
.getBehaviorBriefStatistics(this.getId());
return result;
}
}

View File

@ -0,0 +1,87 @@
package org.bench4q.agent.test.scenario;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Stack;
import org.bench4q.agent.scenario.instruction.ControlInstruction;
import org.bench4q.agent.scenario.instruction.Instruction;
import org.bench4q.agent.scenario.instruction.SampleInstruction;
import org.bench4q.agent.scenario.instruction.TestInstruction;
import org.bench4q.agent.scenario.instruction.TimerInstruction;
import org.bench4q.agent.scenario.node.Behavior;
import org.bench4q.agent.scenario.node.PluginImplementBehavior;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.junit.Test;
public class Test_Behavior {
@Test
public void tsetComileUserBehaviorToInstructions() {
PluginImplementBehavior behavior = (PluginImplementBehavior) Behavior
.buildWith(BehaviorModel.UserBehaviorBuilder(1, "GET", "Http",
Arrays.asList(ParameterModel.createParameter("url",
"http://www.baidu.com"))));
ArrayList<Instruction> code = new ArrayList<Instruction>();
Stack<TestInstruction> conditions = new Stack<TestInstruction>();
behavior.compile(code, conditions);
assertEquals(1, code.size());
assertEquals(0, conditions.size());
assertEquals(behavior.getName(),
((SampleInstruction) code.get(0)).getBehaviorName());
assertEquals(behavior.getUse(),
((SampleInstruction) code.get(0)).getPluginName());
assertEquals(1,
((SampleInstruction) code.get(0)).getParameters().length);
assertArrayEquals(behavior.getParameters(),
((SampleInstruction) code.get(0)).getParameters());
}
@Test
public void testCompileTimerBehaviorToInstruction() {
PluginImplementBehavior behavior = (PluginImplementBehavior) Behavior
.buildWith(BehaviorModel.TimerBehaviorBuilder(1, "sleep",
"ConstantTimer", Arrays.asList(ParameterModel
.createParameter("time", "20"))));
ArrayList<Instruction> code = new ArrayList<Instruction>();
Stack<TestInstruction> conditions = new Stack<TestInstruction>();
behavior.compile(code, conditions);
assertEquals(1, code.size());
assertEquals(0, conditions.size());
assertEquals(behavior.getName(),
((TimerInstruction) code.get(0)).getBehaviorName());
assertEquals(behavior.getUse(),
((TimerInstruction) code.get(0)).getPluginName());
assertEquals(1, ((TimerInstruction) code.get(0)).getParameters().length);
assertArrayEquals(behavior.getParameters(),
((TimerInstruction) code.get(0)).getParameters());
}
@Test
public void testCompileControlBehavior2Instruction() {
PluginImplementBehavior behavior = (PluginImplementBehavior) Behavior
.buildWith(BehaviorModel.ControlBehaviorBuilder(1, "next",
"CsvProvider", Collections.<ParameterModel> emptyList()));
ArrayList<Instruction> code = new ArrayList<Instruction>();
Stack<TestInstruction> conditions = new Stack<TestInstruction>();
behavior.compile(code, conditions);
assertEquals(1, code.size());
assertEquals(0, conditions.size());
assertEquals(behavior.getName(),
((ControlInstruction) code.get(0)).getBehaviorName());
assertEquals(behavior.getUse(),
((ControlInstruction) code.get(0)).getPluginName());
assertEquals(0,
((ControlInstruction) code.get(0)).getParameters().length);
assertArrayEquals(behavior.getParameters(),
((ControlInstruction) code.get(0)).getParameters());
}
@Test
public void testCompileIfBehavior2Instruction() {
// Behavior behavior = Behavior.buildWith(BehaviorModel.)
}
}

View File

@ -16,6 +16,7 @@ import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.dfa.ParamPart;
import org.bench4q.agent.scenario.dfa.ParamPart.ParamPartType;
import org.bench4q.agent.scenario.node.Behavior;
import org.bench4q.agent.scenario.node.PluginImplementBehavior;
import org.bench4q.agent.test.TestBase;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.helper.TestHelper;
@ -119,10 +120,11 @@ public class Test_Scenario extends TestBase {
}));
Scenario scenario = Scenario.scenarioCompiler(inputModel);
for (Behavior behavior : scenario.getAllBehaviors()) {
assertEquals("first", behavior.getName());
assertEquals(0, behavior.getId());
assertEquals("http", behavior.getUse());
for (Parameter parameter : behavior.getParameters()) {
PluginImplementBehavior pluginBehavior = (PluginImplementBehavior) behavior;
assertEquals("first", pluginBehavior.getName());
assertEquals(0, pluginBehavior.getId());
assertEquals("http", pluginBehavior.getUse());
for (Parameter parameter : pluginBehavior.getParameters()) {
assertNotNull(parameter.getParamParts());
List<ParamPart> result = Arrays.asList(parameter
.getParamParts());

View File

@ -66,7 +66,7 @@ public class Test_ScenarioContext extends TestBase {
assertEquals(1, context.getExecutor().getMaximumPoolSize());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void test_updatePopulationWithZero() {
ScenarioContext context = getScenarioWithScenarioAndSchedule();
context.initTasks(10);