JMeter-使用$ {}或vars.get()获得变量值之间的区别是什么 [英] JMeter-what is difference between use ${} or vars.get() to get value of variable
问题描述
今天,当我调试JMeter脚本时,发现了一个使我感到困惑的问题.
Today when I was debugging my JMeter script, I found a problem that confused me a lot.
- CSV数据配置元素:在CSV中,我将变量
userId
设置为1001200
- CSV Data Config element: in CSV, I set variable
userId
to1001200
- 然后在下面运行脚本,并在使用
${userId}
和vars.get("userId")
时获得不同的"userId"值.我认为它们应该具有相同的值,但事实并非如此.运行vars.put("userId", "-111")
之后,${userId}
和vars.get("userId")
获得不同的值:
- Then run script below, and get different value of "userId" when using
${userId}
andvars.get("userId")
. I think they should be same value, but it seems not. After runvars.put("userId", "-111")
,${userId}
andvars.get("userId")
get different values:
因此,即使${}
和vars.get()
的变量相同,似乎也有一些区别,有人知道原因吗?
so it seems ${}
and vars.get()
have some difference even though their variable is same, does anyone know the reason?
谢谢.
推荐答案
@ user7294900提供的答案是指选中缓存编译脚本选项的情况.但是,即使未选中它,您的脚本也将在执行前解析定义为${varName}
的变量,而在执行期间解析vars.get("varName")
的变量.
Answer provided by @user7294900 refers to the case when Cache compiled script option is checked. But even if it's not checked, your script will resolve variables defined as ${varName}
before execution, while vars.get("varName")
is resolved during execution.
在JMeter即将运行任何元素(采样器,预处理器或后处理器)之前,它将占用(每个)文本字段,并将由${...}
标识的任何变量,函数或内联代码解析为可用的值在评估时.因此语法${...}
将变量转换为常量字符串,并且您的代码(对于Groovy或任何其他执行引擎)将如下所示:
Before JMeter is about to run any element (sampler, pre- or post-processor), it will take (every) text field and will resolve any variables, functions or inline code, identified by ${...}
to their values available at the time of the evaluation. Thus syntax ${...}
converts variable into a constant string and your code (for Groovy or any other execution engine) will look like this:
log.info("***" + "1001200" + "***");
log.info("***" + vars.get("userId") + "***");
vars.put("userId", "-111");
log.info("***" + "1001200" + "***");
log.info("***" + vars.get("userId") + "***");
因此,无论您在执行过程中如何更改变量,它都不会更改,因为它不再是变量.但是vars.get("userId")
另一方面是函数调用,并且每次都会检查变量值.
Thus no matter how you change variable during execution, it won't change since it's no longer a variable. But vars.get("userId")
on the other hand, is a function call and will check variable value every single time.
这篇关于JMeter-使用$ {}或vars.get()获得变量值之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!