0
votes

I need to calculate some dates in jmeter, so I created a BeanShell Pre Processor in my test structure:

  1. Thread Group
    • Cache Manager
    • Cookie Manager
    • HTTP Header
    • HTTP Manager
    • IF controller (if the user could login)
    • BeanShell Pre Processor

And my code it's like:

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

try {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssX");
    long startTime = calendar.getTimeInMillis();
    long endTime = Long.parseLong(vars.get("dateMiliseconds")); 
    randomStartDate = df.format(startTime + (long)(Math.random()*(endTime-startTime)));
    randomEndDate = randomTime1 + (long)(Math.random()*(endTime - randomTime1)+86400000);
    log.warn("startDate: "+randomStartDate+ " endDate: "+randomEndDate);
    vars.put("RandomStartDate", randomStartDate);
    vars.put("RandomEndDate", randomEndDate); 
} catch (Exception e) {
    log.warn("The error is: "+e);
    throw e;
}

However when I ran the test Jmeter error says:

ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: import java.text.SimpleDateFormat; import java.util.Calendar; try{ Calendar ca . . . : illegal use of undefined variable, class, or 'void' literal

WARN - jmeter.modifiers.BeanShellPreProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: import java.text.SimpleDateFormat; import java.util.Calendar; try{ Calendar ca . . .: illegal use of undefined variable, class, or 'void' literal

Any ideas? Thanks in advance

1

1 Answers

1
votes

Your randomTime1 is not defined anywhere, it need to have some value.

Also it is better to use public final void warn(String message, Throwable throwable) method - this way you will be able to see the full stacktrace in jmeter.log file.

And finally, for performance perspective it's better to use either __time(), __longSum() and __Random() functions combination or consider moving to JSR223 PreProcessor and groovy language if you want to stick to scripting