Pages

Tuesday, 30 May 2017

All about ${__time()} function in jmeter

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

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. 


Friday, 28 April 2017

Cheat Sheet for Regular Expression in Jmeter

Apache Jmeter, Regular Expression, RegEx
In Jmeter, Regular Expression play very important role. It is used to identify dynamic values in response which is coming from server. Regular expression captures dynamic value coming from server matching left and right boundaries. 

There are multiple types of regular expression we need to apply on day to day basis in our scripting. This blog will cover all types of regular expression with Example.


1. (\d+) and (\d) and [0-9]+


 It is used to capture decimal dynamic values coming from server. If you want extract just one digit from dynamic values (\d) can be used. if you want to extract multiple decimals in a one go (\d+) is used


Example:


a. <decimal-Values>123456</decimal-Values>


Regular expression to capture 123456: 


<decimal-Values>(\d+)</decimal-Values>


or


<decimal-Values>(\d\d\d\d\d\d)</decimal-Values>


or


<decimal-Values>([0-9]+)</decimal-Values>


b. <decimal-Values>5</decimal-Values>


Regular expression to capture 5:


<decimal-Values>(\d)</decimal-Values>


2. (\D+) and (\D)


It is used to capture all dynamic value coming from server except decimal. It capture alphabets, White Space, New lines, Special Character. 


Example:


a. <Random-Values>a$%^sdg @ASzG<Random-Values>


Regular expression to capture a$%^sdg @ASzG:


<Random-Values>(\D+)<Random-Values>


b. <Random-Values>@<Random-Values>


Regular expression to capture @:


<Random-Values>(\D)<Random-Values>


3. (\w+) and (\w)


It is used to capture alphabets coming from server in capital or small letter. (\w+) is used for capturing multiple alphabets and underscore and (\w) used for capturing single alphabet.


Example:


a. <Sample-Words>abcdEFGH<\Sample-Words>


Regular Expression for capturing abcdEFGH:


<Sample-Words>(\w+)<\Sample-Words>


b. <Sample-Word>H<\Sample-Word>


Regular Expression for capturing H:


<Sample-Words>(\w)<\Sample-Words>


4. (\W+) and (\W)


It is used to extract all dynamic data except alphanumeric. It extract white space as well Special characters except underscore. 


Example:


a. <Target-Value>$@#$@  $#^*</Target-Value>


Regular Expression for extracting $@#$@  $#^*:


<Target-Value>(\W+)</Target-Value>


b. <Target-Value>$</Target-Value>


Regular Expression for extracting $:


<Target-Value>(\W)</Target-Value>


5. (\s) and (\s+)


it is used to capture white space from dynamic data coming from server. We generally don't need to capture white space as a Regular expression but it can be used in combine with other operations like (\d+) or (\w+). For more details see below example.


Example:


a. <Target-data>1234567   abcdefgh</Target-data>


Regular expression for capturing 1234567 and abcdegfh separately when No of white space is not fixed (i.e. sometimes there is 2 white space between strings sometimes 3 white space and sometimes more than 3):


 <Target-data>(\d+)\s+(\w+)</Target-data>


Note: we have not given \s+ in bracket as we don't want to capture it in variable, but it is working as a supportive Regular expression to capture Numeric and Alphabets 


6 (\S+) and (\S)


It is used to capture all the dynamic data except White Space. 


Example:


a. <Target-data>12ad@_!@#$%^&*()</Target-data>


Regular Expression to Capture 12ad@_!@#$%^&*():


<Target-data>(\S+)</Target-data>


7.[a-z]+ and [A-Z]+


It is used to extract any alphabets between a-z. [a-z] is used to extract only small letter. [A-Z] is used to extract only capital letter. Both can be used together to extract mixed letter.


Example:


a. <letter>performanceoptimize</letter>


Regular expression to extract performanceoptimize:


<letter>([a-z]+)</letter>


b. <letter>PERFORMANCEOPTIMIZE</letter>


Regular expression to extract PERFORMANCEOPTIMIZE:


<letter>([A-Z]+)</letter>


c. <letter>PerForManCeoPtiMizE</letter>


Regular expression to extract PerForManCeoPtiMizE:


<letter>([a-zA-Z]+)</letter>


d. if you want to find specific range of characters like between c to j that can also be done using range of c-j. it applies to both capital and small letter


<letter>cdefj</letter>


Regular expression to extract cdefj:


<letter>([c-j]+)</letter>


e. if you want to find only specific character without specifying any range, it is possible as shown below


<letter>agfefhaaeeffg<\letter>


Regular Expression to extract agfefhaaeeffg:


<letter>([aefgh]+)</letter>


8. [^a-z]+ and [^A-Z]+


it is used to match any character, decimal, special character and White space other than mentioned inside box bracket. i.e. [^a-z] will match any character, decimal, special character and white space other than a-z alphabets or [^A-Z] will match any character, decimal, special characters and white space other than A-Z alphabets. 


Example:


a. <letter>1234!@#$%^&*()_-++==</letter>


Regular Expression to extract 1234!@#$%^&*()_-++==:


<letter>([^a-z]+)</letter>


b. <letter>abc1234!@#$%^&*()_-++</letter>


Regular Expression to extract abc1234!@#$%^&*()_-++:


<letter>([^A-Z]+)</letter>


9.[a-z0-9@#$]+ and such Regular expression


If you want to match very specific set of alphabetes, special characters or numeric then you can use this custom facility for it.


Example:


a. If you want to extract tokenId and you know that it will have capital letter, small letter and 0-9 numbers and it won't have any special character or any white space. Then you can use following Custom Regular Expression.


<tokenid>ArtrDeR5HGHDDSsdccasds2342sdscDF</tokenid>


Regular Expression to extract ArtrDeR5HGHDDSsdccasds2342sdscDF:


<tokenid>([a-zA-z0-9]+)</tokenid>


b. if you just want to extract only special character with white space that can also be handled with below custom method


<tokenid>!@#$%^&*   (#@$#$@#$%#)_+_</tokenid>


Regular Expression to extract !@#$%^&*   (#@$#$@#$%#)_+_:


<tokenid>([!@#$%^&*() _+]+)</tokenid>


10. (.*?) and (.+?)


It is used to extract alphanumeric, Special character and white space. Generally you should avoid using both of this in regular expression as it can capture everything. 


Example:


a. <test>asdas!@##sdad@#@`211231adsdSAWSSAD</test>


Regular Expression to capture asdas!@##sdad@#@`211231adsdSAWSSAD: 

<test>(.+?)</test>


11. there are other different type of Regular expression but it is not widely used. you can see below brief about it



Tokens
Meaning
Example
a+
Matches one or more consecutive a character
<test>aaaaaaaa</test>

RegEx to find aaaaaaaa:

<test>(a+)</test>

a*
Matches zero or more consecutive a character
<test>aaaaaaaa</test>

RegEx to find aaaaaaaa:

<test>(a*)</test>

a?
Matches an `a` character or nothing
<test>a</test>

RegEx to find aaaaaaaa:

<test>(a?)</test>

a{n}
Matches exactly n consecutive characters
<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a{5})</test>
a{n,}
Matches n or more consucative characters
<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a{2,})</test>
a{m,n}
Matches consecutive characters between m and n

<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a{2,5})</test>

a+?
Matches as few as possible
<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a+?)</test>
ab|cd
Matches either ab or cd 
<test>ab</test>

RegEx to find aaaaa:

<test>(ab|cd)</test>
\n, \t, \r 
\n is for matching new line character, \t is for matching tab and \r is for matching carriage return


Easiest way to build any regular expression is to use available online tool which is very user friendly and allows you to create regular expression more effectively. Personally, I am using http://www.regextester.com/ for building up my regular expression.

You might face more challenge in finding correct and more accurate Regular expression for your work. You can ask me in comment section or in query section about problems you are facing while matching regular expression.