0
votes

I have this jmeter file which does multiple calls on several endpoints (it's a regression test). For every endpoint there is the same JSR223 PreProcessor-script that generates a JWT. The JWT is created with variables (different per thread) that are set in 'User defined variables' (also one per thread) and imported in the script by using vars.get().

On runtime the script is not renewing the variables per thread (or per loop). If the script runs twice the script from the last thread in run 1 is being used in the first thread from run 2.

I imagine it has to do something with caching but I can't figure out what.

I've tried checking and not checking the checkbox: 'Cache compiled script if available'.

// get values from 'User defined variables'
String sharedSecret = vars.get("sharedSecret");
String uitgifteDatumTijdOffset = vars.get("uitgifteDatumTijdOffset");
String uitgifteDatumTijd = Instant.now().minusMillis(Long.parseLong(uitgifteDatumTijdOffset)).toString();
String kvkNummer = vars.get("kvkNummer");
String ean13Code = vars.get("ean13Code");
String cnCertificaat = "Test met uitgiftedatum: " + uitgifteDatumTijd + " en EAN13-code: " + ean13Code;

// construct your body data - JSON entity in case below
JsonObject jo = new JsonObject();

jo.addProperty("KvKNummer", kvkNummer);
jo.addProperty("EAN13-code", ean13Code);
jo.addProperty("UitgifteDatumTijd", uitgifteDatumTijd);
jo.addProperty("CN-Certificaat", cnCertificaat);

String jsonString = jo.toString();

// perform JWT-signing of body data
byte[] bytesEncoded = Base64.encodeBase64(sharedSecret.getBytes());
String secret = new String(bytesEncoded);

try {

    String jwtToken = Jwts.builder()
    .setHeaderParam("alg","HS512")
    .setHeaderParam("typ","JWT")
    .setPayload(jsonString)
         .signWith(SignatureAlgorithm.HS512, secret) // base64EncodedSecretKey
         .compact();

    // put JWT-signed body data into variable
    vars.put("jwtToken", "Bearer " + jwtToken);

    } catch (Exception ex) {
        prev.setSuccessful(false);
        log.error(ex.getMessage());
        System.err.println(ex.getMessage());
    }

I expect this script to use different variables for every thread. But now it's copying the last value.

2

2 Answers

0
votes

Keep the following setting in CSV Data Set Config

Sharing Mode = All Threads

enter image description here

Now, if you are running for 5 threads, make sure you have 5 entries in CSV.

enter image description here

Use the variable in JSR223 pre processor using vars.get() and vars.put

enter image description here

Pass that value in Sampler

enter image description here

Each Thread will pick unique values from CSV and process it.

0
votes

The script generates the same JWT token value because you provide the same input variables to it.

Several relevant quotes from the User Defined Variables documentation:

  • The User Defined Variables element lets you define an initial set of variables, just as in the Test Plan.

  • Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.

  • For defining variables during a test run, see User Parameters. UDVs are processed in the order they appear in the Plan, from top to bottom.

  • For simplicity, it is suggested that UDVs are placed only at the start of a Thread Group (or perhaps under the Test Plan itself).

So my expectation is that your multiple User Defined Variables instances are being merged into one and your Groovy script is using the values which are defined in the bottom User Defined Variables configuration element. You can double check the variables values using Debug Sampler and View Results Tree listener combination.

For providing different initial variables for each user go for User Parameters instead as User Defined Variables set up the "global" set of variables which is shared across different threads and even Thread Groups.