Skip to content

#6295 Document running Kotlin DSL test plans - #6858

Open
orimarko wants to merge 3 commits into
apache:masterfrom
orimarko:gh6295-kotlin-dsl-example
Open

orimarko wants to merge 3 commits into
apache:masterfrom
orimarko:gh6295-kotlin-dsl-example

Conversation

@orimarko

@orimarko orimarko commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

Document how to run a Kotlin DSL test plan

Motivation and Context

Part of #6295

Document how to execute a test plan built with the Kotlin DSL and write
sample results to a JTL file. Add a test that builds and runs a plan,
waits for completion, and checks the generated results.

How Has This Been Tested?

./gradlew :src:components:test --tests "*KotlinDslRunTest"

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the code style of this project.
  • I have updated the documentation accordingly.

@milamberspace milamberspace left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on #6295, documenting how to actually run a DSL-built plan is a real gap in this page.

Main points

  1. The new doc section has no code sample. Every other section of this page comes with a <source> block, and the issue reporter explicitly asked for the equivalent of the Java DSL jtlWriter(...) example. As it stands, readers still have to guess configure / runTest / awaitTermination and how to set the JTL path, which is what the issue complained about. See the inline suggestion.
  2. The test largely duplicates OpenModelThreadGroupConfigElementTest (same plan, same scheduleString, same DebugSampler, same engine/await sequence); only the ResultCollector and the JTL check are new. Its main value would be to guarantee that the documented snippet compiles and runs, so I'd suggest keeping the doc snippet and the test code in sync (same code, same idioms). Alternatively, the JTL assertions could be added to the existing test. The threads.openmodel package is also a slightly odd home for a test whose purpose is the DSL usage example.

Minor

  • The JTL check results.contains("Kotlin DSL sample") passes as soon as a single line is written; asserting 5 data lines (plus header) would verify that all samples were persisted.
  • The blank line that used to separate the two sections ended up inside the new section; there's now no blank line between </section> and <section name="Extending Kotlin DSL">.

@orimarko orimarko left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@milamberspace Thank you for reviewing, I fixed the issues and move test to package kotlin/org/apache/jmeter/dsl

@milamberspace milamberspace left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! The documentation now has a proper code sample, the test lives in a better place, and the snippet uses the idiomatic DSL style.

Two things are still needed, and one is a regression introduced by keeping the test in sync with the docs (my earlier wording probably encouraged that, sorry):

  1. The test no longer asserts anything. All the assertions (number of samples, JTL file content) were removed, so the test would pass even if no sample was run and no file was written. The plan-building code can stay identical to the documentation, but the assertions should come back after it.
  2. The test writes into the source tree. filename = "target/results.jtl" is relative, and Gradle runs the tests from the module directory, so each run creates src/components/target/results.jtl. The repository .gitignore only ignores the top-level /target/, so that file shows up as untracked for contributors. The documentation can keep target/results.jtl, but the test should use a @TempDir.

See the inline comment for a version of the test I ran locally against this branch (passes, and leaves nothing behind).

  1. Minor: the indentation of the <source> block in the documentation is inconsistent (nested blocks are misaligned, and the block starts with 8 spaces whereas the other samples of the page start at column 0). There is a suggestion inline.

val tree = testTree {
TestPlan::class {
ResultCollector::class {
filename = "target/results.jtl"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relative path makes each test run create src/components/target/results.jtl (not covered by the repository .gitignore), and nothing checks the result anymore. Suggested version, keeping the plan identical to the documentation but writing to a temporary directory and asserting the JTL content:

class KotlinDslRunTest : JMeterTestCase(), JMeterSerialTest {
    @TempDir
    lateinit var tempDir: Path

    @Test
    fun `run Kotlin DSL plan and write results`() {
        val jtlFile = tempDir.resolve("results.jtl")

        // Same code as in build-programmatic-test-plan.xml, except for the JTL location
        val tree = testTree {
            TestPlan::class {
                ResultCollector::class {
                    filename = jtlFile.toString()
                }
                OpenModelThreadGroup::class {
                    name = "Thread Group"
                    // 5 requests within 100 ms, then a pause so all the threads can finish
                    scheduleString = "rate(50 / sec) random_arrivals(100 ms) pause(2 s)"
                    DebugSampler::class {
                        name = "Kotlin DSL sample"
                    }
                }
            }
        }
        StandardJMeterEngine().apply {
            configure(tree)
            runTest()
            awaitTermination(Duration.ofSeconds(10))
        }

        assertEquals(
            5,
            Files.readAllLines(jtlFile).count { it.contains("Kotlin DSL sample") },
            "The JTL file should contain the 5 samples"
        )
    }
}

(needs org.junit.jupiter.api.Assertions.assertEquals, org.junit.jupiter.api.io.TempDir, java.nio.file.Files and java.nio.file.Path imports)

Comment on lines +194 to +214
<source>
val tree = testTree {
TestPlan::class {
ResultCollector::class {
filename = "target/results.jtl"
}
OpenModelThreadGroup::class {
name = "Thread Group"
// 5 requests within 100 ms, then a pause so all the threads can finish
scheduleString = "rate(50 / sec) random_arrivals(100 ms) pause(2 s)"
DebugSampler::class {
name = "Kotlin DSL sample"
}
}
}
}
StandardJMeterEngine().apply {
configure(tree)
runTest()
awaitTermination(Duration.ofSeconds(10))
}</source>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation fix, aligned with the other samples of this page:

Suggested change
<source>
val tree = testTree {
TestPlan::class {
ResultCollector::class {
filename = "target/results.jtl"
}
OpenModelThreadGroup::class {
name = "Thread Group"
// 5 requests within 100 ms, then a pause so all the threads can finish
scheduleString = "rate(50 / sec) random_arrivals(100 ms) pause(2 s)"
DebugSampler::class {
name = "Kotlin DSL sample"
}
}
}
}
StandardJMeterEngine().apply {
configure(tree)
runTest()
awaitTermination(Duration.ofSeconds(10))
}</source>
<source>
val tree = testTree {
TestPlan::class {
ResultCollector::class {
filename = "target/results.jtl"
}
OpenModelThreadGroup::class {
name = "Thread Group"
// 5 requests within 100 ms, then a pause so all the threads can finish
scheduleString = "rate(50 / sec) random_arrivals(100 ms) pause(2 s)"
DebugSampler::class {
name = "Kotlin DSL sample"
}
}
}
}
StandardJMeterEngine().apply {
configure(tree)
runTest()
awaitTermination(Duration.ofSeconds(10))
}</source>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants