【问题标题】:Jmeter how to construct payload with same size from csv file by iterationJmeter如何通过迭代从csv文件构造相同大小的有效载荷
【发布时间】:2022-01-13 18:56:10
【问题描述】:

我有一个包含 10 个用户 ID 的 csv 文件,要求是为 csv 中的每 5 个用户构建一个有效负载。以下是我的代码:

def start    = (vars.get('__jm__Thread Group__idx') as int)
def offset   = 5
def payload  = [:]
def data     = []

def file     = 'C:/path/dataset.csv'
start.upto(offset, { index ->
    def lineFromCsv = new File(file).readLines().get(index)
    data.add(['userId': lineFromCsv.split(',')[0], 'groupId': lineFromCsv.split(',')[1]])
})

payload.put('data', data)
log.info("%%%The Payload is%%%:" + payload)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())

我的第一个问题是为什么第一次有效载荷(第一次迭代)中有 6 个项目,而我期望有 5 个。而第二次有效载荷(第二次迭代)中有 5 个项目,正如预期的那样。每个有效载荷都应该具有相同的项目数。
我的第二个问题是如何使第二个有效负载从第一个有效负载停止的位置开始解析。第二个有效载荷应该包含 csv 中的下 5 个用户?每个有效载荷之间不应有任何重叠项。
下面是有效载荷:
第一个有效载荷:

POST data:
{
    "data": [
        {
            "userId": "fakeUser3k0000002",
            "groupId": "1"
        },
        {
            "userId": "fakeUser3k0000003",
            "groupId": "2"
        },
        {
            "userId": "fakeUser3k0000004",
            "groupId": "2"
        },
        {
            "userId": "fakeUser3k0000005",
            "groupId": "3"
        },
        {
            "userId": "fakeUser3k0000006",
            "groupId": "4"
        },
        {
            "userId": "fakeUser3k0000007",
            "groupId": "5"
        }
    ]
}

第二个有效载荷:

POST data:
{
    "data": [
        {
            "userId": "fakeUser3k0000003",
            "groupId": "2"
        },
        {
            "userId": "fakeUser3k0000004",
            "groupId": "2"
        },
        {
            "userId": "fakeUser3k0000005",
            "groupId": "3"
        },
        {
            "userId": "fakeUser3k0000006",
            "groupId": "4"
        },
        {
            "userId": "fakeUser3k0000007",
            "groupId": "5"
        }
    ]
}

【问题讨论】:

    标签: groovy jmeter jsr223


    【解决方案1】:
    def start    = (vars.get('__jm__Thread Group__idx') as int)
    def offset   = 5
    

    我猜Thread Group 是你的 jmeter 循环名称。

    你必须像这样构建循环

    (start*offset).step((start+1)*offset,1){ index->
        println it
    }
    

    所以对于start=5,您将拥有索引 25 到 29

    【讨论】:

    • start.upto((start * offset).step((start + 1) * offset, 1), { index -> def lineFromCsv = new File(file).readLines().get(index) data.add(['userId': lineFromCsv.split(',')[0], 'groupId': lineFromCsv.split(',')[1]]) }) 我得到了这个异常:javax.script.ScriptException:groovy.lang.MissingMethodException:没有方法签名:java.lang.Integer.step() 适用于参数类型:(整数, 整数)值:[5, 1]
    • 使用我在答案中指定的表达式。没有upto。
    【解决方案2】:
    1. 为了在第一个有效载荷中获得 5 个项目,您需要将偏移量定义为 4,因为在第一次迭代期间 __jm__Thread Group__idx 变量值是 0,您可以使用 Debug Sampler and View Results Tree listener combination 检查它
    2. 为了从位置4 开始第二次迭代,您需要在构造第一个有效负载后将offset 值存储到JMeter Variable 中,并在第二次迭代期间读取它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 2021-07-12
      • 2020-10-11
      • 2022-01-11
      相关资源
      最近更新 更多