diff --git a/docs/guide/request-generation/counter.md b/docs/guide/request-generation/counter.md index 8abb1ae4..c48f8f3c 100644 --- a/docs/guide/request-generation/counter.md +++ b/docs/guide/request-generation/counter.md @@ -13,5 +13,5 @@ testPlan( ) ).run(); ``` - +You can also use `.resetOnEachIteration(true)` if you need the counter to reset to its starting value at the beginning of each new Thread Group iteration. Check [DslCounter](/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/configs/DslCounter.java) for more details. diff --git a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/configs/DslCounter.java b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/configs/DslCounter.java index 7f53844d..f69b7953 100644 --- a/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/configs/DslCounter.java +++ b/jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/configs/DslCounter.java @@ -24,6 +24,7 @@ public class DslCounter extends BaseConfigElement { private long increment = 1; private long max = Long.MAX_VALUE; private boolean perThread = false; + private boolean resetOnEachIteration = false; public DslCounter(String varName) { super(varName, CounterConfigGui.class); @@ -98,14 +99,35 @@ public DslCounter perThread(boolean perThread) { return this; } + /** + * Specifies whether to reset the counter value at the beginning of each thread group iteration. + * + * @param resetOnEachIteration specifies to reset the counter when a new thread group iteration + * starts. When not specified (set to false), the counter persists its + * value across different iterations. By default, it is set to false. + * @return the counter for further configuration and usage. + * @since 2.3 + */ + public DslCounter resetOnEachIteration(boolean resetOnEachIteration) { + this.resetOnEachIteration = resetOnEachIteration; + return this; + } + @Override protected TestElement buildTestElement() { + if (resetOnEachIteration && !perThread) { + throw new IllegalStateException( + "Invalid counter configuration: resetOnEachIteration(true) only works with perThread(true). A shared " + + "counter (perThread=false) has no per-thread state, so it cannot reset on each iteration." + ); + } CounterConfig ret = new CounterConfig(); ret.setVarName(varName); ret.setStart(start); ret.setIncrement(increment); ret.setEnd(max); ret.setIsPerUser(perThread); + ret.setResetOnThreadGroupIteration(resetOnEachIteration); return ret; } @@ -124,6 +146,7 @@ protected MethodCall buildMethodCall(CounterConfig testElement, MethodCallContex ret.chain("increment", paramBuilder.longParam("incr", 1L)); ret.chain("maximumValue", paramBuilder.longParam("end", Long.MAX_VALUE)); ret.chain("perThread", paramBuilder.boolParam("per_user", false)); + ret.chain("resetOnEachIteration", paramBuilder.boolParam("reset_on_tg_iteration", false)); return ret; } diff --git a/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/configs/DslCounterTest.java b/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/configs/DslCounterTest.java index 7800defa..7bf1db5a 100644 --- a/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/configs/DslCounterTest.java +++ b/jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/configs/DslCounterTest.java @@ -50,6 +50,25 @@ public void shouldUseIncrementalValuesInRequestWhenCounterInRequest() throws Exc verify(threads, getRequestedFor(urlEqualTo("/" + (startingValue + increment)))); } + @Test + public void shouldResetCounterOnEachIterationWhenResetOptionIsEnabled() throws Exception { + int startingValue = 1; + int increment = 1; + int threads = 1; + testPlan( + threadGroup(threads, 2, + counter("MY_COUNTER") + .startingValue(startingValue) + .increment(increment) + .perThread(true) + .resetOnEachIteration(true), + httpSampler(wiremockUri + "/${MY_COUNTER}") + ) + ).run(); + verify(2, getRequestedFor(urlEqualTo("/1"))); + verify(0, getRequestedFor(urlEqualTo("/2"))); + } + @Nested public class CodeBuilderTest extends MethodCallBuilderTest {