BeanShell Processor Tutorial 2: Advanced Usage
Learn about working with dates, file writing, and thread groups or responses in BeanShell.
Join the DZone community and get the full member experience.
Join For FreeBeanShell 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.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, Integer.parseInt("5"));
Calendar calReturn = Calendar.getInstance();
calReturn.add(Calendar.DAY_OF_YEAR, Integer.parseInt("10"));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
vars.put("outFormatDate",df.format(cal.getTime()));
vars.put("returnFormatDate",df.format(calReturn.getTime()));
log.info("Departure Date "+vars.get("outFormatDate"));
log.info("Arrival Date "+vars.get("returnFormatDate"));
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.
FileOutputStream fout = new FileOutputStream(PATH+"/log.txt");
PrintStream out = new PrintStream(fout);
out.println("Departure Date "+vars.get("outFormatDate"));
out.println("Arrival Date "+vars.get("returnFormatDate"));
out.close();
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.
log.info("THREAD Name "+ctx.getThread().getThreadName());
log.info("THREAD Name "+ctx.getThread().getStartTime());
log.info("Response Time is: " + prev.getTime().toString());
log.info("Connect Time is: " + prev.getConnectTime().toString());
log.info("URL is: " + prev.getURL());
log.info("The result is passed: " + prev.isSuccessful().toString());
log.info("Headers are: " + prev.getResponseHeaders());
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.
Happy load testing!
Published at DZone with permission of Canberk Akduygu. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments