【问题标题】:jmeter pass variable between threadsjmeter在线程之间传递变量
【发布时间】:2017-12-14 03:41:27
【问题描述】:

TestPlan 线程组 HTTP 请求 1 ->正则表达式提取器 - 返回 10 个结果 - URL - 单线程 ForEach 控制器 - 使用提取器中的变量 - 成功循环上述结果 HTTP Request2 -> 正则表达式提取器 - 返回 10 个结果 以上是在 1 个线程下

我想让 ForEach 控制器在不同的线程下——运行多个 therads 并使用从 1 个线程 HTTP 采样器中提取的 URL——我尝试使用这两种方法 https://www.blazemeter.com/blog/knit-one-pearl-two-how-use-variables-different-thread-groups How do I pass a variable from one Thread Group to another in JMeter 但不知何故现在设法让它工作 请帮忙

【问题讨论】:

    标签: multithreading foreach controller jmeter


    【解决方案1】:

    ForEach Controller 不适用于 JMeter 属性,它仅适用于 JMeter 变量,因此如果您想在不同的 Thread Groups 之间传递它们,则需要编写一些脚本。

    1. 在正则表达式提取器后添加JSR223 PostProcessor,并将以下代码放入“脚本”区域

      vars.entrySet().each { var ->
          if (var.getKey().startsWith('foo')) {
              props.put(var.getKey(), var.getValue())
          }
      }
      

      foo 替换为正则表达式提取器中的Reference Name。上面的代码会将所有名称以foo开头的变量转换为相关的JMeter属性

    2. Test Action 采样器添加到第二个线程组(您不需要测量属性到变量转换的时间,对吗)
    3. JSR223 PreProcessor 添加为测试操作采样器的子项
    4. 将以下代码放入“脚本”区域

      props.entrySet().each {prop ->
          if (prop.getKey().startsWith('foo')){
              vars.put(prop.getKey(),prop.getValue())
          }
      }
      

      上面的代码将 JMeter 属性转换为 JMeter 变量,这样您就可以在第二个线程组的 ForEach 控制器中使用它们。同样,将 foo 替换为您自己的变量的引用名称。

    有关在 JMeter 测试中使用 Groovy 脚本的更多信息,请参阅 Apache Groovy - Why and How You Should Use It 文章

    【讨论】:

    • 我有参考名称:inputKitchenLevel1
    • 我有参考名称:inputKitchenLevel1。在 JSR223 PostProcessor 我有这个代码 vars.entrySet().each { var -> if (var.getKey().startsWith('input')) { props.put(var.getKey(), var.getValue()) } } 语言被选为 groovy - 2nd thread Test Action -> JSR223 PreProcessor with code props.entrySet().each {prop -> if (prop.getKey().startsWith('input')){ vars.put(prop .getKey(),prop.getValue()) } } foreach 控制器 - 输入变量前缀:${inputKitchenLevel1} 但我收到错误
    • JSR223 后处理器错误 ERROR javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-all-2.4.12.jar:2.4.12] at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_144] at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor .java:42) [ApacheJMeter_components.jar:3.3 r1808647] 在 org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:833) [ApacheJMeter_core.jar:3.3 r1808647]
    猜你喜欢
    • 2012-12-30
    • 2018-10-11
    • 2021-07-29
    • 2017-11-15
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    相关资源
    最近更新 更多