Calculating Age by custom Program - jmeter

There is one POST request which submits both Date of Birth & Age.
I found the Date of Birth from previous request, extracted it through Regular Expression Extractor, and passing the Variable in the POST request.
But I did not found the Age from the previous request. Tried figuring out the Age element through Firebug also. But it's not located, due to this field is auto calculated by the Date of Birth field and also the field is read-only.
How do I get the value of Age field?
Possible solution could be using some Jmeter in-built function or by writing some Program in any Scripting Language. But I do not know how to do it in Jmeter.
Thanks in advance.

Given that you have variable i.e. "birthdate" with the value of "1970-05-15" you can figure out person's age with the Beanshell PreProcessor as follows:
Add a Beanshell PreProcessor as a child of the sampler which needs to pass birth date and age.
Put the following code into the PreProcessor's "Script" area
import java.text.SimpleDateFormat;
String birthDate = vars.get("birthDate");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date dateOfBirth = sdf.parse(birthDate);
Calendar dob = Calendar.getInstance();
dob.setTime(dateOfBirth);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH)) {
age--;
} else if (today.get(Calendar.MONTH) == dob.get(Calendar.MONTH)
&& today.get(Calendar.DAY_OF_MONTH) < dob.get(Calendar.DAY_OF_MONTH)) {
age--;
}
vars.put("age",String.valueOf(age));
You will be able to refer calculated age as ${age}
See SimpleDateFormat JavaDoc for explanation of "yyyy-mm-dd" pattern and information on how to define the one which matches your application date and time format and How to use BeanShell: JMeter's favorite built-in component guide to learn about scripting in JMeter.

Without seeing the response I can't say whether is can be extracted or not. A viable workaround would be to add a Post Processor to the request that the DOB is extracted from (this can be a jsr223 or beanshell) from there you can grab the DOB as such:
String dob = vars.get{"dateOfBirth"}
From there you will need to parse this into a date and compare it with the current date to get the age. You then need to put the "Age" variable so JMeter can access it.
vars.put("age", String.valueOf(age))

Related

deluge script - date comparison operator versus current date

I wrote the following deluge script, whene I am comparing the date of joining of the employee with the current date but i get a NULL response
Date of joining looks like 20-Nov-1979
searchMap1 = Map();
searchMap1.put("searchField","Dateofjoining");
searchMap1.put("searchOperator",">");
searchMap1.put("searchText",now);
Is there a solution to this?
I've managed to get my response from the application support team (in this case it is the zoho suite)
searchMap1 = Map();
searchMap1.put("searchField","Dateofjoining");
searchMap1.put("searchOperator","After");
searchMap1.put("searchText",zoho.currentdate.toString("dd-MMM-yyyy");
if you're comparing date strings it's easiest to print out both and then convert them to dates depending on their format:
info date_one; //output 20-01-2022
info date_two; //output 20/01/2022
if (date_one.toDate("dd-MM-yyyy") > date_two.toDate("dd/MM/yyyy")
{
//do this
}

How to get only first part of the date ('DD') from an entire date?

I've converted a date into string. Now, I want to parse a string to get the DD part from DD-MM-YYYY.
For e.g.
If the date is 03-05-2017 (DD-MM-YYYY) then the goal is to get only first part of the string i.e. 03 (DD).
You've tagged this question as a ServiceNow question, so I assume you're using the ServiceNow GlideDateTime Class to derive the date as a string. If that is correct, did you know that you can actually derive the day of the month directly from the GlideDateTime object? You can use the getDayOfMonth(), getDayOfMonthLocalTime(), or getDayOfMonthUTC().
You could of course, also use String.prototype.indxOf() to get the first hyphen's location, and then return everything up to that location using String.prototype.slice().
Or, if you're certain that the day of the month in the string will contain an initial zero, you can simply .slice() out a new string from index 0 through index 2.
var date = '03-05-2017';
var newDate = date.slice(0, 2);
console.log(newDate); //==>Prints "03".
var alternateNewDate = date.slice(0, date.indexOf('-'));
console.log(alternateNewDate); //==>Prints "03".

How to insert previous month of data into database Nifi?

I have data in which i need to compare month of data if it is previous month then it should be insert otherwise not.
Example:
23.12.2016 12:02:23,Koji,24
22.01.2016 01:21:22,Mahi,24
Now i need to get first column of data (23.12.2016 12:02:23) and then get month (12) on it.
Compared that with before of current month like.,
If current month is 'JAN_2017',then get before of 'JAN_2017' it should be 'Dec_2016'
For First row,
compare this 'Dec_2016'[month before] with month of data 'Dec_2016' [23.12.2016].
It matched then insert into database.
EDIT 1:
i have already tried with your suggestions.
"UpdateAttribute to add a new attribute with the previous month value, and then RouteOnAttribute to determine if the flowfile should be inserted "
i have used below expression language in RouteOnAttribute,
${literal('Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'):getDelimitedField(${csv.1:toDate('dd.MM.yyyy hh:mm:ss'):format('MM')}):equals(${literal('Dec,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov'):getDelimitedField(${now():toDate(' Z MM dd HH:mm:ss.SSS yyyy'):format('MM'):toNumber()})})}
it could be failed in below data.,
23.12.2015,Andy,21
23.12.2017,Present,32
My data may contains some past years and future years
It matches with my expression it also inserted.
I need to check month with year in data.
How can i check it?
The easiest answer is to use the ExecuteScript processor with simple date logic (this will allow you to use the Groovy/Java date framework to correctly handle things like leap years, time zones, etc.).
If you really don't want to do that, you could probably use a regex and Expression Language in UpdateAttribute to add a new attribute with the previous month value, and then RouteOnAttribute to determine if the flowfile should be inserted into the database.
Here's a simple Groovy test demonstrating the logic. You'll need to add the code to process the session, flowfile, etc.
#Test
public void textScriptShouldFindPreviousMonth() throws Exception {
// Arrange
def input = ["23.12.2016 12:02:23,Koji,24", "22.01.2016 01:21:22,Mahi,24"]
def EXPECTED = ["NOV_2016", "DEC_2015"]
// Act
input.eachWithIndex { String data, int i ->
Calendar calendar = Date.parse("dd.MM.yyyy", data.tokenize(" ")[0]).toCalendar()
calendar.add(Calendar.MONTH, -1)
String result = calendar.format("MMM_yyyy").toUpperCase()
// Assert
assert result == EXPECTED[i]
}
}

JMeter: Converting extracted time stamp value to date format

I have to extract the timestamp value from a response and have to pass it as a parameter to next request.
I have extracted the timestamp value from Regular Expression Extractor.
Time stamp value is 1481086800000
Value to be passed is in the format(Month/Date/Year HH:mm)- 12/07/2016 10:30
Kindly provide your valuable suggestion on how to convert the extracted time stamp value into above date format.
Following code directly converted epoch timestamp to AKST timezone. No need of two samplers as suggested in the comments.
Add JSR223 Sampler, select Groovy and add the following code:
import java.text.*;
//long timeStamp = Long.parseLong(vars.get("time"));
Date date = new Date(1481086800000); //replace the long value with timeStamp you captured.
DateFormat formatter = new SimpleDateFormat("MM/dd/YYYY HH:mm");
TimeZone tzInAmerica = TimeZone.getTimeZone("America/Anchorage");
formatter.setTimeZone(tzInAmerica);
String dateFormatted = formatter.format(date);
vars.put("newDate", dateFormatted); //access new value using ${newDate}, in your script.
log.info(dateFormatted);
Screenshot reference:

jmeter how to add +4 to the date extracted from the jmeter response using regular expression extractor?

I am facing issue while trying to add 4 days to the date extracted from the response.
I have extracted the date using regular expression extractor and placed in a variable depdate . Now i need one more date with +4 of extracted date .
Add a Beanshell PostProcessor after the Regular Expression Extractor
Put the following code into the PostProcessor's "Script" area:
import java.text.SimpleDateFormat;
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy"); // change it according to your Date format
Date originalDate = sdf.parse(vars.get("depdate"));
Calendar cal = Calendar.getInstance();
cal.setTime(originalDate);
cal.add(Calendar.DAY_OF_YEAR, 4); // change it if you need to add something else
Date newDate = cal.getTime();
vars.put("newDepdate", sdf.format(newDate));
log.info("Original date: " + vars.get("depdate"));
log.info("New date: " + vars.get("newDepdate"));
Refer the new date as ${newDapdate} where required
Remarks:
Above code assumes date in dd/mm/yyyy format, i.e. 23/08/2016. If your date format is different - use your own pattern, check out Customizing Formats article for details.
Above code assumes adding 4 days to the current date. If you need to add 4 minutes, hours, months, years, whatever - change Calendar.DAY_OF_YEAR to your value, see Calendar class JavaDoc for available options
For anything else check out How to Use BeanShell: JMeter's Favorite Built-in Component guide

Resources