JMeter 循环索引在循环控制器中始终返回 0

时间:2021-07-05 11:56:34

标签: jmeter

当我在 JSR223 Sampler 中使用命令 ${__jm__myLoopControllerName__idx} 时,它总是返回 0 作为索引。

采样器位于循环控制器中。但是我可以看到我正在循环的 CSV 文件是为每一行完成的,因为在侦听器“查看结果树”中,我可以在请求标头中看到数据来自每一行。我究竟做错了什么 ? 谢谢你的帮助。 米。

2 个答案:

答案 0 :(得分:0)

在 JSR223 脚本中使用 vars 获取变量:

String index = vars.get("__jm__myLoopControllerName__idx");

${} 语法中的变量正在获取 cached

<块引用>

确保脚本不使用任何使用 ${varName} 的变量,因为缓存将只使用 ${varName} 的第一个值。而是使用:vars.get("varName")

答案 1 :(得分:0)

根据JSR223 Sampler documentation

<块引用>

JSR223 测试元素具有可以显着提高性能的功能(编译)。要从此功能中受益:

  • 使用脚本文件而不是内联它们。如果此功能在 ScriptEngine 上可用,这将使 JMeter 编译它们并缓存它们。

  • 或者使用脚本文本并检查缓存编译脚本如果可用属性。>

    >>> import math
    >>> import numpy as np
    >>> math.nan is math.nan
    True
    >>> np.nan is np.nan
    True
    >>> np.nan is math.nan
    False
    

因此,要么将您的 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. 放在“参数”部分,并在脚本中将其称为“参数”:

enter image description here

或使用 vars shorthand for JMeterVariables class instance 像:

${__jm__myLoopControllerName__idx}

enter image description here