From 29e2bd8edad226bb8f9addc717f841ee0d342871 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Thu, 18 Sep 2014 14:23:40 +0800 Subject: [PATCH 1/3] adding the ifNode to Scenario adding the ifNode to Scenario --- .../org/bench4q/agent/api/TestController.java | 4 +- .../org/bench4q/agent/scenario/Batch.java | 2 +- .../org/bench4q/agent/scenario/Scenario.java | 2 +- .../bench4q/agent/scenario/engine/VUser.java | 2 +- .../scenario/instruction/GotoInstruction.java | 5 + .../scenario/instruction/Instruction.java | 5 + .../scenario/instruction/TestInstruction.java | 7 ++ .../bench4q/agent/scenario/node/Behavior.java | 116 ++++++++++++++++++ .../agent/scenario/node/ConditionNode.java | 5 + .../scenario/node/ConditionedBehavior.java | 20 +++ .../agent/scenario/node/ControlBehavior.java | 24 ++++ .../agent/scenario/node/IfBehavior.java | 13 ++ .../agent/scenario/node/TimerBehavior.java | 23 ++++ .../scenario/node/TransactionBehavior.java | 23 ++++ .../agent/scenario/node/UserBehavior.java | 25 ++++ .../agent/test/scenario/Test_Scenario.java | 2 +- .../agent/test/scenario/Test_VUser.java | 2 +- 17 files changed, 273 insertions(+), 7 deletions(-) create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/GotoInstruction.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/Instruction.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TestInstruction.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionNode.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ControlBehavior.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/IfBehavior.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TimerBehavior.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/UserBehavior.java 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/Scenario.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java index 1bc2ea1a..21a11f78 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 @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.bench4q.agent.scenario.behavior.Behavior; +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; 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/GotoInstruction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/GotoInstruction.java new file mode 100644 index 00000000..506f62ad --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/GotoInstruction.java @@ -0,0 +1,5 @@ +package org.bench4q.agent.scenario.instruction; + +public class GotoInstruction { + +} 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/TestInstruction.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TestInstruction.java new file mode 100644 index 00000000..3a760e04 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TestInstruction.java @@ -0,0 +1,7 @@ +package org.bench4q.agent.scenario.instruction; + +public class TestInstruction implements Instruction { + + public TestInstruction(String pluginName, String behaviorName) { + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java new file mode 100644 index 00000000..190b0611 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java @@ -0,0 +1,116 @@ +package org.bench4q.agent.scenario.node; + +import org.bench4q.agent.datacollector.DataCollector; +import org.bench4q.agent.scenario.Parameter; +import org.bench4q.share.models.agent.ParameterModel; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +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"; + + 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 + */ + + public abstract void compile(); + + protected void baseCompile() { + for (Parameter parameter : this.getParameters()) { + parameter.compileToGenerateParts(); + } + } + + public static Behavior buildWith(BehaviorModel behaviorModel) { + Behavior behavior = null; + if (behaviorModel.getType().equalsIgnoreCase(TIMER_BEHAVIOR)) { + behavior = new TimerBehavior(); + } else if (behaviorModel.getType().equalsIgnoreCase(USER_BEHAVIOR)) { + behavior = new UserBehavior(); + } else if (behaviorModel.getType().equalsIgnoreCase(CONTROL_BEHAVIOR)) { + behavior = new ControlBehavior(); + } else if (behaviorModel.getType().equalsIgnoreCase(TEST_BEHAVIOR)) { + // TODO: refactor this + behavior = new IfBehavior(); + } 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()]); + + for (int k = 0; k < behaviorModel.getParameters().size(); k++) { + ParameterModel parameterModel = behaviorModel.getParameters() + .get(k); + behavior.getParameters()[k] = new Parameter( + parameterModel.getKey(), parameterModel.getValue()); + } + return behavior; + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionNode.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionNode.java new file mode 100644 index 00000000..d784b3cc --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionNode.java @@ -0,0 +1,5 @@ +package org.bench4q.agent.scenario.node; + +public class ConditionNode { + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java new file mode 100644 index 00000000..a06b46f2 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java @@ -0,0 +1,20 @@ +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 ConditionNode condition; + + @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/node/ControlBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ControlBehavior.java new file mode 100644 index 00000000..8fe881ee --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ControlBehavior.java @@ -0,0 +1,24 @@ +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 { + + @Override + public boolean shouldBeCount() { + return false; + } + + @Override + public BehaviorBriefModel getBehaviorBriefResult( + DataCollector dataStatistics) { + return null; + } + + @Override + public void compile() { + this.baseCompile(); + } + +} 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..4e960cc9 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/IfBehavior.java @@ -0,0 +1,13 @@ +package org.bench4q.agent.scenario.node; + +import java.util.List; + +public class IfBehavior extends ConditionedBehavior { + private List thenBehaviors; + private List elseBehaviors; + + @Override + public void compile() { + this.baseCompile(); + } +} 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..fc222707 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TimerBehavior.java @@ -0,0 +1,23 @@ +package org.bench4q.agent.scenario.node; + +import org.bench4q.agent.datacollector.DataCollector; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; + +public class TimerBehavior extends Behavior { + @Override + public void compile() { + this.baseCompile(); + } + + @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/node/TransactionBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java new file mode 100644 index 00000000..66e004a2 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java @@ -0,0 +1,23 @@ +package org.bench4q.agent.scenario.node; + +import org.bench4q.agent.datacollector.DataCollector; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; + +public class TransactionBehavior extends Behavior { + @Override + public void compile() { + this.baseCompile(); + } + + @Override + public boolean shouldBeCount() { + return true; + } + + @Override + public BehaviorBriefModel getBehaviorBriefResult( + DataCollector dataStatistics) { + return null; + } + +} 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..7bf4ac7b --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/UserBehavior.java @@ -0,0 +1,25 @@ +package org.bench4q.agent.scenario.node; + +import org.bench4q.agent.datacollector.DataCollector; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; + +public class UserBehavior extends Behavior { + + @Override + public void compile() { + this.baseCompile(); + } + + @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; From 43d056a19b568bcb1dac7c422eda3b1ec870a093 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Thu, 18 Sep 2014 21:43:03 +0800 Subject: [PATCH 2/3] refactor the engine's running way to instruction running refactor the engine's running way to instruction running --- .../org/bench4q/agent/scenario/Parameter.java | 10 +- .../org/bench4q/agent/scenario/Scenario.java | 7 +- .../agent/scenario/behavior/Behavior.java | 100 ------------------ .../scenario/behavior/ControlBehavior.java | 19 ---- .../agent/scenario/behavior/TestBehavior.java | 19 ---- .../scenario/behavior/TimerBehavior.java | 19 ---- .../behavior/TransactionBehavior.java | 19 ---- .../agent/scenario/behavior/UserBehavior.java | 18 ---- .../instruction/ControlInstruction.java | 11 ++ .../scenario/instruction/GotoInstruction.java | 6 +- .../PluginImplementInstruction.java | 29 +++++ .../instruction/SampleInstruction.java | 12 +++ .../scenario/instruction/TestInstruction.java | 17 ++- .../instruction/TimerInstruction.java | 12 +++ .../bench4q/agent/scenario/node/Behavior.java | 61 +++++++---- .../scenario/node/ConditionBehavior.java | 34 ++++++ .../scenario/node/ConditionedBehavior.java | 2 +- .../agent/scenario/node/ControlBehavior.java | 26 ++++- .../agent/scenario/node/IfBehavior.java | 48 ++++++++- .../node/PluginImplementBehavior.java | 25 +++++ .../agent/scenario/node/TimerBehavior.java | 25 ++++- .../scenario/node/TransactionBehavior.java | 10 +- .../agent/scenario/node/UserBehavior.java | 25 ++++- .../scriptrecord/ConditionBehaviorModel.java | 5 + .../agent/scriptrecord/IfBehaviorModel.java | 44 ++++++++ .../scriptrecord/refactor/BehaviorModel.java | 93 ---------------- 26 files changed, 364 insertions(+), 332 deletions(-) delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/ControlInstruction.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/PluginImplementInstruction.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/SampleInstruction.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/instruction/TimerInstruction.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionBehavior.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/PluginImplementBehavior.java create mode 100644 Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/ConditionBehaviorModel.java create mode 100644 Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/IfBehaviorModel.java delete mode 100644 Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/refactor/BehaviorModel.java 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 21a11f78..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,7 +3,10 @@ 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.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; @@ -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/Behavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java deleted file mode 100644 index c483b69b..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.bench4q.agent.scenario.behavior; - -import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.scenario.Parameter; -import org.bench4q.share.models.agent.ParameterModel; -import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; -import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; - -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"; - - private int id; - private String use; - private String name; - private Parameter[] parameters; - - 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 ""; - } - - public void compile() { - for (Parameter parameter : this.getParameters()) { - parameter.compileToGenerateParts(); - } - } - - public static Behavior buildWith(BehaviorModel behaviorModel) { - Behavior behavior = null; - if (behaviorModel.getType().equalsIgnoreCase(TIMER_BEHAVIOR)) { - behavior = new TimerBehavior(); - } else if (behaviorModel.getType().equalsIgnoreCase(USER_BEHAVIOR)) { - behavior = new UserBehavior(); - } else if (behaviorModel.getType().equalsIgnoreCase(CONTROL_BEHAVIOR)) { - behavior = new ControlBehavior(); - } else if (behaviorModel.getType().equalsIgnoreCase(TEST_BEHAVIOR)) { - behavior = new TestBehavior(); - } 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()]); - - for (int k = 0; k < behaviorModel.getParameters().size(); k++) { - ParameterModel parameterModel = behaviorModel.getParameters() - .get(k); - behavior.getParameters()[k] = new Parameter( - parameterModel.getKey(), parameterModel.getValue()); - } - return behavior; - } -} 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/behavior/ControlBehavior.java deleted file mode 100644 index 40633f51..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.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 ControlBehavior 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/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/TransactionBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java deleted file mode 100644 index a96d4e48..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.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 TransactionBehavior extends Behavior { - - @Override - public boolean shouldBeCount() { - return true; - } - - @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/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 index 506f62ad..2d3e9115 100644 --- 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 @@ -1,5 +1,9 @@ package org.bench4q.agent.scenario.instruction; -public class GotoInstruction { +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/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 index 3a760e04..ee72929c 100644 --- 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 @@ -1,7 +1,20 @@ package org.bench4q.agent.scenario.instruction; -public class TestInstruction implements Instruction { +import org.bench4q.agent.scenario.Parameter; - public TestInstruction(String pluginName, String behaviorName) { +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/node/Behavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java index 190b0611..2b445146 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/Behavior.java @@ -1,9 +1,16 @@ 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; /** @@ -12,10 +19,10 @@ import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; * */ 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; @@ -73,44 +80,52 @@ public abstract class Behavior { /** * No matter which one to inherit from this class, it should call - * baseCompile in its own compile function + * baseCompile in its own compile function. + * + * This function has side effect, the result is both code and conditions */ - public abstract void compile(); - - protected void baseCompile() { - for (Parameter parameter : this.getParameters()) { - parameter.compileToGenerateParts(); - } - } + 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)) { // TODO: refactor this - behavior = new IfBehavior(); + 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/node/ConditionedBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java index a06b46f2..47ee4d7c 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionedBehavior.java @@ -4,7 +4,7 @@ import org.bench4q.agent.datacollector.DataCollector; import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; public abstract class ConditionedBehavior extends Behavior { - protected ConditionNode condition; + 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 index 8fe881ee..01b3a24a 100644 --- 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 @@ -1,9 +1,26 @@ 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 Behavior { +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() { @@ -17,8 +34,9 @@ public class ControlBehavior extends Behavior { } @Override - public void compile() { - this.baseCompile(); + 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 index 4e960cc9..9f1b2578 100644 --- 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 @@ -1,13 +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() { - this.baseCompile(); + 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 index fc222707..31e1f46e 100644 --- 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 @@ -1,12 +1,32 @@ 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() { - this.baseCompile(); + public void compile(ArrayList code, + Stack conditions) { + code.add(new TimerInstruction(this.getUse(), this.getName(), this + .getParameters())); } @Override @@ -19,5 +39,4 @@ public class TimerBehavior extends Behavior { DataCollector dataStatistics) { return null; } - } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java index 66e004a2..5d60cd3d 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/TransactionBehavior.java @@ -1,12 +1,18 @@ 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() { - this.baseCompile(); + public void compile(ArrayList code, + Stack conditions) { + // TODO: } @Override 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 index 7bf4ac7b..43fc23d8 100644 --- 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 @@ -1,13 +1,32 @@ 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 Behavior { +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() { - this.baseCompile(); + public void compile(ArrayList code, + Stack conditions) { + code.add(new SampleInstruction(this.getUse(), this.getName(), this + .getParameters())); } @Override 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; - } -} From cd68f1a03a02ad913920e449d35173a67b827a34 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Thu, 18 Sep 2014 21:43:29 +0800 Subject: [PATCH 3/3] refactor refactor --- .../java/org/bench4q/agent/scenario/node/ConditionNode.java | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionNode.java diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionNode.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionNode.java deleted file mode 100644 index d784b3cc..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/node/ConditionNode.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.bench4q.agent.scenario.node; - -public class ConditionNode { - -}