Skip to content

Commit d54f0ff

Browse files
author
kopysov
committed
Add random choice support for WeightedSwitchController
- Added `isRandomChoice()` method to DslWeightedSwitchController to enable random selection of child elements - Added test case to verify random choice functionality
1 parent 02d0ebc commit d54f0ff

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 7 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,11 @@ public DslWeightedSwitchController child(long weight, DslSampler child) {
8081
return addWeightedChild(weight, child);
8182
}
8283

84+
public DslWeightedSwitchController isRandomChoice(){
85+
this.isRandomChoice = true;
86+
return this;
87+
}
88+
8389
private DslWeightedSwitchController addWeightedChild(long weight, ThreadGroupChild child) {
8490
children.add(new WeightedChild(weight, child));
8591
return this;
@@ -144,6 +150,7 @@ public HashTree buildTreeUnder(HashTree parent, BuildTreeContext context) {
144150
}
145151
}
146152
controller.setData(model);
153+
controller.setIsRandomChoice(isRandomChoice);
147154
return ret;
148155
}
149156

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+
.isRandomChoice()
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)