Craft Powerful Performance Tests with Ease Loadium Script Builder
BeanShell Processor Tutorial 2: Advanced Usage
BeanShell Processor Tutorial 2: Advanced Usage
BeanShell is the most powerful component of Apache JMeter. You can execute any Java code by using BeanShell scripts. If you are new to BeanShell, please take a look basic usage of BeanShell Processor.
In this BeanShell tutorial, I want to deep dive into BeanShell and show you some other example of its usage. If you’re familiar with Java or any other language, it is very easy to get used to it.
Sample 1: Dealing with Dates
Dates are the most frightening component of any developer and testers also. In case you need deal with dynamic dates during your test, just follow these steps.
In this BeanShell example, we will create two dates for a reservation system.
After running the code, you will have output:
Our code added 5 and 7 days to today’s date in order to create two new Date objects. Then we assigned those variables by using vars.put() method to be able to use them in JMeter scripts.
Sample 2: File Writing
You might need some extra logging during your test. Basic FileOutputStream and PrintStream are enough to achieve this need.
This will create a log.txt file under the PATH you specified. PrintStream will write anything you want in that text file.
Sample 3: Dealing with Thread Groups or Responses
You can enable your BeanShell Processor to get information from your Thread.
CTX variable gives you the control over your current Thread. There are many functions that you can use.
PREV variable gives you control over your sampler’s response.
Then you have the response:
As you can see, the sky is the limit. Keep on load testing by using BeanShell Processors. Check out Loadium blog to learn more about BeanShell Processors.
Happy load testing!