Fix to my groovy call, I missed out a "_"

-----Original Message-----
From: Stuart Kenworthy
Sent: Wednesday, April 3, 2024 11:10 AM
To: JMeter Users List <user@jmeter.apache.org>
Subject: RE: Iteration Number in JSR223 Groovy string

I think the problem is, in certain circumstances when used in JMeter components 
vars  are only resolved on first read. From that point on JMeter is using the 
resolved value, not the original function.

The reason you get a 0 is if JMeter expects an integer in that space, it 
represents null as 0. When it ran it initially the response was null, so will 
forever return 0.

One way of getting around this is to wrap the resolution in a groovy function.

${__groovy( return vars.getObject("__jm__" + ctx.getThreadGroup.getName() + 
"__idx)" )}

Or something like, this will run the groovy code fresh each time it is hit. 
Have a play about with it. I used vars.getObject() to return the variable as 
its actual type because vars.get() in groovy always returns it as a string.

-----Original Message-----
From: Tong Sun <suntong...@gmail.com>
Sent: Tuesday, April 2, 2024 6:34 PM
To: Dmitri T <glin...@live.com>
Cc: JMeter Users List <user@jmeter.apache.org>
Subject: Re: Iteration Number in JSR223 Groovy string

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 <glin...@live.com> 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_S
> ampler>
>
> 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.
>
>

The information included in this email and any files transmitted with it may 
contain information that is confidential and it must not be used by, or its 
contents or attachments copied or disclosed to, persons other than the intended 
addressee. If you have received this email in error, please notify BJSS. In the 
absence of written agreement to the contrary BJSS' relevant standard terms of 
contract for any work to be undertaken will apply. Please carry out virus or 
such other checks as you consider appropriate in respect of this email. BJSS 
does not accept responsibility for any adverse effect upon your system or data 
in relation to this email or any files transmitted with it. BJSS Limited, a 
company registered in England and Wales (Company Number 2777575), VAT 
Registration Number 613295452, Registered Office Address, 1 Whitehall Quay, 
Leeds, LS1 4HR

Reply via email to