【问题标题】:Json extract unique data - JmeterJson 提取唯一数据 - Jmeter
【发布时间】:2019-03-19 17:22:40
【问题描述】:

我的线程组中有一组请求,其中第一个请求将为第二个请求提供输入。我正在使用 json 提取器(匹配 No.0)从列表中提取值。我想确保在后续线程运行中不会获取在线程 1 中提取的相同数据。你能建议如何处理吗?

【问题讨论】:

    标签: jmeter jmeter-plugins


    【解决方案1】:

    根据JSON Extractor documentation

    如果 JSON 路径查询导致许多结果,您可以选择提取哪些作为变量:

    0 :表示随机(默认值)

    “随机”并不能保证唯一性,因此如果您需要数据唯一 - 请考虑为此“匹配号”字段提供递增值。

    示例设置:

    1. 鉴于以下 JSON

      {
          "store": {
              "book": [
                  {
                      "category": "reference",
                      "author": "Nigel Rees",
                      "title": "Sayings of the Century",
                      "price": 8.95
                  },
                  {
                      "category": "fiction",
                      "author": "Evelyn Waugh",
                      "title": "Sword of Honour",
                      "price": 12.99
                  },
                  {
                      "category": "fiction",
                      "author": "Herman Melville",
                      "title": "Moby Dick",
                      "isbn": "0-553-21311-3",
                      "price": 8.99
                  },
                  {
                      "category": "fiction",
                      "author": "J. R. R. Tolkien",
                      "title": "The Lord of the Rings",
                      "isbn": "0-395-19395-8",
                      "price": 22.99
                  }
              ],
              "bicycle": {
                  "color": "red",
                  "price": 19.95
              }
          },
          "expensive": 10
      }
      
    2. 假设您想使用唯一的书名,即

      • Sayings of the Century - 第一次迭代
      • Sword of Honour - 第二次迭代
    3. 将“匹配号”字段值修改为如下所示:

      ${__intSum(${__jm__Thread Group__idx},1,)}
      

      其中__jm__Thread Group__idx 是自JMeter 4.0 以来可用的预定义变量,它返回当前Thread Group 迭代,__intSum() 是JMeter 函数,它将1 添加到迭代数(因为它是从零开始的)

      完整的 JSON 提取器配置类似于:

    4. 基本上就是这样,现在您确信每次线程组迭代都会获取新值,可以使用 View Results Tree listener 来检查它。

    【讨论】:

    • 您好,感谢您的指导,我尝试了您提到的步骤并收到以下错误:ERROR o.a.j.t.JMeterThread: Error while processing sampler: 'Debug Sampler'。 java.lang.NumberFormatException:对于输入字符串:"${__jm__Thread Group__idx}" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:1.8.0_181] at java.lang.Integer.parseInt(Integer .java:569) ~[?:1.8.0_181] 在 java.lang.Integer.parseInt(Integer.java:615) ~[?:1.8.0_181]。我只使用 Jmeter 4.0
    • 您的线程组的名称是什么?如果它与 Thread Group 不同,则需要修改变量,如 ${__jm__put-your-thread-group-name-here__idx}
    • 我能够修复它。再次感谢您的支持!
    • 该逻辑适用于循环计数,但不适用于线程数。例如:如果我有 5 个线程和一个循环计数,则逻辑与 Match No as 1 相同
    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多