4
votes

I am attempting a BSF Assertion using javascript in JMeter.

In the 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 specific time range of plus or minus 10 seconds of each other - or if the time difference is more than 20 seconds.

What is the best way to do this?

I don't know if I should use the Date.parse(dateVal) The Date.parse function returns an integer value representing the number of milliseconds between midnight, January 1, 1970 and the date supplied in dateVal.

I have been trying this with a BSF Assertion - javascript, but it is not working:

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.");
}

The Assertion result: Assertion error: false Assertion failure: true Assertion failure message: ERROR: The difference between db_created_date value (2013-11-11 16:22:53.863187) NaN and the dateTime (2013-11-11 16:22:53.863187) NaN is too great.

1

1 Answers

0
votes

I'm unsure regarding Javascript but your NaNs look weird. Following Beanshell 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
}