Skip to content

Commit e7cbcc9

Browse files
Merge pull request #296 from KopysovDB/master
Add random choice support for WeightedSwitchController
2 parents 7b26213 + b1c567d commit e7cbcc9

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
public class DslWeightedSwitchController extends BaseController<DslWeightedSwitchController> {
4545

4646
public static final long DEFAULT_WEIGHT = 100;
47+
private boolean isRandomChoice = false;
4748

4849
public DslWeightedSwitchController() {
4950
super("Weighted Switch Controller", WeightedSwitchControllerGui.class, new ArrayList<>());
@@ -80,6 +81,21 @@ public DslWeightedSwitchController child(long weight, DslSampler child) {
8081
return addWeightedChild(weight, child);
8182
}
8283

84+
/**
85+
* Sets the random choice mode for child elements.
86+
* <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.
90+
*
91+
* @param randomChoice true - to enable random selection, false - for sequential selection
92+
* @return current controller instance for method chaining
93+
*/
94+
public DslWeightedSwitchController randomChoice(boolean randomChoice){
95+
this.isRandomChoice = randomChoice;
96+
return this;
97+
}
98+
8399
private DslWeightedSwitchController addWeightedChild(long weight, ThreadGroupChild child) {
84100
children.add(new WeightedChild(weight, child));
85101
return this;
@@ -144,6 +160,7 @@ public HashTree buildTreeUnder(HashTree parent, BuildTreeContext context) {
144160
}
145161
}
146162
controller.setData(model);
163+
controller.setIsRandomChoice(isRandomChoice);
147164
return ret;
148165
}
149166

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ public void shouldOnlyExecuteGivenPercentOfTheTimesWhenInPlan() throws Exception
4444
new WeightedLabel(label3, weight2)));
4545
}
4646

47+
@Test
48+
public void shouldHandleRandomChoiceWhenInPlan() throws Exception {
49+
int threads = 1;
50+
int iterations = 20;
51+
long weight1 = 60;
52+
long weight2 = 30;
53+
String label3 = "sample3";
54+
55+
TestPlanStats stats = testPlan(
56+
threadGroup(threads, iterations,
57+
weightedSwitchController()
58+
.child(weight1, httpSampler(SAMPLE_1_LABEL, wiremockUri))
59+
.children(httpSampler(SAMPLE_2_LABEL, wiremockUri))
60+
.child(weight2, httpSampler(label3, wiremockUri))
61+
.randomChoice(true)
62+
)
63+
).run();
64+
65+
Map<String, Long> sampleCounts = buildSampleCountsMap(stats);
66+
assertThat(sampleCounts.keySet()).containsExactlyInAnyOrder(SAMPLE_1_LABEL, SAMPLE_2_LABEL, label3);
67+
}
68+
69+
4770
private Map<String, Long> buildSampleCountsMap(TestPlanStats stats) {
4871
return stats.labels().stream()
4972
.collect(Collectors.toMap(s -> s, s -> stats.byLabel(s).samplesCount()));

0 commit comments

Comments
 (0)