Robert Stevens
Robert Stevens

Reputation: 528

Jmeter vars.put doesnt save variable (jsr223 preprocessor groovy)

I want to store variable in goovy and use them later on

I get my initial value from "user defined variables" (these are set by maven)

but when i change them and then put them they are not stored permanently

so the effect works as followed:

... 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

Script:

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"));

}

Can someone see what i do wrong here (the logs are only for debugging)

Upvotes: 3

Views: 8026

Answers (2)

Robert Stevens
Robert Stevens

Reputation: 528

So I got it working:

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"));

}

The problem seems to be that vars.put only works for the current thread

Upvotes: 2

Ori Marko
Ori Marko

Reputation: 58862

first iteration you get inside if (req%100 == 0) so local variable is increment by 120 second iteration no get inside if (req%100 == 0) so print vars.get("throughput") which stays 600.

If you want variable to be updated just put it in vars after troughput += 120;:

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

Upvotes: 1

Related Questions