Re: Iteration Number in JSR223 Groovy string

Dmitri T <[email protected]> Tue, 2 Apr 2024 19:51:53 +0200
Newsgroups gmane.comp.jakarta.jmeter.user
Message-ID <PH7PR12MB713907E099152DF7ECCDEB7BC03E2@PH7PR12MB7139.namprd12.prod.outlook.com>
Tong Sun wrote:
> Thanks.
>
> So the root cause is:
>
> > ensure your script code does not use
> >     JMeter variables or JMeter function calls directly
>
> Having known that, I still don't quite understand why I'm getting zeros.
> My actual groovy string is:
>
> "ThreadGroup: ${__threadNum}, Iteration Number: 
> ${__V(__jm__${__threadGroupName}__idx)}"
>
> Now, suppose `${__threadGroupName}` returns `My_TG`, then the 
> `${__V(__jm__${__threadGroupName}__idx)}` will become 
> `${__jm__My_TG__idx}`, right? Then why ${__threadNum} gives none zero, 
> while ${__jm__My_TG__idx} always gives zero pls? They are the same 
> built-in JMeter variables and should behave the same way, right?
>
>
>
> On Tue, Apr 2, 2024 at 1:09 PM Dmitri T <[email protected] 
> <mailto:[email protected]>> wrote:
>
>     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.
>
I have bad news for you, given you're not capable of understanding what 
does "caching would only cache first replacement." phrase means even 
with the help of online translation tools and LLM you won't last long in 
IT so you should consider switching to OnlyFans career right away.

I'll try to explain it using the simplest possible words:

  * Iteration 1: your function returns 0
  * Iteration 2: your function returns 0 as it was cached previously, it
    will not be re-evaluated
  * Iteration 3: your function returns 0 as it was cached previously


__threadNum() returns the number of the current thread which doesn't 
change across iterations

But it you use other function which generates i.e. timestamp, random 
string, UUID or whatever you will observe the same behaviour: the value 
generated in 1st iteration will be returned during subsequent iterations.