Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/guide/request-generation/counter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down