JMeter BSF Assertion - Date Variable Comparison

I am trying to approve BSF using javascript in JMeter.

In JMeter Debug Sampler, I have these two variables: insert_date = 2013-11-11 16: 22: 33.000343 db_created_date_1 = 2013-11-11 16: 22: 53.863187

and I want to compare them to see if they occurred within a certain period of time plus or minus 10 seconds from each other - or if the time difference is more than 20 seconds.

What is the best way to do this?

I do not know whether to use Date.parse (dateVal) The Date.parse function returns an integer representing the number of milliseconds between midnight, January 1, 1970 and the date specified in dateVal.

I tried this with BSF Assertion - javascript, but it does not work:

if ((vars.put("my1",Date.parse(vars.get("db_created_date_1"))) - vars.put("my2",vars.get("insert_date")))  != 0) {
    AssertionResult.setFailure(true); 
    AssertionResult.setFailureMessage("ERROR: The difference between db_created_date value (${db_created_date_1}) ${my1} and the dateTime (${insert_date}) ${my2} is too great.");
}

Validation result: Validation error: false Validation error: true Confirmation error message: ERROR: Difference between db_created_date (2013-11-11 16: 22: 53.863187) NaN and dateTime (2013-11-11 16: 22: 53.863187) NaN too big.

+4
source share
1 answer

I'm not sure about Javascript, but your NaNs look weird. After Beanshell, the code works for me:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
long created = sdf.parse(vars.get("db_created_date_1")).getTime();
long insert = sdf.parse(vars.get("insert_date")).getTime();
long delta = Math.abs((created - insert));
if (delta > 20){
    //ka-boom
}
0
source

All Articles