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 1e6e62c9..7f53844d 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 @@ -21,6 +21,9 @@ public class DslCounter extends BaseConfigElement { private final String varName; private String start = "0"; + private long increment = 1; + private long max = Long.MAX_VALUE; + private boolean perThread = false; public DslCounter(String varName) { super(varName, CounterConfigGui.class); @@ -54,12 +57,55 @@ public DslCounter startingValue(String start) { return this; } + /** + * Specifies how much the counter will increase in each iteration. + * + * @param inc specifies how much to increase the counter in each iteration. By default, 1. + * @return the counter for further configuration and usage. + * @since 1.22 + */ + public DslCounter increment(long inc) { + this.increment = inc; + return this; + } + + /** + * Specifies the maximum value of the counter. + *

+ * When the value exceeds this value, the counter is reset to its starting value. + * + * @param max specifies the maximum value to use. When not specified, {@link Long#MAX_VALUE} is + * used. + * @return the counter for further configuration and usage. + * @since 1.22 + */ + public DslCounter maximumValue(long max) { + this.max = max; + return this; + } + + /** + * Specifies to use a separate counter for each thread. + * + * @param perThread specifies to use a separate counter for each thread. When not specified, then + * the counter is shared, and incremented, by all thread group threads. By + * default, it is set to false. + * @return the counter for further configuration and usage. + * @since 1.22 + */ + public DslCounter perThread(boolean perThread) { + this.perThread = perThread; + return this; + } + @Override protected TestElement buildTestElement() { CounterConfig ret = new CounterConfig(); ret.setVarName(varName); ret.setStart(start); - ret.setIncrement(1); + ret.setIncrement(increment); + ret.setEnd(max); + ret.setIsPerUser(perThread); return ret; } @@ -75,6 +121,9 @@ protected MethodCall buildMethodCall(CounterConfig testElement, MethodCallContex "CounterConfig"); MethodCall ret = buildMethodCall(paramBuilder.stringParam("name")); ret.chain("startingValue", paramBuilder.longParam("start", 0L)); + ret.chain("increment", paramBuilder.longParam("incr", 1L)); + ret.chain("maximumValue", paramBuilder.longParam("end", Long.MAX_VALUE)); + ret.chain("perThread", paramBuilder.boolParam("per_user", 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 19d34056..7800defa 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 @@ -1,25 +1,74 @@ package us.abstracta.jmeter.javadsl.core.configs; -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static us.abstracta.jmeter.javadsl.JmeterDsl.*; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.verify; +import static us.abstracta.jmeter.javadsl.JmeterDsl.counter; +import static us.abstracta.jmeter.javadsl.JmeterDsl.httpSampler; +import static us.abstracta.jmeter.javadsl.JmeterDsl.testPlan; +import static us.abstracta.jmeter.javadsl.JmeterDsl.threadGroup; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import us.abstracta.jmeter.javadsl.JmeterDslTest; +import us.abstracta.jmeter.javadsl.codegeneration.MethodCallBuilderTest; +import us.abstracta.jmeter.javadsl.core.DslTestPlan; public class DslCounterTest extends JmeterDslTest { + @Test + public void shouldUseIncrementalValuesInRequestWhenSharedCounterInRequest() throws Exception { + int threads = 2; + int iterations = 2; + testPlan( + threadGroup(threads, iterations, + counter("USER_ID"), + httpSampler(wiremockUri + "/${USER_ID}") + ) + ).run(); + for (int i = 0; i < threads * iterations; i++) { + verify(getRequestedFor(urlEqualTo("/" + i))); + } + } + @Test public void shouldUseIncrementalValuesInRequestWhenCounterInRequest() throws Exception { int startingValue = 1; + int increment = 2; + int threads = 2; testPlan( - threadGroup(1, 2, + threadGroup(threads, 3, counter("USER_ID") - .startingValue(startingValue), + .startingValue(startingValue) + .increment(increment) + .maximumValue(startingValue + increment) + .perThread(true), httpSampler(wiremockUri + "/${USER_ID}") ) ).run(); - verify(getRequestedFor(urlEqualTo("/" + startingValue))); - verify(getRequestedFor(urlEqualTo("/" + (startingValue + 1)))); + verify(threads * 2, getRequestedFor(urlEqualTo("/" + startingValue))); + verify(threads, getRequestedFor(urlEqualTo("/" + (startingValue + increment)))); + } + + @Nested + public class CodeBuilderTest extends MethodCallBuilderTest { + + public DslTestPlan testPlanWithDefaultCounter() { + return testPlan( + counter("MY_COUNTER") + ); + } + + public DslTestPlan testPlanWithCustomizedCounter() { + return testPlan( + counter("MY_COUNTER") + .startingValue(1) + .increment(2) + .maximumValue(10) + .perThread(true) + ); + } + } }