Skip to content

Commit efab62f

Browse files
authored
Merge pull request #153 from andy-tarr/master
add Runtime Controller support
2 parents 216a6c8 + 24043c1 commit efab62f

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/JmeterDsl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import us.abstracta.jmeter.javadsl.core.controllers.DslForEachController;
1515
import us.abstracta.jmeter.javadsl.core.controllers.DslIfController;
1616
import us.abstracta.jmeter.javadsl.core.controllers.DslOnceOnlyController;
17+
import us.abstracta.jmeter.javadsl.core.controllers.DslRuntimeController;
1718
import us.abstracta.jmeter.javadsl.core.controllers.DslTransactionController;
1819
import us.abstracta.jmeter.javadsl.core.controllers.DslWeightedSwitchController;
1920
import us.abstracta.jmeter.javadsl.core.controllers.DslWhileController;
@@ -622,6 +623,25 @@ public static DslForEachController forEachController(String name, String varsPre
622623
public static DslOnceOnlyController onceOnlyController(ThreadGroupChild... children) {
623624
return new DslOnceOnlyController(Arrays.asList(children));
624625
}
626+
/**
627+
* Builds a Runtime Controller that allows it's child elements to be executed for a defined amount of time (seconds)
628+
* per thread group iteration.
629+
* This can be very useful in tailoring a test plan to more accurately the actual behavior of users in a system or
630+
* perhaps control the execution of a set of samplers for a period of time followed by a request to refresh an auth token.
631+
* When the defined execution time of the controller is reached, the runtime controller will stop execution and the next part of the
632+
* test plan will be executed
633+
*@param seconds defines the number of seconds the runtime controller should be executed on
634+
* each iteration. Can be a variable expression or a fixed value.
635+
* @param children contains the test plan elements to execute only one time on first iteration of
636+
* each thread group.
637+
* @return the controller instance for further configuration and usage.
638+
* @see DslRuntimeController
639+
*/
640+
641+
public static DslRuntimeController runtimeController(String seconds,
642+
ThreadGroupChild... children) {
643+
return new DslRuntimeController(seconds, Arrays.asList(children));
644+
}
625645

626646
/**
627647
* Builds a Percent Controller to execute children only a given percent of times.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package us.abstracta.jmeter.javadsl.core.controllers;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.List;
5+
import org.apache.jmeter.control.RunTime;
6+
import org.apache.jmeter.control.gui.RunTimeGui;
7+
import org.apache.jmeter.testelement.TestElement;
8+
import us.abstracta.jmeter.javadsl.codegeneration.MethodCall;
9+
import us.abstracta.jmeter.javadsl.codegeneration.MethodCallContext;
10+
import us.abstracta.jmeter.javadsl.codegeneration.SingleTestElementCallBuilder;
11+
import us.abstracta.jmeter.javadsl.codegeneration.TestElementParamBuilder;
12+
import us.abstracta.jmeter.javadsl.codegeneration.params.ChildrenParam;
13+
import us.abstracta.jmeter.javadsl.core.threadgroups.BaseThreadGroup;
14+
import us.abstracta.jmeter.javadsl.core.threadgroups.BaseThreadGroup.ThreadGroupChild;
15+
16+
/**
17+
* Allows running a part of a test plan repeatedly for a specified number of seconds
18+
* for each iteration of each thread in a thread group.
19+
* <p>
20+
* Internally this uses JMeter Runtime Controller (Runtime class).
21+
*
22+
*/
23+
public class DslRuntimeController extends BaseController<DslRuntimeController> {
24+
25+
protected String seconds;
26+
27+
public DslRuntimeController(String seconds,
28+
List<BaseThreadGroup.ThreadGroupChild> children) {
29+
super("Runtime Controller", RunTimeGui.class, children);
30+
this.seconds = seconds;
31+
}
32+
33+
@Override
34+
protected TestElement buildTestElement() {
35+
RunTime ret = new RunTime();
36+
ret.setRuntime(seconds);
37+
return ret;
38+
}
39+
40+
public static class CodeBuilder extends SingleTestElementCallBuilder<RunTime> {
41+
public CodeBuilder(List<Method> builderMethods) {
42+
super(RunTime.class, builderMethods);
43+
}
44+
45+
@Override
46+
protected MethodCall buildMethodCall(RunTime testElement,
47+
MethodCallContext context) {
48+
TestElementParamBuilder paramBuilder = new TestElementParamBuilder(testElement,
49+
"RuntimeController");
50+
return buildMethodCall(paramBuilder.stringParam("seconds"),
51+
new ChildrenParam<>(ThreadGroupChild[].class));
52+
}
53+
}
54+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package us.abstracta.jmeter.javadsl.core.controllers;
2+
3+
import org.junit.jupiter.api.*;
4+
import us.abstracta.jmeter.javadsl.*;
5+
import us.abstracta.jmeter.javadsl.codegeneration.*;
6+
import us.abstracta.jmeter.javadsl.core.*;
7+
8+
import static org.assertj.core.api.Assertions.*;
9+
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;
10+
11+
public class DslRuntimeControllerTest extends JmeterDslTest {
12+
13+
@Test
14+
public void shouldExecuteMultipleTimesWhenRuntimeControllerInPlan() throws Exception {
15+
TestPlanStats stats = testPlan(
16+
threadGroup(1, 1,
17+
runtimeController ("1",
18+
httpSampler(wiremockUri)
19+
)
20+
)
21+
).run();
22+
assertThat(stats.overall().samplesCount()).isGreaterThan(1);
23+
}
24+
25+
@Nested
26+
public class CodeBuilderTest extends MethodCallBuilderTest {
27+
28+
public DslTestPlan testPlanWithRuntimeController() {
29+
return testPlan(
30+
threadGroup(1, 1,
31+
runtimeController("5",
32+
httpSampler("http://localhost")
33+
)
34+
)
35+
);
36+
}
37+
38+
}
39+
40+
}

0 commit comments

Comments
 (0)