Re: Iteration Number in JSR223 Groovy string

Dmitri T <[email protected]> Tue, 2 Apr 2024 19:09:26 +0200
Newsgroups gmane.comp.jakarta.jmeter.user
Message-ID <PH7PR12MB71390D43401229A8B4349E51C03E2@PH7PR12MB7139.namprd12.prod.outlook.com>
Tong Sun wrote:
> JMeter has a built-in variable ${__jm__Thread Group__idx} to retrieve the
> current iteration, and I want to replace the `Thread Group`
> using ${__threadGroupName}. So this comes natural to me:
>
> "Iteration Number is ${__V(__jm__${__threadGroupName}__idx)}"
>
> But when I try it, I always get "0".
>
> What's wrong with it and what's the fix pls? thx
>
It comes natural to you only. If you open JSR223 Sampler 
<https://jmeter.apache.org/usermanual/component_reference.html#JSR223_Sampler> 
documentation you will see something like:

> The JSR223 test elements have a feature (compilation) that can 
> significantly increase performance. To benefit from this feature:
>
>   * Use Script files instead of inlining them. This will make JMeter
>     compile them if this feature is available on ScriptEngine and
>     cache them.
>   * Or Use Script Text and check Cache compiled script if available
>     property.
>     When using this feature, ensure your script code does not use
>     JMeter variables or JMeter function calls directly in script code
>     as *caching would only cache first replacement*. Instead use
>     script parameters.
>

So either put the functions combination into "Parameters" section of 
your "script" and use it like

"Iteration number is " + Parameters

or go for code-based equivalent:

"Iteration number is: " + vars.get('__jm__' + 
ctx.getThreadGroup().getName() + '__idx')

Check out Top 8 JMeter Java Classes You Should Be Using with Groovy 
<https://www.blazemeter.com/blog/jmeter-java-classes> to learn what do 
these *vars* and *ctx* guys mean.