vlsi commented on code in PR #6858:
URL: https://github.com/apache/jmeter/pull/6858#discussion_r4725882779


##########
xdocs/usermanual/build-programmatic-test-plan.xml:
##########
@@ -179,6 +179,41 @@ val root = testTree { // (1)
         </ul>
       </p>
     </section>
+    <section name="Running a test plan" anchor="running_test_plan">
+      <p>
+        The <code>testTree { ... }</code> function builds a test plan tree
+        but does not execute it. To run the plan, configure a
+        <code>StandardJMeterEngine</code> with the tree and start the test.
+      </p>

Review Comment:
   **F1 major** The section says configuring a `StandardJMeterEngine` and 
starting it runs the plan, but the snippet fails with a NullPointerException in 
any program that has not loaded JMeter properties, and writes no JTL file.
   
   Problem: This is the step issue #6295 asks for. A reader who pastes the 
snippet into `main` or into a JUnit test of their own project gets 
`ExecutionException` from `awaitTermination`, and the page names no setup step 
that leads from that error to the fix. `KotlinDslRunTest` passes only because 
`JMeterTestCase` loads `jmeter.properties` in its static initializer.
   
   <details><summary>Reproducer</summary>
   
   ```kotlin
   // standalone main, classpath = ApacheJMeter_core + ApacheJMeter_components 
and their dependencies
   fun main() {
       val tree = testTree { /* the page's snippet, unchanged */ }
       StandardJMeterEngine().apply {
           configure(tree)
           runTest()
           awaitTermination(Duration.ofSeconds(10))
       }
   }
   // current: ExecutionException <- NullPointerException: Cannot invoke 
"java.util.Properties.setProperty(String, String)"
   //          because "org.apache.jmeter.util.JMeterUtils.appProperties" is 
null; target/results.jtl is not created
   // expected: target/results.jtl holds a header and 5 "Kotlin DSL sample" rows
   // JMeterUtils.loadJMeterProperties("<a jmeter.properties file>") before 
testTree makes it pass
   ```
   
   </details>
   
   Proposed solution: Show 
`JMeterUtils.loadJMeterProperties("/path/to/apache-jmeter/bin/jmeter.properties")`
 (with its import) before `testTree` in the sample, and say in the prose that 
the engine reads its settings from a `jmeter.properties` file, so a standalone 
program loads one first: the `bin/jmeter.properties` of a JMeter distribution, 
or a file of its own. F3 covers making the test run that same code.



##########
xdocs/usermanual/build-programmatic-test-plan.xml:
##########
@@ -179,6 +179,41 @@ val root = testTree { // (1)
         </ul>
       </p>
     </section>
+    <section name="Running a test plan" anchor="running_test_plan">
+      <p>
+        The <code>testTree { ... }</code> function builds a test plan tree
+        but does not execute it. To run the plan, configure a
+        <code>StandardJMeterEngine</code> with the tree and start the test.
+      </p>
+
+      <p>
+        A <code>ResultCollector</code> can be added to the plan to write
+        sample results to a JTL file. Wait for the test to finish before
+        reading the results.
+      </p>
+      <source>
+val tree = testTree {

Review Comment:
   **F2 minor** The run sample has no imports, and its `Duration.ofSeconds(10)` 
compiles only against `java.time.Duration`, while the page's next Kotlin sample 
imports `kotlin.time.Duration`.
   
   Problem: A reader who copies the sample has to find the packages of 
`StandardJMeterEngine`, `ResultCollector`, `OpenModelThreadGroup`, and 
`Duration`, and the `Duration` this page imports elsewhere is the wrong one.
   
   ```kotlin
   import kotlin.time.Duration  // as in the page's "Extending Kotlin DSL" 
sample
   // ... the page's snippet ...
           awaitTermination(Duration.ofSeconds(10))
   // current (kotlinc 2.4.20): error: unresolved reference 'ofSeconds'
   // with import java.time.Duration: compiles
   ```
   
   Proposed solution: Open the sample with its imports, as the page's other 
Kotlin samples do: `org.apache.jmeter.engine.StandardJMeterEngine`, 
`org.apache.jmeter.reporters.ResultCollector`, 
`org.apache.jmeter.sampler.DebugSampler`, 
`org.apache.jmeter.testelement.TestPlan`, 
`org.apache.jmeter.threads.openmodel.OpenModelThreadGroup`, 
`org.apache.jmeter.treebuilder.dsl.testTree`, 
`org.apache.jmeter.util.JMeterUtils` (for the setup of F1), and 
`java.time.Duration`.



##########
xdocs/usermanual/build-programmatic-test-plan.xml:
##########
@@ -179,6 +179,41 @@ val root = testTree { // (1)
         </ul>
       </p>
     </section>
+    <section name="Running a test plan" anchor="running_test_plan">
+      <p>
+        The <code>testTree { ... }</code> function builds a test plan tree
+        but does not execute it. To run the plan, configure a
+        <code>StandardJMeterEngine</code> with the tree and start the test.
+      </p>
+
+      <p>
+        A <code>ResultCollector</code> can be added to the plan to write
+        sample results to a JTL file. Wait for the test to finish before
+        reading the results.
+      </p>
+      <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)"

Review Comment:
   **F4 minor** Run with the new instructions, the page's earlier Kotlin plans, 
which use a classic `ThreadGroup` with no thread count and no `LoopController`, 
write a JTL with only its header and report no error.
   
   Problem: The section says how to run "the plan" built with `testTree`, but 
switches from the `ThreadGroup` of "Creating a plan with Kotlin DSL" to 
`OpenModelThreadGroup` without saying why. A reader who adds the engine lines 
to the page's existing sample gets an empty result and no hint of what is 
missing.
   
   <details><summary>Reproducer</summary>
   
   ```kotlin
   val tree = testTree {
       TestPlan::class {
           ResultCollector::class { filename = "results.jtl" }
           ThreadGroup::class {               // the page's "Creating a plan 
with Kotlin DSL" sample
               name = "Search Order Thread Group"
               +DebugSampler::class
           }
       }
   }
   StandardJMeterEngine().apply { configure(tree); runTest(); 
awaitTermination(Duration.ofSeconds(10)) }
   // current: returns normally; results.jtl holds only the CSV header
   // expected: the page says what makes this plan produce samples
   ```
   
   </details>
   
   Proposed solution: State in the section what a classic `ThreadGroup` needs 
before it produces samples (a thread count and a `LoopController` as its main 
controller) and why the sample uses `OpenModelThreadGroup`, or make the earlier 
samples set them so each runs as written.



##########
xdocs/usermanual/build-programmatic-test-plan.xml:
##########
@@ -179,6 +179,41 @@ val root = testTree { // (1)
         </ul>
       </p>
     </section>
+    <section name="Running a test plan" anchor="running_test_plan">
+      <p>
+        The <code>testTree { ... }</code> function builds a test plan tree
+        but does not execute it. To run the plan, configure a
+        <code>StandardJMeterEngine</code> with the tree and start the test.
+      </p>
+
+      <p>
+        A <code>ResultCollector</code> can be added to the plan to write
+        sample results to a JTL file. Wait for the test to finish before
+        reading the results.
+      </p>

Review Comment:
   **F5 minor** The section says to wait for the test to finish but not that 
`awaitTermination` throws `TimeoutException` when the plan outlasts the 
duration, and leaves the test running.
   
   Problem: The 10-second value fits only this 2-second schedule, and it is the 
one value a reader must size to their own plan. A reader with a longer plan 
gets `TimeoutException` while the engine keeps sampling, so code after the call 
reads a partial JTL file.
   
   Proposed solution: Say in the paragraph that the duration must exceed the 
plan's run time, that `awaitTermination` throws `TimeoutException` without 
stopping the test, and that `stopTest()` stops it. The test can stop the engine 
in a `finally` the same way.



##########
src/components/src/test/kotlin/org/apache/jmeter/dsl/KotlinDslRunTest.kt:
##########
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jmeter.dsl
+
+import org.apache.jmeter.engine.StandardJMeterEngine
+import org.apache.jmeter.junit.JMeterTestCase
+import org.apache.jmeter.reporters.ResultCollector
+import org.apache.jmeter.sampler.DebugSampler
+import org.apache.jmeter.testelement.TestPlan
+import org.apache.jmeter.threads.openmodel.OpenModelThreadGroup
+import org.apache.jmeter.treebuilder.dsl.testTree
+import org.apache.jorphan.test.JMeterSerialTest
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.io.TempDir
+import java.nio.file.Files
+import java.nio.file.Path
+import java.time.Duration
+
+class KotlinDslRunTest : JMeterTestCase(), JMeterSerialTest {
+    @TempDir
+    lateinit var tempDir: Path
+
+    @Test
+    fun `run Kotlin DSL plan and write results`() {

Review Comment:
   **F10 nit** The test name `run Kotlin DSL plan and write results` states the 
steps, not the outcome the test checks.
   
   Proposed solution: Name what the new assertion establishes.
   
   ```suggestion
       fun `a ResultCollector in a Kotlin DSL plan writes one JTL row per 
sample`() {
   ```



##########
src/components/src/test/kotlin/org/apache/jmeter/dsl/KotlinDslRunTest.kt:
##########
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jmeter.dsl
+
+import org.apache.jmeter.engine.StandardJMeterEngine
+import org.apache.jmeter.junit.JMeterTestCase
+import org.apache.jmeter.reporters.ResultCollector
+import org.apache.jmeter.sampler.DebugSampler
+import org.apache.jmeter.testelement.TestPlan
+import org.apache.jmeter.threads.openmodel.OpenModelThreadGroup
+import org.apache.jmeter.treebuilder.dsl.testTree
+import org.apache.jorphan.test.JMeterSerialTest
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.io.TempDir
+import java.nio.file.Files
+import java.nio.file.Path
+import java.time.Duration
+
+class KotlinDslRunTest : JMeterTestCase(), JMeterSerialTest {

Review Comment:
   **F3 minor** Nothing ties KotlinDslRunTest to the page's sample: the 
`<source>` block is a hand copy, and the test inherits setup from 
`JMeterTestCase` that the sample does not show, so the test stays green while 
the page's code fails.
   
   Problem: The first review asked to keep the sample and the test in sync, and 
the test is what lets the docs claim verified steps. F1 and F2 are defects of 
the page that this test cannot see.
   
   Proposed solution: Make one copy the source of the other, and run it without 
what `JMeterTestCase` prepares: for example a test that extracts the `<source>` 
block of section `running_test_plan` and compares it, whitespace-normalized, 
with the code the test runs, where that code performs the page's setup itself 
in a JVM no other test initialized. If the test is not meant to keep the page 
runnable, drop it: `OpenModelThreadGroupConfigElementTest` already runs this 
plan through the engine.



##########
xdocs/usermanual/build-programmatic-test-plan.xml:
##########
@@ -179,6 +179,41 @@ val root = testTree { // (1)
         </ul>
       </p>
     </section>
+    <section name="Running a test plan" anchor="running_test_plan">
+      <p>
+        The <code>testTree { ... }</code> function builds a test plan tree
+        but does not execute it. To run the plan, configure a
+        <code>StandardJMeterEngine</code> with the tree and start the test.
+      </p>
+
+      <p>
+        A <code>ResultCollector</code> can be added to the plan to write
+        sample results to a JTL file. Wait for the test to finish before
+        reading the results.
+      </p>
+      <source>
+val tree = testTree {
+    TestPlan::class {
+        ResultCollector::class {
+            filename = "target/results.jtl"
+        }

Review Comment:
   **F8 nit** The results paragraph shows `filename` but not that a relative 
path resolves against the working directory, or that a second run appends to an 
existing file.
   
   Problem: #6295 names the results path as unclear. A reader who runs from an 
IDE gets `target/results.jtl` under whatever working directory the IDE picked, 
and a rerun adds rows after the old ones, so the file mixes runs.
   
   Proposed solution: Add to the paragraph that a relative `filename` resolves 
against the working directory and that an existing file is appended to.



##########
src/components/src/test/kotlin/org/apache/jmeter/dsl/KotlinDslRunTest.kt:
##########
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jmeter.dsl
+
+import org.apache.jmeter.engine.StandardJMeterEngine
+import org.apache.jmeter.junit.JMeterTestCase
+import org.apache.jmeter.reporters.ResultCollector
+import org.apache.jmeter.sampler.DebugSampler
+import org.apache.jmeter.testelement.TestPlan
+import org.apache.jmeter.threads.openmodel.OpenModelThreadGroup
+import org.apache.jmeter.treebuilder.dsl.testTree
+import org.apache.jorphan.test.JMeterSerialTest
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.io.TempDir
+import java.nio.file.Files
+import java.nio.file.Path
+import java.time.Duration
+
+class KotlinDslRunTest : JMeterTestCase(), JMeterSerialTest {
+    @TempDir
+    lateinit var tempDir: Path
+
+    @Test
+    fun `run Kotlin DSL plan and write results`() {
+        val jtlFile = tempDir.resolve("results.jtl")
+
+        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

Review Comment:
   **F9 nit** The comment says "5 requests", but a `DebugSampler` sends no 
request; the rows it produces are samples.
   
   Proposed solution: Change the docs copy at 
build-programmatic-test-plan.xml:202 the same way.
   
   ```suggestion
                       // 5 samples within 100 ms, then a pause so all the 
threads can finish
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to