Search code examples
jmeterbeanshell

Store a long value to a variable in Jmeter using preprocessor and print/use the same value in a sampler


below are the steps i followed, 1) Added a BeanShell sampler and add a Bean Shell Preprocessor to the same sampler.

long c = 25478995L;
log.info(c + " - It is long");
vars.put("c",c)

2) I am trying to print the value c in Bean Shell sampler like below,

${c}

3) But when i see in view result tree i am getting the ${c} instead i am expecting to view 25478995.

Can any one help me please. Thanks in advance.

Regards, Hari


Solution

    1. If you want to store a Long per se you need to use vars.putObject() function instead like:

      vars.putObject("c", c);
      

      and later on:

      log.info("My long value is: " + vars.getObject("c"));
      
    2. If you want to have String representation - you need to convert your Long to String first like:

      vars.put("c", Long.toString(c));
      
    3. Don't refer variables like ${c} in scripts, use vars.get("c"); or vars.getObject("c"); instead
    4. Since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language rather than Beanshell for performance reasons, see Apache Groovy - Why and How You Should Use It for more details.