Search code examples
kotlingroovyjmeter

jMeter's JSR223 Sampler escapes quotes using Kotlin


Please don't suggest another Samplers, I really need JSR223, because the script there is much more complex, this is just an example.

I am using jMeter Java API to generate load tests in Kotlin. When I create a JSR223Sampler and add a Groovy script into it like

private fun getJSR223Sampler(): JSR223Sampler {
    val jsr223Sampler = JSR223Sampler()
    jsr223Sampler.setProperty(TestElement.TEST_CLASS, JSR223Sampler::class.java.name)
    jsr223Sampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI::class.java.name)
    jsr223Sampler.name = "Trigger Minio help"
    jsr223Sampler.setProperty("scriptLanguage", "groovy")
    jsr223Sampler.setProperty("cacheKey", "true")
    jsr223Sampler.setProperty("filename", "")
    jsr223Sampler.setProperty("parameters", "")
    val script = """
        def minioHelp = "mc --help";
        def proc = minioHelp.execute();
    """.trimIndent()
    jsr223Sampler.setProperty("script", script)
    return jsr223Sampler
}

When I run this test based on this example

val jmeter = StandardJMeterEngine()
SaveService.saveTree(root, FileOutputStream("loadTest.jmx"))
jmeter.configure(root)
jmeter.run()

generated script inside has escaped quote marks:

  <JSR223Sampler guiclass="TestBeanGUI" testclass="JSR223Sampler" testname="Trigger Minio help">
    <stringProp name="scriptLanguage">groovy</stringProp>
    <stringProp name="cacheKey">true</stringProp>
    <stringProp name="filename"></stringProp>
    <stringProp name="parameters"></stringProp>
    <stringProp name="script">def minioHelp = &quot;mc --help&quot;;
    def proc = minioHelp.execute();</stringProp>
  </JSR223Sampler>

which lead into an error:

"javax.script.ScriptException: java.io.IOException: Cannot run program ""mc"": error=2, No such file or directory"

How can I avoid this behaviour? I have to say that when I import generated JMX file into jMeter, it works perfectly, so problem must be somewhere in jMeter engine used in Java?


Solution

  • JMeter .jmx scripts are XML files and " character is one of 5 which do require escaping. I believe if you create your test plan using JMeter GUI and open the resulting .jmx script using your favourite text/XML editor you will see that quotation marks got escaped as well there.

    Just make sure that your mc (whatever it is) in your operating system PATH and should start working.

    "jMeter engine" works in exactly the same manner: it converts the .jmx script into a HashTree and runs it, no matter whether you use GUI or non-GUI mode