我正在阅读 JMeter 文档,并遇到了这个关于“If控制器”:
当条件被解释为 Javascript 时,脚本无法使用任何变量。如果您需要访问此类变量,请选择“将条件解释为变量表达式?”并使用 __javaScript() 函数调用。然后,您可以在脚本中使用对象“vars”、“log”、“ctx”等。
我不太明白这一点。这是否意味着如果我想访问“用户定义的参数”,那么我只能通过编写一些 JavaScript 来访问它?此框后面的示例引用“${COUNT}”
有人可以澄清 If 控制器的用法,也许可以举一两个示例吗?
I was reading the JMeter documentation and came across this info box about "If Controllers":
No variables are made available to the script when the condition is interpreted as Javascript. If you need access to such variables, then select "Interpret Condition as Variable Expression?" and use a __javaScript() function call. You can then use the objects "vars", "log", "ctx" etc. in the script.
I don't quite follow this. Does this mean if I want access to a "User Defined Parameter" then I can access it only by writing some JavaScript? The example that follows this box then refers to "${COUNT}"
Could someone clarify the usage of the If Controller, maybe with an example or two?
发布评论
评论(9)
这些答案都是错误的!您需要将变量引用放在引号中,如下所示:
All these answers are wrong! You need to put the variable reference in quotes, like so:
您可以简单地使用类似
有时 JMeter 文档可能会令人困惑:)
2017 年 9 月 27 日编辑:
这里的答案有效,但当线程数超过 40 时,会对性能产生非常糟糕的影响。 >
请参阅下面的正确且最有效的答案:
请参阅:
You can simply use something like
Sometimes JMeter documentation can be confusing :)
Edit 27 september 2017:
The answer here works but has a very bad performance impact when number of threads exceeds 40.
See below for correct and most performing answer:
See:
如果控制器将在内部使用 javascript 来评估条件,但这可能会降低性能。
更好的选项(从 JMeter 4 开始默认,请参阅 https://bz.apache .org/bugzilla/show_bug.cgi?id=61675) 是选中“将条件解释为变量表达式?”,然后在条件字段中有 2 个选项:
或任何您想要包含 true/false 的变量
例如,如果 COUNT 等于 1:
或
从 4.0 开始,如果不使用“将条件解释为变量表达式?”,则会出现红色警告显示:
如果您想了解有关 JMeter 和性能测试的更多信息 书可以帮助你。
If Controller will internally use javascript to evaluate the condition but this can have a performance penalty.
A better option (default one starting from JMeter 4, see https://bz.apache.org/bugzilla/show_bug.cgi?id=61675) is to check "Interpret Condition as Variable Expression?", then in the condition field you have 2 options:
or any variable you want that contains true/false
For example if COUNT is equal to 1:
OR
Starting with 4.0, if you don't use the "Interpret Condition as Variable Expression?", a warning in RED will be displayed:
If you'd like to learn more about JMeter and performance testing this book can help you.
取消选中复选框
“将条件解释为变量表达式”
我浪费了几个小时而没有取消选中此复选框。无论语句末尾有或没有分号(;),它都可以工作。确保在调用 if 控制器之前已设置用户定义变量。
以下所有变体在 Jakarta Jmeter 1.5 中都对我有用
UNCHECK the CHECKBOX
"Interpret condition as variable expression"
I wasted a couple of hours without unchecking this checkbox. It worked with and without semicolon(;) at the end of the statement. Make sure that you have set the User-Defined Variables before calling the if controller.
All the following variations worked for me in Jakarta Jmeter 1.5
上帝保佑http://habrahabr.ru
一直尝试直到找到这些。
使用引号是我的解决方案。
God bless the http://habrahabr.ru
Have tried until found these.
Using the quotes was my solution.
正如 Gerrie 所说,您需要检查变量
,但要小心 '用户定义变量< /a>'
这基本上意味着您不能在“If 控制器”内定义“用户定义变量”。请查看“BeanShell”。
As Gerrie said you need to check your variable
But be careful with the 'User Defined Variables'
That basically means that you cannot define 'User Defined Variables' inside an 'If Controller'. Take a look to the 'BeanShell' instead.
代替:
${my_variable}=='1'
和
“${my_variable}”==“1”
Replace:
${my_variable}=='1'
with
"${my_variable}" == "1"
如果它的字符串值传递如下并且其性能有效
${__groovy("${key}"=="value")}
if it's string value pass as below and its performance effective
${__groovy("${key}"=="value")}
我已经在条件下使用了
${code_g1}== 200
并且它有效我。I have used
${code_g1}== 200
in condition and it worked for me.