Run massively scalable performance tests on web, mobile, and APIs

Request a Demo
Apr. 25th, 2018

Creating and Testing Dates in JMeter - Learn How

Quite often, you have to deal with Dates when performance testing an API whose parameters have a Date value. This article will explain how to create different dates in different formats and in different time zones inside your Apache JMeter™ test.

 

Imagine that we have an API that stores data about the user in the database when she/he registers on the site. This API contains 3 parameters: Login, Password and Registration Date in year-month-day format. To test this API in JMeter with different dates that will be created automatically, we will use the Java class LocalDate from the java.time package. (To learn how to use the TimeShift function in JMeter, you can read this blog post).

 

For this, you need to do the following

 

1. Add a Thread Group

 

Right Click -> Add -> Threads(Users) -> Thread Group

 

creating dates in jmeter

 

2. Add User Parameters

 

Thread Group -> Right Click -> Add -> Pre Processors -> User Parameters

 

creating dates in a jmeter test

 

In User Parameters, add the variable "date" with the value "currentDay", as shown in the image below.

 

User Parameters - Add Variable

 

jmeter test for database dates

 

The variable "date" will take values depending on the corresponding dates created. The possible values for this variable will be described below.

 

3. Add a JSR223 Sampler

 

Thread Group -> Right Click -> Add -> Sampler -> JSR223 Sampler

 

JSR223 Sampler -> Language Groovy

 

api testing dates with jmeter

 

At this step, the corresponding Dates will be created, depending on the variable "date".

 

In the JSR223 Sampler, add the following code example.


import java.time.LocalDate;

String dateNew = "";
switch ("${date}") {

            case "nextYear": dateNew = LocalDate.now().plusYears(1).toString();
                break;
            
            case "nextMonth": dateNew = LocalDate.now().plusMonths(1).toString();
                break;

            case "nextWeek": dateNew = LocalDate.now().plusWeeks(1).toString();
                break;

            case "tomorrow": dateNew = LocalDate.now().plusDays(1).toString();
                break;

            case "currentDay": dateNew = LocalDate.now().toString();
                break;

            case "yesterday": dateNew = LocalDate.now().minusDays(1).toString();
                break;

            case "lastWeek": dateNew = LocalDate.now().minusWeeks(1).toString();
                break;

            case "lastMonth": dateNew = LocalDate.now().minusMonths(1).toString();
                break;

            case "lastYear": dateNew = LocalDate.now().minusYears(1).toString();
                break;
                
            default:
            
            	log.info("!!!!!!!!!!! Date is not created !!!!!!!!!!!");
            	SampleResult.setStopTestNow(true);            	  
        }
        
vars.put("dateNew", dateNew);

 

dates in jmeter open source test

 

The code shown above does the following:

 

import java.time.LocalDate; - Import of the LocalDate class, which allows us to use the necessary methods for creating Dates.

 

String dateNew = ""; - Creating a variable "dateNew" with the data type String, which is assigned an empty value. After the Date value is created, this value will be assigned to the variable "dateNew".

 

Switch is an operator that is used to select one value from many, depending on the expression that is specified in parentheses after the switch statement. In our case, the variable that was created in User Parameters is specified in parentheses.

 

After the case statement, a possible value is specified, which is obtained by calculating the expression in parentheses after the switch operator. In other words, when we specify the value of the variable "date" in User Parameters (in our case it's currentDay), this value is compared with the value after the case statement (in our case, a match is found where case "currentDay" :), if the match is found, then only the code that follows after the case operator up to the break operator is executed (in our case only the following code will be executed: dateNew = LocalDate.now (). toString ();). The rest of the code that is written in curly braces after the switch operator will not be executed.

 

If for some reason a value that returns an expression in parentheses after the switch operator is not found with a value after the case operator, then the code after the default operator is executed. In our case, for example, if we do not specify the value of the date variable in User Parameters, execute the following code:

 

log.info ("!!!!!!!!!!! Date is not created !!!!!!!!!!!"); - Displays the message "!!!!!!!!!!! Date is not created !!!!!!!!!!! " into the Jmeter console

 

SampleResult.setStopTestNow (true); - Stops the test

 

Thus, the possible values for the date variable that we created in User Parameters will be: nextYear, nextMonth, nextWeek, tomorrow, currentDay, yesterday, lastWeek, lastMonth, lastYear.

 

The result is the following:

 

When the date variable in User Parameters is "currentDay" then the following is performed:

 

dateNew = LocalDate.now (). toString ();

  • LocalDate.now () - Creating the current date that is set on the computer on which the test is run
  • toString () - converting the created date into a variable with data type String and assigning the received value to the variable "dateNew"

 

When the date variable in User Parameters is "tomorrow" then the following is performed:

 

dateNew = LocalDate.now (). plusDays (1) .toString ();

  • LocalDate.now () - Creating the current date that is set on the computer on which the test is run
  • plusDays (1) - adding one day to the current date. If it is necessary to add, for example 10 days, then 1 must be replaced by 10.
  • toString () - converting the created date into a variable with data type String and assigning the received value to the variable "dateNew"

 

When the date variable in User Parameters is "yesterday" then the following is performed:

 

dateNew = LocalDate.now (). minusDays (1) .toString ();

  • LocalDate.now () - Creating the current date that is set on the computer on which the test is run
  • minusDays (1) - minus 1 day from the current date. If it is necessary to take away, for example 10 days, then 1 must be replaced by 10.
  • toString () - converting the created date into a variable with data type String and assigning the received value to the variable "dateNew"

 

Similarly, the rest of the usage variants work, depending on the value of the date variable in User Parameters.

 

Below are listed the actions that are performed by methods from the LocalDate class:

  • LocalDate.now (). PlusYears (1) - creating the current date and adding one year to the current date
  • LocalDate.now (). PlusMonths (1) - creating the current date and adding one month to the current date
  • LocalDate.now (). PlusWeeks (1) - creating the current date and adding one week to the current date
  • LocalDate.now (). MinusWeeks (1) - creating the current date and subtraction one week from the current date
  • LocalDate.now (). MinusMonths (1) - creating the current date and subtraction one month from the current date
  • LocalDate.now (). MinusYears (1) - creating the current date and subtraction one year from the current date
  • vars.put ("dateNew", dateNew); - creating a variable "dateNew", which will be available within the one Thread Group

 

4. Add jp@gc - Dummy Sampler

 

Thread Group -> Right Click -> Add -> Sampler -> jp@gc - Dummy Sampler

 

configuring jmeter to test dates

 

The jp@gc - Dummy Sampler will simulate a request to our tested API.

 

In jp@gc - Dummy Sampler add the following code

 

{"login" : "user", "password" : "1234", "date" : "${dateNew}"}

{"message" : "Registration is successful"}

 

jmeter open source test for dates

 

5. Add a View Results Tree

 

Thread Group -> Right Click -> Add -> Listener -> View Results Tree

 

The View Results Tree will allow us to see the request that is sent to the API.

 

api request for dates in jmeter

 

After starting the test, we will see a request in which the current date is formed, as shown in the image below:

 

testing year, current day, next year, tomorrow in jmeter

 

If we change the value of the variable "date" in User Parameters to a value, for example, lastYear, then the date will be formed (current date minus 1 year), as shown in the image below:

 

Dates in Different Formats

 

Depending on how the API is designed, the Date values can be represented in different formats, for example, Day-Month-Year, Year-Month-Day-Time.

 

Below you will see various methods that allow you to create Dates in different formats and in different time zones:

 

Add a JSR223 Sampler

 

Thread Group -> Right Click -> Add -> Sampler -> JSR223 Sampler

 

JSR223 Sampler -> Language Groovy

 

testing next year in jmeter api

 

In the JSR223 Sampler, add the following code example.

 

import java.time.*;
import java.time.format.*;


String localDateTime = LocalDateTime.now().toString();
log.info("Date in the format Year-Month-Day-Time: " + localDateTime);

String localDateTime1  = LocalDateTime.now().format(DateTimeFormatter.ofPattern("y-MM-dd HH:mm:ss"));
log.info("Date in the format Year-Month-Day-Time: " + localDateTime1);

String localDateTime2  = LocalDateTime.now(ZoneId.of("GMT-5")).toString();
log.info("Date for the GMT time zone: " + localDateTime2);

String localDateTime3  = LocalDateTime.now(ZoneId.of("UTC-05:00")).toString();
log.info("Date for the UTC time zone: " + localDateTime3);

 

testing for tomorrow in api with jmeter

 

String localDateTime = LocalDateTime.now (). ToString ():

  • LocalDateTime.now () - Creating the current date with the current time;
  • toString () - converting the created date into a variable with data type String and assigning the received value to the variable "localDateTime"

 

String localDateTime1 = LocalDateTime.now (). Format (DateTimeFormatter.ofPattern ("y-MM-dd HH: mm: ss")):

  • LocalDateTime.now () - Creating the current date with the current time and assigning the received value to the variable "localDateTime1".
  • DateTimeFormatter.ofPattern ("y-MM-dd HH: mm: ss") - Creating your own format, according to which you want to display the Date (in our case, the Date will be displayed in the format Year-Month-Day Hours: Minutes: Seconds).

In the DateTimeFormatter Class, the possible standard formats that can be used to display the created date, as well as valid characters that can be used to create custom display formats are described very well and in detail.

  • format () - A method that allows us to apply the format we created for displaying the Date value.

 

String localDateTime2 = LocalDateTime.now (ZoneId.of ("GMT-5")). ToString ();

  • ZoneId.of ("GMT-5") - Creating a time zone, according to which the date will be created. In our case, a date will be created for the time zone GMT -5.
  • LocalDateTime.now () - Creating the current date with the current time;
  • toString () - converting the created date into a variable with data type String and assigning the received value to the variable "localDateTime2"

 

String localDateTime3 = LocalDateTime.now (ZoneId.of ("UTC-05: 00")). ToString ();

  • ZoneId.of ("UTC-05: 00") - Creating a time zone according to which the date will be created. In our case, a date will be created for the time zone UTC-05: 00.
  • LocalDateTime.now () - Creating the current date with the current time;
  • toString () - converting the created date into a variable with data type String and assigning the received value to the variable "localDateTime3"

 

log.info () is a method that prints values to the JMeter console. In our case, this method will output to the JMeter console the Date values that will be created.

 

Before running the code described above, you need to open the JMeter console, where the result will be displayed.

 

dates testing tutorial

 

The image above shows the created dates in different formats and in different time zones.

 

Learn more advanced JMeter from our free JMeter academy.

 

Running Your Tests Faster with BlazeMeter

 

If you want to run multiple scripts in parallel to check different things in your database, you can run it as a Multi Test in BlazeMeter. Read more here.

 

testing dates in blazemeter

 

By running your test with BlazeMeter you will get instant results on the fly with more advanced and insightful reporting, both real-time and historical. You can also share your tests and results with team members and managers.

 

Try out BlazeMeter for yourself. Just put your URL in the box below and your test will start in minutes.

     
arrow Please enter a valid URL

Interested in writing for our Blog? Send us a pitch!