Another important difference here (assuming this is groovy code in a jsr223 script), you are assigning Groovy variables, but __evalVar works on JMeter variables.

To set a JMeter variable from within Groovy code, use vars.put("name", value).

And to reiterate: Don't confuse ${...} from JMeter with the one from Groovy. JMeter will try to replace the ${...} stuff on first run (as Dmitri wrote).

JMeter has a fallback mechanism, if it doesn't find a variable in its own namespace, that the expression will be used unchanged. In the case of a more complex expression, the result depends on the function.

In your case:

* ${query} (with no JMeter variable named "query") will result in ${query}, which is then interpreted by Groovy and accesses the Groovy variable "query".

* The complex expression ${__evalVar(query)} will result in an empty string (as can be tested in the function helper dialog). (Groovy sees log.info('=='))

Felix

Am 05.09.22 um 09:05 schrieb Dmitri T:
1. You don't need to use __evalVar() function in Groovy, in general
   inlining JMeter Functions and Variables into scripts is not the best
   idea as only first occurrence will be cached
<https://jmeter.apache.org/usermanual/component_reference.html#JSR223_Sampler>
   so you will have the same query for all iterations even if the
   values will be changing.
2. If you want to utilize Groovy's string interpolation
<https://groovy-lang.org/syntax.html#_string_interpolation> feature
   you need to use double quotation marks
<https://groovy-lang.org/syntax.html#_double_quoted_string> like
   "select ${column} from ${table}"
3. "column" and "table" variables need to be defined *before* declaring
   the "query" variable


Suggested code change:


column="name"
table="customers"
query="select ${column}from ${table}"
log.info("${query}")


More information on Groovy scripting in JMeter - Apache Groovy: What Is Groovy Used For? <https://www.blazemeter.com/blog/apache-groovy>


On 9/4/2022 10:31 PM, Tong Sun wrote:
Hi,

Fromhttps://jmeter.apache.org/usermanual/functions.html#__evalVar

__evalVar
The evalVar function returns the result of evaluating an expression stored
in a variable.

This allows one to read a string from a file, and process any variable
references in it. For example, if the variable "query" contains "select
${column} from ${table}" and "column" and "table" contain "name" and "
customers", then ${__evalVar(query)} will evaluate as "select name from
customers".

However, I tried it (in JM5.5), but wasn't able to make it work:

query = '''select ${column} from ${table}'''
column = "name"
table = "customers"

log.info("${query}")
log.info('=${__evalVar(query)}=')


but the output I got is:

select ${column} from ${table}==
How to make it work please?

Attachment: OpenPGP_0xEA6C3728EA91C4AF.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to