Disclaimer: This post is for those still using Beanshell. However, you should know that the Philippe Mouawad, JMeter contributor, has asked us to pass on that everyone should “avoid Beanshell in favor of JSR223 + Groovy because Beanshell is old, slow and does not support Java 5 and upper syntax.” If you still will be using Beanshell for whatever reason, here are Beanshell for JMeter Best Practices.
JMeter has powerful abilities to perform general tasks out of the box. In many performance test cases you don’t need to write code when creating test cases. However, in some circumstances you might need to write a script. At this point Beanshell comes to our aid. This post will look at Beanshell for JMeter Best Practices.
BeanShell is a light-weight scripting programming language. It has syntax similar to Java. Instead of compiling, it is an interpreted scripting language.
There are a few places you can utilize Beanshell within JMeter:
- BeanShell Sampler allows you to use the BeanShell scripting language to do performance tasks and measure results. Apache JMeter BeanShell Sampler Docs
- BeanShell Listener allows the use of BeanShell for processing samples for saving, so you can listen to results from other samples and process the response. BeanShell Listener docs
- BeanShell Assertion enables assertion checking with a BeanShell script, so you can check your results and use them to validate a response. BeanShell Assertion Docs
- BeanShell Timer can be used to generate custom delay logic. Docs
- BeanShell PreProcessor allows running a script before a sampler is executed. Docs
- BeanShell PostProcessor runs a script after a sampler is executed. Docs
Before we start to review best practices keep in mind that your first priority should be avoid scripting if possible.
Beanshell for JMeter Best Practices
Using the right syntax
BeanShell interprets standard Java statements, expressions and syntax. So it is important not to forget the basic expressions. For example:
After an expression, you must use a semicolon or once using if statement use == operator for comparison
Using Exception Handling (try-catch)
Exception handling is a mechanism which allows exceptions to be thrown and caught. It is a very good habit to use exception handling to avoid getting errors. For example below we declared one integer variable and try to divide it by zero. Once we run the test, we will get an error and the code is terminated .
However once we wrap our code within the try-catch block, our code will not be terminated and we won’t come across an error. Our test will not be affected by this error.
Check the variables value before manipulating it
This is also a common issue in JMeter trying to manipulate variables with null value. In order to avoid getting errors, it is best practice to check if the variable is a null value.
Using Log Viewer and log function
It is good practice to follow the logs of your test execution. To open this click Options->Log Viewer Menu tab.
Also adding extra logging like log.info (“log”); so you are able to determine which lines are fine and where execution stops by looking into Log Viewer tab or jmeter.log file
What other Beanshell for JMeter Best Practices do you follow?