Building a Test Plan Programmatically
|
JMeter 5.6 brings experimental classes and methods to build test plans programmatically, so please feel free to provide your feedback.
|
In this section, you will learn how to create a Test Plan with JMeter APIs. The Test Plan is a collection of elements arranged in a tree-like manner. However, in JMeter APIs, the elements do not form a tree.
Parent-child relationships are stored in a separate structure: ListedHashTree.
|
|
Extending Kotlin DSL
|
As you use the DSL for test plan generation, you might want to factor out the common patterns.
For instance, imagine you want factor out Thread Group creation so it always has a Summariser element.
 |
 |
 |
 |
import kotlin.time.Duration.Companion.seconds
import org.apache.jmeter.sampler.DebugSampler
import org.apache.jmeter.testelement.TestPlan
import org.apache.jmeter.threads.ThreadGroup
import org.apache.jmeter.treebuilder.dsl.testTree
fun TreeBuilder.threadGroup( // (1)
name: String,
numThreads: Int = 10,
rampUp: Duration = 3.seconds,
body: Action<ThreadGroup>
) {
ThreadGroup::class { // (2)
this.name = name
this.numThreads = numThreads
this.rampUp = rampUp.inWholeSeconds.toInt()
+Summariser::class
body(this) // (3)
}
}
fun buildTree() {
val root = testTree {
TestPlan::class {
threadGroup(name = "Search Order Thread Group", rampUp = 1.seconds) { // (4)
+DebugSampler::class
}
}
} |
 |
 |
 |
 |
- Firstly, you can factor test element creation logic as an extension function for TreeBuilder as in (1).
It uses regular DSL to add an element (see (2)), and then it calls the lambda body in (3) to fill thread group children.
- You can use the extension by calling it when you need it in the test plan, see (4)
- Note how named parameters, and default values keep the code readable
|
|
Creating a plan with Java DSL
|
JMeter 5.6 introduces Java DSL which might make it easier to create and maintain test plans as the structure of the code
would resemble the structure of the generated test plan tree
 |
 |
 |
 |
import org.apache.jmeter.sampler.DebugSampler
import org.apache.jmeter.testelement.TestPlan
import org.apache.jmeter.threads.ThreadGroup
import static org.apache.jmeter.treebuilder.dsl.TreeBuilders.testTree
ListedHashTree root = testTree(b -> { // (1)
b.add(TestPlan.class, tp -> { // (2)
b.add(ThreadGroup.class, tg -> {
tg.setName("Search Order Thread Group");
b.add(DebugSampler.class); // (3)
b.add(new DebugSampler()); // (4)
});
});
}); |
 |
 |
 |
 |
- Firstly, we create a TreeBuilder at (1).
Note how this builder reference should be used to append all the elements
- Then we add elements to the tree in (2), and populate its children.
The lambda parameters correspond to the added elements, so you can configure their properties
- Note how adding element returns the subtree, so we add threadGroup under testPlan in (2)
- If no children needed, you could omit the lambda parameter as in (3)
- By default, JMeter uses no-argument constructors to create elements, however, you can add TestElement instances to the tree as well, see (4)
|
|
|