JSR / groovy get crazy when using ${__counter} in commented out line
piotr.mirek (Bug 66265): When adding this line error, even it's commented out.
code used:
log.info("DEBUG: A: " + ${__threadNum} + "-" + vars.getIteration()); // log.info("DEBUG: B " + ${__threadNum} + "-" + ${__counter});
Is there any way to ignore commented lines to avoid confusion?
Created attachment jmeter.bug.__coutner.JPG: screenshot
Severity: normal OS: All
@FSchumacher (migrated from Bugzilla): To answer your question, no, I don't know a way to ignore those lines.
But, I think this error can be avoided altogether, when you stop to use the ${...} JMeter feature on JSR223 (or specifically Groovy) scripts.
The reason to not use it:
- It is not needed in Groovy code
- JMeter will use the ${...} like a template to generate the actual JSR223 code and use it for compilation/interpretation. If the code stays the same throughout the test, the cache is more effective and uses less RAM
- It is confusing, as Groovy uses the same syntax for variable interpolation
And you omitted an exception, that you will likely have received in the log files. __counter is a function, that has to be called with a parameter.
A combination of those things lead the the exception.
You can pass the threadNum as a parameter/argument to the sampler and get rid of the templating for threadNum. Same can probably done for counter (using it with a parameter). That can be combined with log format strings to
log.info("DEBUG: A: {}-{}", args[0], args[1]);
Add "${__threadNum} ${__counter(TRUE)}" to the "Parameters" field to fill the args variable.
@FSchumacher (migrated from Bugzilla): After sending the last comment, it struck me, that the problem here is, that the missing argument to counter lets JMeter use the original text as a script and that leads to Groovy seeing ${...} somewhere, where it expects a function (or variable). $ is a valid name for a function and the syntax of Groovy is flexible enough to let the parser believe, that the {...} is a closure ready to be send to that function. Alas the function named $ can't be found and the Groovy exception is thrown.
To take small steps. "log.info(${__threadNum})" gets replaced by JMeter to "log.info(23)", which is then handled by Groovy.
"log.info(${__threadNum)} // ${counter}" leads to an exception in the templating process and therefore that string is given as is to Groovy, which then looks for the "$" function, which would be happy to get passed a closure with "__threadNum" as content. Note, the "// ${counter}" gets ignored by Groovy.