Search code examples
jmeterbeanshell

Jmeter. BeanShell PostProcessor


I have gone through the bean shell scripting in jmeter but i did not find any example of that, how it is useful in jmeter and which way.means reading the sampler values etc. Can any one explain bean shell scripting in Jmeter with example.In beanshell post/pre processor script where we write the script. I am struggling with this what is the actual usage of it .Please explain with this .it would be great help for me or others as well for understanding the usage of it.


Solution

  • If you look into "Script" section of Beanshell Post Processor you'll see the following:

    Script(variables: ctx, vars, props, prev, data, log)
    
    • ctx - stands for JMeterContext, provides access to JMeter Context API (see JavaDoc for details). Example usage:

      int threadNum = ctx.getThreadNum(); // get current thread number 
      
    • vars - stands for JMeterVariables. Using vars you can get/set variable values.

      String myvar = vars.get("myvar"); // get ${myvar} variable value and store it to myvar string 
      myvar = myvar + "something"; // append "something" to myvar
      vars.put("myvar", myvar); // put new value into ${myvar} variable
      
    • props - stands for JMeter Properties. Basically the same as variables, but variables visibility is limited to current thread group only and properties are "global"

    • prev - shorthand to previous SampleResult. Seems to be exactly what you're looking for. You can get/set start time, end time, execution time, latency, URL, response code, response message, etc. See JavaDoc for comprehensive information. Example usage:

      String code = prev.getResponseCode(); 
      String message = prev.getResponseMessage();
      
    • data - byte array containing parent sampler response data

      String samplerData = new String(data);
      System.out.println(samplerData);
      
    • log - can be used to print something to jmeter.log file

      log.info("This line has been written by Beanshell Post Processor");
      

    See How to use BeanShell: JMeter's favorite built-in component guide for more details and real-life examples.