Skip to content

Commit 22b6a5b

Browse files
Merge pull request #301 from abstracta/UPDATE_CODE_BUILDER_WSC
Add randomChoice to weightedSwitchCotroller Code Builder
2 parents 858329e + 94c015e commit 22b6a5b

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchController.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
* Selects a child in each iteration according to specified relative weights.
3030
* <p>
3131
* Internally this uses <a
32-
* href="https://github.com/Blazemeter/jmeter-bzm-plugins/blob/master/wsc/WeightedSwitchController.md">
32+
* href="https://github.com/Blazemeter/jmeter-bzm-plugins/blob/master/wsc
33+
* /WeightedSwitchController.md">
3334
* BlazeMeter Weighted Switch Controller plugin</a>.
3435
* <p>
3536
* This controller is handy when you want part of the test plan to act in a probabilistic manner
@@ -57,10 +58,10 @@ public DslWeightedSwitchController() {
5758
* Adds a child to the controller with a configured weight for selecting it in iterations.
5859
*
5960
* @param weight is the weight to assign to this particular element for execution in iterations.
60-
* Keep in mind that if you use {@link #children(ThreadGroupChild...)} to add
61-
* samplers or controllers, their default assigned weight will be 100.
62-
* @param child is the element to add as controller child that will be selected for execution
63-
* during iterations according to given weight.
61+
* Keep in mind that if you use {@link #children(ThreadGroupChild...)} to add samplers or
62+
* controllers, their default assigned weight will be 100.
63+
* @param child is the element to add as controller child that will be selected for execution
64+
* during iterations according to given weight.
6465
* @return the controller for further configuration and usage.
6566
*/
6667
public DslWeightedSwitchController child(long weight, DslController child) {
@@ -71,10 +72,10 @@ public DslWeightedSwitchController child(long weight, DslController child) {
7172
* Adds a child to the controller with a configured weight for selecting it in iterations.
7273
*
7374
* @param weight is the weight to assign to this particular element for execution in iterations.
74-
* Keep in mind that if you use {@link #children(ThreadGroupChild...)} to add
75-
* samplers or controllers, their default assigned weight will be 100.
76-
* @param child is the element to add as controller child that will be selected for execution
77-
* during iterations according to given weight.
75+
* Keep in mind that if you use {@link #children(ThreadGroupChild...)} to add samplers or
76+
* controllers, their default assigned weight will be 100.
77+
* @param child is the element to add as controller child that will be selected for execution
78+
* during iterations according to given weight.
7879
* @return the controller for further configuration and usage.
7980
*/
8081
public DslWeightedSwitchController child(long weight, DslSampler child) {
@@ -84,14 +85,17 @@ public DslWeightedSwitchController child(long weight, DslSampler child) {
8485
/**
8586
* Sets the random choice mode for child elements.
8687
* <p>
87-
* When random choice mode is enabled (randomChoice = true), the controller will select child elements
88-
* randomly, according to their weights. When disabled (randomChoice = false), selection will occur
89-
* sequentially based on weights.
88+
* When random choice mode is enabled (randomChoice = true), the controller will select child
89+
* elements randomly, according to their weights. When disabled (randomChoice = false), selection
90+
* will occur sequentially based on weights.
91+
* <p>
92+
* The randomChoice(true) method cannot guarantee that the actual execution percentages of its
93+
* child elements will exactly match the expected distribution.
9094
*
9195
* @param randomChoice true - to enable random selection, false - for sequential selection
9296
* @return current controller instance for method chaining
9397
*/
94-
public DslWeightedSwitchController randomChoice(boolean randomChoice){
98+
public DslWeightedSwitchController randomChoice(boolean randomChoice) {
9599
this.isRandomChoice = randomChoice;
96100
return this;
97101
}
@@ -192,7 +196,10 @@ public CodeBuilder(List<Method> builderMethods) {
192196
@Override
193197
protected MethodCall buildMethodCall(WeightedSwitchController testElement,
194198
MethodCallContext context) {
199+
TestElementParamBuilder paramBuilder = new TestElementParamBuilder(testElement);
195200
MethodCall ret = buildMethodCall();
201+
ret.chain("randomChoice",
202+
paramBuilder.boolParam(WeightedSwitchController.IS_RANDOM_CHOICE, false));
196203
Map<String, Long> weights = extractSamplersWeights(testElement);
197204
chainChildren(ret, context, weights);
198205
return ret;

jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/controllers/DslWeightedSwitchControllerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ public DslTestPlan testPlanWithWeightedSwitchController() {
107107
);
108108
}
109109

110+
public DslTestPlan testPlanWithWeightedSwitchControllerAndRandomChoice() {
111+
return testPlan(
112+
threadGroup(1, 1,
113+
weightedSwitchController()
114+
.randomChoice(true)
115+
.child(2, httpSampler("sample1", "http://localhost"))
116+
.child(3, httpSampler("sample2", "http://localhost"))
117+
)
118+
);
119+
}
120+
110121
public DslTestPlan testPlanWithWeightedSwitchControllerAndUnweightedElements() {
111122
return testPlan(
112123
threadGroup(1, 1,

jmeter-java-dsl/src/test/resources/test-plan.template.jmx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
<stringProp name="HTTPSampler.port">{{port:}}</stringProp>
3636
<stringProp name="HTTPSampler.path">/</stringProp>
3737
<stringProp name="HTTPSampler.method">POST</stringProp>
38-
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
3938
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
39+
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
4040
</HTTPSamplerProxy>
4141
<hashTree>
4242
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager">
@@ -58,8 +58,8 @@
5858
<stringProp name="HTTPSampler.port">{{port:}}</stringProp>
5959
<stringProp name="HTTPSampler.path">/</stringProp>
6060
<stringProp name="HTTPSampler.method">GET</stringProp>
61-
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
6261
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
62+
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
6363
</HTTPSamplerProxy>
6464
<hashTree/>
6565
</hashTree>

0 commit comments

Comments
 (0)