We all have to deal with Date and time in our script, and thankfully jmeter has one inbuilt function which we can use in many cases, but sometimes we also need to use Beanshell for getting future or Past dates. this blog is all about how differently you can use jmeter time function.
Main thing we all have to consider is that ${__time()} function takes current date and time of local machine, i.e. on whichever Load Generator machine you are executing a test, it will take local time of that machine. So you also need to consider time difference, if any, of Load Generator machine and your servers.
Below is few examples of time function:
Note: below function was executed on 30th May 2017 12:56:29 PM
But there are many cases where you need to take either future date or Past date, during that time we cannot use time function. We have to use beanshell for getting such data. Below is few example of extracting future date/Past date using beanshell.
For time function as well sometimes we need to take past time or future time. Below is function which can be used for extracting past/future timings
Main thing we all have to consider is that ${__time()} function takes current date and time of local machine, i.e. on whichever Load Generator machine you are executing a test, it will take local time of that machine. So you also need to consider time difference, if any, of Load Generator machine and your servers.
Below is few examples of time function:
Note: below function was executed on 30th May 2017 12:56:29 PM
Function
|
Output
|
Description
|
${__time()}
|
1496127516340
|
Current
time in milliseconds
|
${__time(dd)}
|
30
|
It will
extract current date
|
${__time(MM)}
|
05
|
It will extract
current month
|
${__time(MMM)}
|
May
|
It will
extract current month
|
${__time(yy)}/
${__time(yy)}
|
17
|
It will
extract current year but with two decimal values
|
${__time(yyyy)}/
${__time(YYYY)}
|
2017
|
It will
extract current year but with four decimal values
|
${__time(YMD)}/
${__time(yyyyMMdd)}
|
20170530
|
Extract
year month and date together without any differentiation in between
|
${__time(yyMMdd)}
|
170530
|
Extract
year month and date together without any differentiation in between
|
${__time(dd-MM-yyyy)}
|
30-05-2017
|
Extract
year month and date together with delimiter (-) in between
|
${__time(MM/dd/yyyy)}
|
05/30/2017
|
Extract
year month and date together with delimiter (/) in between
|
${__time(HH)}/
${__time(hh)}
|
12
|
Extract
current time (only hours)
NOTE: Capital H gives time in 24 hours
format and small h gives time in
12 hour format
|
${__time(mm)}
|
56
|
Extract
current time (only Minutes)
|
${__time(ss)}
|
29
|
Extract
current time (only Seconds)
|
${__time(HHmmss)}/
${__time(HMS)}
|
125629
|
Extract
current time with hours, minutes and seconds but without any delimiter
|
${__time(HH:mm:ss)}
|
12:56:29
|
Extract
current time with hours, minutes and seconds but with delimiter ( : )
|
${__time(dd-MM-yyyy
HH:mm:ss)}
|
30-05-2017
12:56:29
|
Extract
date and time together
|
${__time(dd-MM-yyyy
hh:mm:ss a)}
|
30-05-2017 12:56:29 PM
|
Extract
date and time together with AM or PM
|
${__time(dd-MM-yyyy'T'hh:mm:ss
a)}
|
30-05-2017T12:56:29PM
|
Extract
date and time together with T in-between
|
But there are many cases where you need to take either future date or Past date, during that time we cannot use time function. We have to use beanshell for getting such data. Below is few example of extracting future date/Past date using beanshell.
import java.text.SimpleDateFormat;
import java.util.Date;
int DaysCount = 5; //this variable needs to be customized as per your need
Date date = new
Date();
date.setDate(date.getDate() + DaysCount);
SimpleDateFormat df
= new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = df.format(date);
vars.put("FutureDate",formattedDate);
//Futuredate is your jmeter variable
For time function as well sometimes we need to take past time or future time. Below is function which can be used for extracting past/future timings
import
java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
int AddSeconds= 00; //this
variable needs to be customized as per your need
int AddMinutes= 05; //this
variable needs to be customized as per your need
int AddHours= 05; //this
variable needs to be customized as per your need
Date now
= new
Date();
Calendar c
= Calendar.getInstance();
c.setTime(now);
c.add(Calendar.SECOND, AddSeconds);
c.add(Calendar.MINUTE, AddMinutes);
c.add(Calendar.HOUR, AddHours);
Date NewTime = c.getTime();
SimpleDateFormat df = new
SimpleDateFormat("HH:mm:ss");
String mytime
= df.format(NewTime);
vars.put("NewTime",mytime);
// NewTime is your jmeter variable
You can use both Date and time beanshell together as per your requirement.
Please feel free to comment your doubts and questions.