Skip to content

Commit

Permalink
Allow to set increment, max and perThread settings in counters
Browse files Browse the repository at this point in the history
  • Loading branch information
rabelenda-abstracta committed Oct 16, 2023
1 parent 8283081 commit 47db8f6
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
* <p>
* 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;
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
);
}

}

}

0 comments on commit 47db8f6

Please sign in to comment.