Jmeter vars.put不保存变量(jsr223预处理器groovy)

时间:2017-07-17 07:26:27

标签: maven groovy jmeter

我想将变量存储在goovy中,稍后再使用它们

我从“用户定义的变量”中获取初始值(这些是由maven设置的)

但是当我更改它们然后放入它们时它们不会永久存储

所以效果如下:

... INFO ...: throughput was: 600
... INFO ...: throughput is now: 720
... INFO ...: throughput is now (get): 720
... INFO ...: throughput is now (get): 720
... INFO ...: throughput is now (get): 600
... INFO ...: throughput is now (get): 600

脚本:

org.apache.jmeter.testelement.property.JMeterProperty;


int troughtputEnabled = Integer.parseInt(vars.get("enableTroughput"));
if(troughtputEnabled == 1){
    int req = Integer.parseInt(vars.get("idCounter"));
    int troughput = Integer.parseInt(vars.get("throughput"));
    if (req%100 == 0){
        log.info("throughput was: " +  troughput);
        troughput += 120;
        log.info("throughput is now: " +  troughput);
        vars.put("throughput",troughput+"");
        log.info("throughput is now (get): " +  vars.get("throughput"));
    }
    log.info("throughput is now (get): " +  vars.get("throughput"));

}

有人可以看到我在这里做错了(日志仅用于调试)

2 个答案:

答案 0 :(得分:1)

所以我得到了它的工作:

import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.util.JMeterUtils;

int troughtputEnabled = Integer.parseInt(vars.get("enableTroughput"));
if(troughtputEnabled == 1){
    int req = Integer.parseInt(vars.get("idCounter"));
    int troughput = Integer.parseInt(vars.get("throughput"));
    if (req%100 == 0){
        log.info("throughput was: " +  troughput);
        troughput += 120;
        log.info("throughput is now: " +  troughput);
        vars.put("throughput",troughput+"");
        log.info("throughput is now (get):---------------- " +  vars.get("throughput"));
        JMeterUtils.setProperty("troughput", troughput +"");

    }
    else
    {
        String tempTroughput = JMeterUtils.getProperty("troughput");
        if (tempTroughput != null && !tempTroughput.equals("")){
            vars.put("throughput",tempTroughput+"");
        }

    }
    log.info("throughput is now (get): " +  vars.get("throughput"));

}

问题似乎是vars.put仅适用于当前线程

答案 1 :(得分:0)

第一次迭代进入if (req%100 == 0),因此局部变量增加120 第二次迭代没有进入if (req%100 == 0),所以打印vars.get("throughput")保持600。

如果您想要更新变量,只需将其放在troughput += 120;之后的变种中:

vars.put("throughput",   String.valueOf( troughput));