1

We need to add n Minutes to a work order start time variable (WOStartTime) for every sample, what are possible ways?

Currently we are using jexl function in jp@gc - Parameterized Controller;

${__jexl(${__threadNum()}*8 + ${WOStartTime})}

where WOStartTime is a datetime fetched from a CSV file;

and getting following exception; 2014/05/20 15:44:11 ERROR - jmeter.functions.JexlFunction: An error occurred while evaluating the expression "1*8 + 5/20/2004 15:05" org.apache.commons.jexl.parser.ParseException: Encountered "15" at line 1, column 17. Was expecting one of: "||" ... "or" ... "&&" ... "and" ... "|" ... "^" ... "&" ... "==" ... "eq" ... "!=" ... "ne" ... "<" ... "lt" ... ">" ... "gt" ... "<=" ... "le" ... ">=" ... "ge" ... "+" ... "-" ... "*" ... "/" ... "div" ... "%" ... "mod" ... ";" ... at org.apache.commons.jexl.parser.Parser.generateParseException(Parser.java:4176)

We have added a BeanShell Preprocessor to manipulate datetime variable;

woStartDate=vars.get("WOStartTime");
StartTime=vars.get("Start");
EndTime=vars.get("End");


Date NewStartTime ; 


if (StartTime == null){
    StartTime =woStartDate;
    print("StartTime == null");
}
else    {
    NewStartTime=StartTime;
    NewStartTime.setTime(NewStartTime.getTime()+2); 
    StartTime=NewStartTime;

}


print(StartTime);

vars.put("Start", StartTime);

    EndTime=StartTime;
    EndTime.setTime(NewStartTime.getTime()+5); 
vars.put("End", EndTime);

StartTime is always NULL , but after vars.put("Start", StartTime) for second sample this should not be NULL, need help to resolve the issue;

Anil
  • 3,722
  • 2
  • 24
  • 49

2 Answers2

2

Maybe because you are not getting correctly the StartTime. You can use

User Defined Variable

and you put into your StartTime and after that you can use it in your BeanShell like that

StartTime="${Start}"

Then use java code to convert and get the format date you want for example:

import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.text.ParseException; 
import java.util.Date; 


SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy:HH:mm"); 

Date OriginalDepositDate = format.parse("${datePlanificationService}"); 
long dateConverti=OriginalDepositDate.getTime();
return dateConverti;

I hope that this will help

Imen CHOK
  • 930
  • 1
  • 12
  • 20
  • Hi thanks for the reply, java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("MM/dd/yyyy") works, but java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); does not work; Another issue seems to be with import, i am unable to import therefor I m using fully qualified names. – Anil May 20 '14 at 12:01
  • you are welcome, maybe the problem is with the format you specified, try to use this format ("dd/MM/yyyy HH:mm:ss"), you are using beanShell Sampler? – Imen CHOK May 20 '14 at 12:51
2

It seems that you're having issues with your Beanshell script.

  1. JMeter variables come as String, you need to cast them to Date
  2. +2 and +5 operators are adding milliseconds, not minutes
  3. If one of variables in first 3 lines will be not set your script won't go on.

Refer to updated script below for details and corrections:

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;

    vars.put("WOStartTime", "1970-01-01 12:00");
    vars.put("Start", "1970-01-01 12:00");
    vars.put("End", "1970-01-01 12:00");
    Date woStartDate = null;
    Date StartTime = null;
    Date EndTime = null;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm");

    try {
        woStartDate = sdf.parse(vars.get("WOStartTime"));
    } catch (NullPointerException ex) {
        System.err.println("WOStartTime is not defined");
    }
    try {
        StartTime = sdf.parse(vars.get("Start"));
    } catch (NullPointerException ex) {
        System.err.println("Start variable is not defined");
    }
    try {
        EndTime = sdf.parse(vars.get("End"));
    } catch (NullPointerException ex) {
        System.err.println("End variable is not defined");
    }


    Date NewStartTime = null;


    if (StartTime == null) {
        StartTime = woStartDate;
        System.out.println(("StartTime == null"));
    } else {
        Calendar cal = Calendar.getInstance();
        cal.setTime(StartTime);
        cal.add(Calendar.MINUTE, 2);
        NewStartTime = cal.getTime();
        StartTime = NewStartTime;

    }


    System.out.println(StartTime);

    vars.put("Start", sdf.format(StartTime));

    EndTime = StartTime;
    if (NewStartTime != null) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(StartTime);
        cal.add(Calendar.MINUTE, 5);
        EndTime = cal.getTime();
    }
    vars.put("End", sdf.format(EndTime));

If you add a Debug Sampler you'll see something like:

JMeterVariables:
JMeterThread.last_sample_ok=true
End=1970-07-01 12:07
Start=1970-02-01 12:02
WOStartTime=1970-01-01 12:00

See How to use BeanShell: JMeter's favorite built-in component guide for more Beanshell tips and tricks.

Hope this helps.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133