HopefullyHelpful
HopefullyHelpful

Reputation: 119

Authorization request run every x minutes, while load test continues

Test PlanI have an API that needs the authorization header - auth expires after 10 minutes

API to be load tested just needs to run the auth header request every 10 minutes - and pass that auth token. I have the variable set up correctly, but am unable to get the auth to re run at 10 minutes

This is very similar but is different enough that I am asking my question

This is the logic in the JSR223 Post Processor

def start_Time = vars.get("startTime")
log.info "start_Time>>>>>>>"+start_Time

long currentTime = ${__time(,)}
log.info "currentTime>>>>>>>"+currentTime;

long diff=currentTime.toLong()-start_Time.toLong();
log.info "diff>>>>>>>"+diff

if(diff>=100){
    start_Time = ${__time(,)};
    vars.put("startTime",start_Time.toString());
}
vars.put("flag",diff.toString());
log.info "FlagValue>>>>>>>"+vars.get("flag")

which doesn't seem to be correct, I have seen some threads on this, but haven't been able to find the problem with the above.

Logic in the If Controller

${__groovy(vars.get("flag").toInteger() >= 100)}

Upvotes: 1

Views: 727

Answers (1)

Dmitri T
Dmitri T

Reputation: 168082

As per JSR223 Sampler documentation:

The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:

  • Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
  • Or Use Script Text and check Cache compiled script if available property.

When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.

Your __time() function call returns the same value each time, you need to replace it with System.currentTimeMillis() function

So I believe your code needs to be changed to look like:

def start_time = vars.getObject('startTime') ?: vars.get('TESTSTART.MS') as long

def currentTime = System.currentTimeMillis()

def diff = currentTime - (start_time as long)

if (diff >= 100) {
    vars.putObject('startTime', System.currentTimeMillis())
}

vars.putObject('flag', diff)

More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

Upvotes: 1

Related Questions