Search code examples
jmeterbeanshell

Change "User Defined Variables" based on external variable


I am having some trouble trying to change User Defined Variables based on an already existing variable.

BeanShell:

String databaseSize;

if (${__P(DBsize,${DB_size})} == '0') {
vars.put("databaseSize","SmallDB"); }
    else if (${__P(DBsize,${DB_size})} == '1') 
    {vars.put("databaseSize","LargeDB");}

User Defined Variables

Name: path

Value: ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}\ ${databaseSize}

I would like to change the path based on what I store in databaseSize. I have two folders in the given path: SmallDB and LargeDB. As far as I know, UDV are getting assigned before JMeter manages to run my BeanShell.

Is there an alternative way of changing the path based on another variable (received from Jenkins or default value received from a file)?

Thank you


Solution

    1. Since JMeter 3.1 it is recommended to use Groovy language for any form of scripting mainly because Groovy performance is much better than Beanshell and friends
    2. You can put __groovy() function directly into Value stanza of the User Defined Variables - it will be evaluated when the configuration element will be processed
    3. You can go for ternary operator in order to convert your code into one smaller line, something like:

      ${__groovy((props.getProperty("DBsize").equals("0")) ? "SmallDB" : "LargeDB",)}
      

      So your configuration would be:

      Groovy user defined variables