5 BeanShell Test Cases You Should be Using
Apache JMeter has numerous built-in components for implementing various user scenarios in load testing scripts. In addition to these built-in components, we recommend you download and install JMeter Plugins, which add extra components and functions.
By using these functions, the majority of user scenarios can be implemented without the need to write code. However, it often happens that use cases and the logic of a tested application are rather complex, and therefore require dynamic data or modifications in load testing scripts samplers. This can't be done without programming. For instance, an HTTP response data has to be used with modifications in the next HTTP request, data processing might be required after data extraction by the JMeter Regular Expression Extractor, or when implementing a custom function.
Fortunately, JMeter has built-in components named BeanShell to resolve this. JAVA-based, BeanShell components let you insert code and implement complicated logic in your script, improving your load testing abilities.
BeanShell components include:
With BeanShell scripts you can manage JMeter variables and data in diverse ways, since they let you perform math operations and extract and replace variables values. In other words, you can do everything JMeter doesn't support directly.
1. Implementing a custom function
Although JMeter has a rich set of built-in functions, in some cases it’s necessary to implement custom functions. For example, when you want to calculate a random value that is used for deciding when to execute the next test fragment of the script.
By adding a BeanShell Sampler you can calculate this variable and evaluate it in the If Controller. The result is used to control navigation to the site.
In the example shown in the screenshot, if the random value is ‘1’ with a 0.25 probability, navigation to the site in the script is completed in 25% of the cases.
Using this approach it's possible to implement additional functions that are not available in standard JMeter.
2. Modifying data between samplers and changing configuration data
Sometimes users need to modify data before or after samplers, like when updating user defined variables or using data that was processed before a sampler, in the sampler itself.
For this purpose, you can use the BeanShell PostProcessor and PreProcessor. Add them as child elements to the sampler, for instance to a HTTP sampler, to modify output or input data correspondingly.
In the screenshot below, the BeanShell PreProcessor element is used to transform parameters and pass them to the login request.
3. Implementing complex assertions
JMeter includes numerous assertion elements for validating the data JMeter samplers return. However, sometimes the validation decision follows complex logic and can’t be implemented in other JMeter assertion components. For example, the number of entities returned in the JSON response, or the evaluation of values in the response.
One solution for this problem is the JMeter BeanShell Assertion. In this screenshot below, the BeanShell Assertion is used for validating the HTTP Sampler response and ensuring it contains exactly 3 entities.
4. Saving data to file
Sometimes you need to deploy data before the test execution or in the setUp thread group. This data, IDs and additional values, will be used in the test execution by user accounts and other entities of the tested application. The data is saved in the CSV file, accessed during the test, and can be removed afterwards in the tearDown group or in a separate script.
Saving the data to file is implemented by the BeanShell PostProcessor component script, added as a child element to the HTTP sampler.
In the screenshot below you can see a test fragment that is used to create a number of organizations for the tested application. Organizations are created with one of the tested application APIs, which returns the created organizations IDs and titles. These IDs and titles are saved in a file through the BeanShell PostProcessor.
5. Implementing a specific timer
JMeter components include a wide range of timers that provide delays between samples. With the timers, you can configure the delay to be constant, random or follow to some rule. If the delay is a random value that is distributed by uniform, gaussian or poisson law, we recommend you use the corresponding built-in timers in JMeter. If not, we need to program the delay.
The BeanShell timer component can be used for programming the delay. In the screenshot below you can see a JMeter script fragment that implements the random timer and a delay that follows the Simpson law distribution.
JMeter BeanShell components are powerful tools for implementing complex scenarios and logic. They do have one serious drawback through – the BeanShell interpreter requires resources, which may impact the performance of the workstation the JMeter load testing script is launched from. This may result in incorrect script operation. Therefore, if the BeanShell script might be impacting your script performance, it's better to use JS223 components and implement the script in the Groovy language.
Congratulations! You now know how to use BeanShell for additional use cases, and improve the way you performance test. Learn about advanced JMeter testing scenarios from this free webinar.
You might also find these useful:
Interested in writing for our Blog? Send us a pitch!