Search code examples
jmetercode-duplicationjsr223

Using IF controller


I want to check a condition before each HTTP request.

I have an access token and a refresh token.

Once the access token is expired, the refresh token end point is called to generate a new access token.(oauth2).

So after every HTTP request, I want check whether the access token is expired. That can be achieved, if I add a jmeter IF controller after each HTTP request. Is there a way to set the IF controller globally(without checking before each request).

I check the expiry time using JSR223 post processor and depending on the result, I execute the IF controller. I want to set this IF controller globally to avoid duplication.

JSR 223 Post processor

import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.jmeter.util.JMeterUtils;


SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
long expiryTime = sdf.parse(vars.get("expiry_time")).getTime();
long currentTime = sdf.parse(vars.get("time")).getTime();
long timeleft = currentTime - expiryTime;
vars.putObject("isExpired",timeleft);

IF controller condition before each HTTP request

${__jexl3(${isExpired}>0,)}

Solution

  • You can use Module Controller and put your If Controller and JSR223 inside Test fragment for reuse

    Module Controller provides a mechanism for substituting test plan fragments into the current test plan at run-time.

    A test plan fragment consists of a Controller and all the test elements (samplers etc.) contained in it. The fragment can be located in any Thread Group. If the fragment is located in a Thread Group, then its Controller can be disabled to prevent the fragment being run except by the Module Controller. Or you can store the fragments in a dummy Thread Group, and disable the entire Thread Group.