Tuesday, October 31, 2017

Jmeter: Date-Time computation in pre-processor

Often I have to deal with dates and times in my scripting and luckily JMeter is quite good at dealing with them. The function to call is __time (JMeter documentation http://jmeter.apache.org/usermanual/functions.html). The thing is the doco is pretty sketchy about all the different formats it can take. So I just created a test script to see what works. The examples are below ready to copy & paste. I also have included scripts for time and date manipulation for completeness.
Note that __time can only display the current time local to the machine.
FunctionExample Result for
01/12/2015 02:00PM
${__time(YMD)}20151201
${__time(yyyyMMdd)}20151201
${__time(yyMMdd)}151201
${__time(dd-MM-yyyy)}01-12-2015
${__time(dd/MM/yyyy hh:mm:ss)}01/12/2015 14:00:00
${__time(dd/MM/yyyy HH:mm:ss a)}01/12/2015 02:00:00 PM
${__time()}1454358328739
${__time(yyyy-MM-dd’T’hh:mm:ssX)}2015-12-01T14:00:00+13
There is no possibility to add/subtract in the __time function. In order to do that you will need to use BeanShell, groovy or javascript.
Here is a simple groovy (JSR223 Processor) example to add a day to the current date. There are dozens of other ways to do this though.
import java.text.SimpleDateFormat; 
import java.util.Date; 

// You need to define ${InDays} as how many days forward/back you want to go
int InDays = Integer.valueOf(vars.get("InDays"));

Date date = new Date(); 
date.setDate(date.getDate() + InDays); 
SimpleDateFormat df  = new SimpleDateFormat("dd/MM/yyyy"); 
String formattedDate = df.format(date); 

// Resulting date will be in ${FutureDate}
vars.put("FutureDate",formattedDate);
This function is only good for adding/subtracting days though. If you need to manipulate time the below function can be used. The below can also be changed to add days too though. It would be overkill if you just need to manipulate the date though.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

// The below variables need to be defined
int ForwardSecond     = Integer.valueOf(vars.get("ForwardSecond"));
int ForwardMinute     = Integer.valueOf(vars.get("ForwardMinute"));
int ForwardHour       = Integer.valueOf(vars.get("ForwardHour"));

Date now         = new Date();
Calendar c       = Calendar.getInstance();
c.setTime(now); 
c.add(Calendar.SECOND, ForwardSecond);
c.add(Calendar.MINUTE, ForwardMinute);
c.add(Calendar.HOUR,   ForwardHour);
Date NewTime        = c.getTime();
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); 
String mytime       = df.format(NewTime);

// Rsult can be accessed through ${NewTime}
vars.put("NewTime",mytime);
I hope this helps those needing to deal with times and dates in JMeter.

1 comment:

  1. A committed group is a moderately new term, notwithstanding, it has previously won numerous business people's consideration. It infers the organization designating programming improvement to a re-appropriated seller is liable for building a remote group. This model is a lot less expensive choice to track down a coder than the past one, offers an extraordinary expansion in efficiency, and gives you admittance to a greater ability pool. By picking devoted group enrollment you will actually want to find Ukrainian programming designers for a cheaper who could have a strong degree of tech skill, regardless of whether you have interesting innovation needs>> Mobilunity

    ReplyDelete