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.
What Is Beanshell in JMeter? BeanShell is one of the most advanced JMeter built-in components. It supports Java syntax and extends it with scripting features like loose types, commands, and method closures.
BeanShell vars.Create a sampler to request a product from the Amazon website. Then add a Post CSS/JQuery Extractor and extract the buying price. Whenever this request is executed, JMeter will create a variable on memory with the “Reference Name.” This variable is only available during that test.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With