diff --git a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchController.java b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchController.java index 9081d56d..5be3351f 100644 --- a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchController.java +++ b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchController.java @@ -29,7 +29,8 @@ * Selects a child in each iteration according to specified relative weights. *

* Internally this uses + * href="https://github.com/Blazemeter/jmeter-bzm-plugins/blob/master/wsc + * /WeightedSwitchController.md"> * BlazeMeter Weighted Switch Controller plugin. *

* This controller is handy when you want part of the test plan to act in a probabilistic manner @@ -57,10 +58,10 @@ public DslWeightedSwitchController() { * Adds a child to the controller with a configured weight for selecting it in iterations. * * @param weight is the weight to assign to this particular element for execution in iterations. - * Keep in mind that if you use {@link #children(ThreadGroupChild...)} to add - * samplers or controllers, their default assigned weight will be 100. - * @param child is the element to add as controller child that will be selected for execution - * during iterations according to given weight. + * Keep in mind that if you use {@link #children(ThreadGroupChild...)} to add samplers or + * controllers, their default assigned weight will be 100. + * @param child is the element to add as controller child that will be selected for execution + * during iterations according to given weight. * @return the controller for further configuration and usage. */ public DslWeightedSwitchController child(long weight, DslController child) { @@ -71,10 +72,10 @@ public DslWeightedSwitchController child(long weight, DslController child) { * Adds a child to the controller with a configured weight for selecting it in iterations. * * @param weight is the weight to assign to this particular element for execution in iterations. - * Keep in mind that if you use {@link #children(ThreadGroupChild...)} to add - * samplers or controllers, their default assigned weight will be 100. - * @param child is the element to add as controller child that will be selected for execution - * during iterations according to given weight. + * Keep in mind that if you use {@link #children(ThreadGroupChild...)} to add samplers or + * controllers, their default assigned weight will be 100. + * @param child is the element to add as controller child that will be selected for execution + * during iterations according to given weight. * @return the controller for further configuration and usage. */ public DslWeightedSwitchController child(long weight, DslSampler child) { @@ -84,14 +85,17 @@ public DslWeightedSwitchController child(long weight, DslSampler child) { /** * Sets the random choice mode for child elements. *

- * When random choice mode is enabled (randomChoice = true), the controller will select child elements - * randomly, according to their weights. When disabled (randomChoice = false), selection will occur - * sequentially based on weights. + * When random choice mode is enabled (randomChoice = true), the controller will select child + * elements randomly, according to their weights. When disabled (randomChoice = false), selection + * will occur sequentially based on weights. + *

+ * The randomChoice(true) method cannot guarantee that the actual execution percentages of its + * child elements will exactly match the expected distribution. * * @param randomChoice true - to enable random selection, false - for sequential selection * @return current controller instance for method chaining */ - public DslWeightedSwitchController randomChoice(boolean randomChoice){ + public DslWeightedSwitchController randomChoice(boolean randomChoice) { this.isRandomChoice = randomChoice; return this; } @@ -192,7 +196,10 @@ public CodeBuilder(List builderMethods) { @Override protected MethodCall buildMethodCall(WeightedSwitchController testElement, MethodCallContext context) { + TestElementParamBuilder paramBuilder = new TestElementParamBuilder(testElement); MethodCall ret = buildMethodCall(); + ret.chain("randomChoice", + paramBuilder.boolParam(WeightedSwitchController.IS_RANDOM_CHOICE, false)); Map weights = extractSamplersWeights(testElement); chainChildren(ret, context, weights); return ret; diff --git a/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchControllerTest.java b/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchControllerTest.java index 5d6b3946..defdf4f2 100644 --- a/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchControllerTest.java +++ b/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchControllerTest.java @@ -107,6 +107,17 @@ public DslTestPlan testPlanWithWeightedSwitchController() { ); } + public DslTestPlan testPlanWithWeightedSwitchControllerAndRandomChoice() { + return testPlan( + threadGroup(1, 1, + weightedSwitchController() + .randomChoice(true) + .child(2, httpSampler("sample1", "http://localhost")) + .child(3, httpSampler("sample2", "http://localhost")) + ) + ); + } + public DslTestPlan testPlanWithWeightedSwitchControllerAndUnweightedElements() { return testPlan( threadGroup(1, 1, diff --git a/jmeter-java-dsl/src/test/resources/test-plan.template.jmx b/jmeter-java-dsl/src/test/resources/test-plan.template.jmx index df8dc557..073d8edb 100644 --- a/jmeter-java-dsl/src/test/resources/test-plan.template.jmx +++ b/jmeter-java-dsl/src/test/resources/test-plan.template.jmx @@ -35,8 +35,8 @@ {{port:}} / POST - true true + true @@ -58,8 +58,8 @@ {{port:}} / GET - true true + true