diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java index a2bdab4c..aaf90162 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java @@ -7,11 +7,11 @@ import java.util.UUID; import org.apache.log4j.Logger; import org.bench4q.agent.plugin.ParameterFileCollector; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.behavior.Behavior; -import org.bench4q.agent.scenario.behavior.UserBehavior; 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.UserBehavior; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.agent.CleanTestResultModel; import org.bench4q.share.models.agent.RunScenarioModel; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Batch.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Batch.java index f05a3a2f..2af555cb 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Batch.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Batch.java @@ -1,6 +1,6 @@ package org.bench4q.agent.scenario; -import org.bench4q.agent.scenario.behavior.Behavior; +import org.bench4q.agent.scenario.node.Behavior; public class Batch { private int id; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Parameter.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Parameter.java index 99ad15f0..fb830855 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Parameter.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Parameter.java @@ -37,13 +37,19 @@ public class Parameter { public Parameter() { } - public Parameter(String key, String value) { + private Parameter(String key, String value) { this(); this.setKey(key); this.setValue(value); } - public void compileToGenerateParts() { + public static Parameter generateParamParts(String key, String value) { + Parameter parameter = new Parameter(key, value); + parameter.compileToGenerateParts(); + return parameter; + } + + private void compileToGenerateParts() { List paramParts = DFA.resolveMaltipleParamPart(this .getValue()); this.setParamParts(paramParts.toArray(new ParamPart[paramParts.size()])); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java index 1bc2ea1a..250bc569 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java @@ -3,8 +3,11 @@ package org.bench4q.agent.scenario; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Stack; -import org.bench4q.agent.scenario.behavior.Behavior; +import org.bench4q.agent.scenario.instruction.Instruction; +import org.bench4q.agent.scenario.instruction.TestInstruction; +import org.bench4q.agent.scenario.node.Behavior; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.RunScenarioModel; @@ -62,7 +65,8 @@ public class Scenario { public void compile() { for (Behavior behavior : this.getAllBehaviors()) { - behavior.compile(); + behavior.compile(new ArrayList(), + new Stack()); } } @@ -76,7 +80,6 @@ public class Scenario { throw new NullPointerException(); } Scenario scenario = extractScenario(scenarioModel); - scenario.compile(); return scenario; } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java deleted file mode 100644 index 79377d27..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.bench4q.agent.scenario.behavior; - -import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; - -public class TestBehavior extends Behavior { - - @Override - public boolean shouldBeCount() { - return false; - } - - @Override - public BehaviorBriefModel getBehaviorBriefResult( - DataCollector dataStatistics) { - return null; - } - -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java deleted file mode 100644 index f33d5221..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.bench4q.agent.scenario.behavior; - -import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; - -public class TimerBehavior extends Behavior { - - @Override - public boolean shouldBeCount() { - return false; - } - - @Override - public BehaviorBriefModel getBehaviorBriefResult( - DataCollector dataStatistics) { - return null; - } - -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java deleted file mode 100644 index 5f710672..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.bench4q.agent.scenario.behavior; - -import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; - -public class UserBehavior extends Behavior { - @Override - public boolean shouldBeCount() { - return true; - } - - @Override - public BehaviorBriefModel getBehaviorBriefResult( - DataCollector dataStatistics) { - return (BehaviorBriefModel) dataStatistics.getBehaviorBriefStatistics(this.getId()); - } - -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java index 7c499b99..2761e042 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java @@ -17,8 +17,8 @@ import org.bench4q.agent.scenario.Page; import org.bench4q.agent.scenario.Parameter; import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.UsePlugin; -import org.bench4q.agent.scenario.behavior.Behavior; import org.bench4q.agent.scenario.dfa.ParamPart; +import org.bench4q.agent.scenario.node.Behavior; public class VUser implements Runnable { private ScenarioContext scenarioContext; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/ControlInstruction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/ControlInstruction.java new file mode 100644 index 00000000..e42f8a9a --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/ControlInstruction.java @@ -0,0 +1,11 @@ +package org.bench4q.agent.scenario.instruction; + +import org.bench4q.agent.scenario.Parameter; + +public class ControlInstruction extends PluginImplementInstruction { + + public ControlInstruction(String pluginName, String behaviorName, + Parameter[] parameters) { + super(pluginName, behaviorName, parameters); + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/GotoInstruction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/GotoInstruction.java new file mode 100644 index 00000000..2d3e9115 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/GotoInstruction.java @@ -0,0 +1,9 @@ +package org.bench4q.agent.scenario.instruction; + +public class GotoInstruction implements Instruction { + public int label; + + public GotoInstruction(int label) { + this.label = label; + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/Instruction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/Instruction.java new file mode 100644 index 00000000..e42aa049 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/Instruction.java @@ -0,0 +1,5 @@ +package org.bench4q.agent.scenario.instruction; + +public interface Instruction { + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/PluginImplementInstruction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/PluginImplementInstruction.java new file mode 100644 index 00000000..b93bbd12 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/PluginImplementInstruction.java @@ -0,0 +1,29 @@ +package org.bench4q.agent.scenario.instruction; + +import org.bench4q.agent.scenario.Parameter; + +public abstract class PluginImplementInstruction implements Instruction { + private final String pluginName; + private final String behaviorName; + private final Parameter[] parameters; + + public PluginImplementInstruction(String pluginName, String behaviorName, + Parameter[] parameters) { + this.pluginName = pluginName; + this.behaviorName = behaviorName; + this.parameters = parameters; + } + + public String getPluginName() { + return pluginName; + } + + public String getBehaviorName() { + return behaviorName; + } + + public Parameter[] getParameters() { + return parameters; + } + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/SampleInstruction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/SampleInstruction.java new file mode 100644 index 00000000..518df304 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/SampleInstruction.java @@ -0,0 +1,12 @@ +package org.bench4q.agent.scenario.instruction; + +import org.bench4q.agent.scenario.Parameter; + +public class SampleInstruction extends PluginImplementInstruction { + + public SampleInstruction(String pluginName, String behaviorName, + Parameter[] parameters) { + super(pluginName, behaviorName, parameters); + } + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TestInstruction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TestInstruction.java new file mode 100644 index 00000000..ee72929c --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TestInstruction.java @@ -0,0 +1,20 @@ +package org.bench4q.agent.scenario.instruction; + +import org.bench4q.agent.scenario.Parameter; + +public class TestInstruction extends PluginImplementInstruction { + private int falseLabel; + + public TestInstruction(String pluginName, String behaviorName, + Parameter[] parameters) { + super(pluginName, behaviorName, parameters); + } + + public int getFalseLabel() { + return falseLabel; + } + + public void setFalseLabel(int elseLabel) { + this.falseLabel = elseLabel; + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TimerInstruction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TimerInstruction.java new file mode 100644 index 00000000..1e800f65 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TimerInstruction.java @@ -0,0 +1,12 @@ +package org.bench4q.agent.scenario.instruction; + +import org.bench4q.agent.scenario.Parameter; + +public class TimerInstruction extends PluginImplementInstruction { + + public TimerInstruction(String pluginName, String behaviorName, + Parameter[] parameters) { + super(pluginName, behaviorName, parameters); + } + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java similarity index 51% rename from Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java rename to Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java index c483b69b..2b445146 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java @@ -1,22 +1,37 @@ -package org.bench4q.agent.scenario.behavior; +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; +/** + * + * @author coderfengyun + * + */ public abstract class Behavior { - private static final String CONTROL_BEHAVIOR = "CONTROLBEHAVIOR"; - private static final String USER_BEHAVIOR = "USERBEHAVIOR"; - private static final String TIMER_BEHAVIOR = "TIMERBEHAVIOR"; - private static final String TEST_BEHAVIOR = "TESTBEHAVIOR"; + public static final String CONTROL_BEHAVIOR = "CONTROLBEHAVIOR"; + public static final String USER_BEHAVIOR = "USERBEHAVIOR"; + 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; } @@ -63,38 +78,54 @@ public abstract class Behavior { return ""; } - public void compile() { - for (Parameter parameter : this.getParameters()) { - parameter.compileToGenerateParts(); - } - } + /** + * No matter which one to inherit from this class, it should call + * baseCompile in its own compile function. + * + * This function has side effect, the result is both code and conditions + */ + + public abstract void compile(ArrayList code, + Stack conditions); public static Behavior buildWith(BehaviorModel behaviorModel) { Behavior behavior = null; if (behaviorModel.getType().equalsIgnoreCase(TIMER_BEHAVIOR)) { - behavior = new TimerBehavior(); + behavior = new TimerBehavior(behaviorModel); } else if (behaviorModel.getType().equalsIgnoreCase(USER_BEHAVIOR)) { - behavior = new UserBehavior(); + behavior = new UserBehavior(behaviorModel); } else if (behaviorModel.getType().equalsIgnoreCase(CONTROL_BEHAVIOR)) { - behavior = new ControlBehavior(); + behavior = new ControlBehavior(behaviorModel); } else if (behaviorModel.getType().equalsIgnoreCase(TEST_BEHAVIOR)) { - behavior = new TestBehavior(); + // TODO: refactor this + if (behaviorModel instanceof IfBehaviorModel) { + behavior = new IfBehavior((IfBehaviorModel) behaviorModel); + } else if (behaviorModel instanceof ConditionBehaviorModel) { + behavior = new ConditionBehavior( + (ConditionBehaviorModel) behaviorModel); + } else { + throw new RuntimeException("Error type: TestInstruction" + + behaviorModel.getClass().getName()); + } + } else { throw new IllegalArgumentException( "The input BehaviorModel's type is not proper"); } - behavior.setName(behaviorModel.getName()); - behavior.setUse(behaviorModel.getUse()); - behavior.setId(behaviorModel.getId()); - behavior.setParameters(new Parameter[behaviorModel.getParameters() - .size()]); + 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); - behavior.getParameters()[k] = new Parameter( + this.getParameters()[k] = Parameter.generateParamParts( parameterModel.getKey(), parameterModel.getValue()); } - return behavior; } } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionBehavior.java new file mode 100644 index 00000000..f0bb86fc --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionBehavior.java @@ -0,0 +1,34 @@ +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.scriptrecord.ConditionBehaviorModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; + +public class ConditionBehavior extends PluginImplementBehavior { + + public ConditionBehavior(ConditionBehaviorModel condition) { + buildAndGenerateParameterPart(condition); + } + + @Override + public boolean shouldBeCount() { + return false; + } + + @Override + public BehaviorBriefModel getBehaviorBriefResult( + DataCollector dataStatistics) { + return null; + } + + @Override + public void compile(ArrayList code, + Stack conditions) { + } + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java similarity index 68% rename from Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java rename to Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java index 40633f51..47ee4d7c 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java @@ -1,9 +1,10 @@ -package org.bench4q.agent.scenario.behavior; +package org.bench4q.agent.scenario.node; import org.bench4q.agent.datacollector.DataCollector; import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; -public class ControlBehavior extends Behavior { +public abstract class ConditionedBehavior extends Behavior { + protected ConditionBehavior condition; @Override public boolean shouldBeCount() { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ControlBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ControlBehavior.java new file mode 100644 index 00000000..01b3a24a --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ControlBehavior.java @@ -0,0 +1,42 @@ +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.ControlInstruction; +import org.bench4q.agent.scenario.instruction.Instruction; +import org.bench4q.agent.scenario.instruction.TestInstruction; +import org.bench4q.share.exception.Bench4QRunTimeException; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; + +public class ControlBehavior extends PluginImplementBehavior { + + public ControlBehavior(BehaviorModel behaviorModel) { + if (behaviorModel.getType() != Behavior.CONTROL_BEHAVIOR) { + throw new Bench4QRunTimeException( + "Not the right type, here we need CONTROL_BEHAVIOR, but it is" + + behaviorModel.getType()); + } + buildAndGenerateParameterPart(behaviorModel); + } + + @Override + public boolean shouldBeCount() { + return false; + } + + @Override + public BehaviorBriefModel getBehaviorBriefResult( + DataCollector dataStatistics) { + return null; + } + + @Override + public void compile(ArrayList code, + Stack conditions) { + code.add(new ControlInstruction(this.getUse(), this.getName(), this + .getParameters())); + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/IfBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/IfBehavior.java new file mode 100644 index 00000000..9f1b2578 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/IfBehavior.java @@ -0,0 +1,57 @@ +package org.bench4q.agent.scenario.node; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +import org.bench4q.agent.scenario.instruction.GotoInstruction; +import org.bench4q.agent.scenario.instruction.Instruction; +import org.bench4q.agent.scenario.instruction.TestInstruction; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.scriptrecord.IfBehaviorModel; + +public class IfBehavior extends ConditionedBehavior { + private List thenBehaviors; + private List elseBehaviors; + + public IfBehavior(IfBehaviorModel model) { + this.thenBehaviors = new ArrayList(); + this.elseBehaviors = new ArrayList(); + this.condition = new ConditionBehavior(model.getCondition()); + for (BehaviorModel then : model.getThenBehaviors()) { + this.thenBehaviors.add(Behavior.buildWith(then)); + } + + for (BehaviorModel elseModel : model.getElseBehaviors()) { + this.elseBehaviors.add(Behavior.buildWith(elseModel)); + } + } + + /** + * both code and conditions are result + */ + @Override + public void compile(ArrayList code, + Stack conditions) { + TestInstruction testInstruction; + GotoInstruction gotoInstruction; + condition.checkCondition(code, conditions); + + code.add(testInstruction = new TestInstruction(condition.getUse(), + condition.getName(), condition.getParameters())); + + for (Behavior behavior : thenBehaviors) { + behavior.compile(code, conditions); + } + if (this.thenBehaviors.size() != 0) { + code.add(gotoInstruction = new GotoInstruction(0)); + testInstruction.setFalseLabel(code.size()); + for (Behavior behavior : this.elseBehaviors) { + behavior.compile(code, conditions); + } + gotoInstruction.label = code.size(); + } else { + testInstruction.setFalseLabel(code.size()); + } + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/PluginImplementBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/PluginImplementBehavior.java new file mode 100644 index 00000000..e4f7b313 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/PluginImplementBehavior.java @@ -0,0 +1,25 @@ +package org.bench4q.agent.scenario.node; + +import java.util.ArrayList; +import java.util.Stack; + +import org.bench4q.agent.scenario.instruction.Instruction; +import org.bench4q.agent.scenario.instruction.TestInstruction; + +/** + * This class is the base class of those nodes that will be implemented in + * plugin + * + * This kind of design has some problems that PluginImplementBehavior need not + * extends Behavior + * + * @author chentienan + * + */ +public abstract class PluginImplementBehavior extends Behavior { + + protected void checkCondition(ArrayList code, + Stack conditions) { + code.addAll(conditions); + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TimerBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TimerBehavior.java new file mode 100644 index 00000000..31e1f46e --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TimerBehavior.java @@ -0,0 +1,42 @@ +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.agent.scenario.instruction.TimerInstruction; +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 TimerBehavior(BehaviorModel timerBehaviorModel) { + if (timerBehaviorModel.getType() != Behavior.TIMER_BEHAVIOR) { + throw new Bench4QRunTimeException( + "The type is not expected. Expected is TIMER_BEHAVIOR, but it is " + + timerBehaviorModel.getType()); + } + this.buildAndGenerateParameterPart(timerBehaviorModel); + } + + @Override + public void compile(ArrayList code, + Stack conditions) { + code.add(new TimerInstruction(this.getUse(), this.getName(), this + .getParameters())); + } + + @Override + public boolean shouldBeCount() { + return false; + } + + @Override + public BehaviorBriefModel getBehaviorBriefResult( + DataCollector dataStatistics) { + return null; + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java similarity index 52% rename from Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java rename to Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java index a96d4e48..5d60cd3d 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java @@ -1,9 +1,19 @@ -package org.bench4q.agent.scenario.behavior; +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 + public void compile(ArrayList code, + Stack conditions) { + // TODO: + } @Override public boolean shouldBeCount() { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/UserBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/UserBehavior.java new file mode 100644 index 00000000..43fc23d8 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/UserBehavior.java @@ -0,0 +1,44 @@ +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.SampleInstruction; +import org.bench4q.agent.scenario.instruction.TestInstruction; +import org.bench4q.share.exception.Bench4QRunTimeException; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; + +public class UserBehavior extends PluginImplementBehavior { + + public UserBehavior(BehaviorModel behaviorModel) { + if (behaviorModel.getType() != Behavior.USER_BEHAVIOR) { + throw new Bench4QRunTimeException( + "Not the expected type, expected is USER_BEHAVIOR, but is " + + behaviorModel.getType()); + } + buildAndGenerateParameterPart(behaviorModel); + } + + @Override + public void compile(ArrayList code, + Stack conditions) { + code.add(new SampleInstruction(this.getUse(), this.getName(), this + .getParameters())); + } + + @Override + public boolean shouldBeCount() { + return true; + } + + @Override + public BehaviorBriefModel getBehaviorBriefResult( + DataCollector dataStatistics) { + return (BehaviorBriefModel) dataStatistics + .getBehaviorBriefStatistics(this.getId()); + } + +} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java index 15bb4cdd..e20a28e3 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java @@ -13,9 +13,9 @@ import java.util.UUID; import org.apache.commons.io.FileUtils; import org.bench4q.agent.scenario.Parameter; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.behavior.Behavior; 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.test.TestBase; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.helper.TestHelper; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java index 5dbb124a..9da341f5 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java @@ -9,8 +9,8 @@ import static org.junit.Assert.*; import org.bench4q.agent.plugin.basic.csvprovider.CsvProvider; import org.bench4q.agent.scenario.Scenario; -import org.bench4q.agent.scenario.behavior.Behavior; import org.bench4q.agent.scenario.engine.VUser; +import org.bench4q.agent.scenario.node.Behavior; import org.bench4q.agent.test.TestBase; import org.bench4q.share.helper.TestHelper; import org.bench4q.share.models.agent.ParameterModel; diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/ConditionBehaviorModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/ConditionBehaviorModel.java new file mode 100644 index 00000000..f2dfe09f --- /dev/null +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/ConditionBehaviorModel.java @@ -0,0 +1,5 @@ +package org.bench4q.share.models.agent.scriptrecord; + +public class ConditionBehaviorModel extends BehaviorModel { + +} diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/IfBehaviorModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/IfBehaviorModel.java new file mode 100644 index 00000000..453ca241 --- /dev/null +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/IfBehaviorModel.java @@ -0,0 +1,44 @@ +package org.bench4q.share.models.agent.scriptrecord; + +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class IfBehaviorModel extends BehaviorModel { + private ConditionBehaviorModel condition; + private List thenBehaviors; + private List elseBehaviors; + + @XmlElement + public ConditionBehaviorModel getCondition() { + return condition; + } + + public void setCondition(ConditionBehaviorModel condition) { + this.condition = condition; + } + + @XmlElementWrapper(name = "thenBehaviors") + @XmlElement + public List getThenBehaviors() { + return thenBehaviors; + } + + public void setThenBehaviors(List thenBehaviors) { + this.thenBehaviors = thenBehaviors; + } + + @XmlElementWrapper(name = "elseBehaviors") + @XmlElement + public List getElseBehaviors() { + return elseBehaviors; + } + + public void setElseBehaviors(List elseBehaviors) { + this.elseBehaviors = elseBehaviors; + } + +} diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/refactor/BehaviorModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/refactor/BehaviorModel.java deleted file mode 100644 index 26c4cb52..00000000 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/refactor/BehaviorModel.java +++ /dev/null @@ -1,93 +0,0 @@ -package org.bench4q.share.models.agent.scriptrecord.refactor; - -import java.util.List; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; -import javax.xml.bind.annotation.XmlRootElement; - -import org.bench4q.share.models.agent.ParameterModel; - -@XmlRootElement(name = "behaviorModel") -public class BehaviorModel { - private int id; - private String use; - private String name; - private String type; - private List parameters; - - @XmlElement - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - @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; - } - - @XmlElement - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - @XmlElementWrapper(name = "parameters") - @XmlElement(name = "parameter") - public List getParameters() { - return parameters; - } - - public void setParameters(List parameters) { - this.parameters = parameters; - } - - public BehaviorModel() { - } - - public static BehaviorModel TimerBehaviorBuilder(int id, String name, - String use, List parameters) { - BehaviorModel behaviorBaseModel = buildBehaviorModelWithoutType(id, - name, use, parameters); - behaviorBaseModel.setType("TIMERBEHAVIOR"); - return behaviorBaseModel; - } - - private static BehaviorModel buildBehaviorModelWithoutType(int id, - String name, String use, List parameters) { - BehaviorModel behaviorBaseModel = new BehaviorModel(); - behaviorBaseModel.setId(id); - behaviorBaseModel.setName(name); - behaviorBaseModel.setUse(use); - behaviorBaseModel.setParameters(parameters); - return behaviorBaseModel; - } - - public static BehaviorModel UserBehaviorBuilder(int id, String name, - String use, List parameters) { - BehaviorModel behaviorBaseModel = buildBehaviorModelWithoutType(id, - name, use, parameters); - behaviorBaseModel.setType("USERBEHAVIOR"); - return behaviorBaseModel; - } -}