I have a JMeter test that generates test data dynamically in the JSR223 Sampler by using the java StringBuilder class then passes the data in the SOAP request body. As per the business logic of the application that I am testing, some of the lines of test data can repeat themselves. There is, however, a line that needs to be unique with each request. In the past, the uniqueness of the line came from a JMeter counter component, and this worked fine, however, I had to implement a more complex logic where the counter that generates the unique line doesn't just count sequentially with each request, instead it counts individually in connection with another line of test data. To achieve this, I have included the counter in the script itself. The script works fine, but here comes the problem: it appears that different threads (users) "spawn" the same iteration of the JSR223 Sampler, which means that it generates a duplicate request. The incidence of this is ~ 1%, but it does happen and I need it to be 0 absolute. So the question is: how can I ensure that each thread (user) generates its own iteration of the JSR223 Sampler? I have noticed a few things: If I have 50 users with a ramp-up period of 1 second the error rate is higher in the beginning of the test. If I add a ramp-up period of 200 seconds the error (duplication) rate is smaller. Furthermore, I have noticed that the Constant Throughput Timer that I am using also plays a part, if I disable it, the duplication rate is even smaller (e.g. 50 users, 200s ramp-up, no Constant Throughput Timer => only 2 errors in 803861 requests during 45 minutes of execution)

Some notes: the data generated cannot be random because in a separate step I need to upload it to the system, so it has to follow a pattern.

I know this can be hard to understand for someone who doesn't know the application, so I will try to provide an illustration of the logic that I had before and the one that I am trying to implement:

Old logic when I was using the counter component to generate the unique line, notice how the Serialn line just counts sequentially regardless of the BatchID line:

Request 1:
BatchID = 100000000
Serialn = SN_100000000_0000

Request 2:
BatchID = 100001000
Serialn = SN_100001000_0001

Request 3:
BatchID = 100000000
Serialn = SN_100000000_0002

Request 4:
BatchID = 100001000
Serialn = SN_100001000_0003

----
*The logic that I am trying to implement, notice how the Serialn line counts individually for each BatchID:*

Request 1:
BatchID = 100000000
Serialn = SN_100000000_0000

Request 2:
BatchID = 100001000
Serialn = SN_100001000_0000

Request 3:
BatchID = 100000000
Serialn = SN_100000000_0001

Request 4:
BatchID = 100001000
Serialn = SN_100001000_0001

The script that I have in the JSR223 Sampler:

SampleResult.setIgnore();

String product = vars.get("PRODUCT") //this is imported from a separate Counter, in the current setup, this counter changes the batchID
String batchID = String.format("%03d%03d%03d", Integer.parseInt(vars.get("MAH")), Integer.parseInt(product), 0);
String sCounter = props.get(product);
Integer counter = 0
if (sCounter != null) {
    counter = Integer.parseInt(sCounter);
}

StringBuilder builder = new StringBuilder();    
builder.append(String.format("<ns1:Pack sn=\"SN_%s_%04X\"/>", batchID, counter));
builder.delete(0,14);
builder.setLength(builder.length()-3);

counter ++;
props.put(product, String.valueOf(counter));

vars.put("Product.Scheme", "GTIN");
vars.put("Product.Code",vars.get("GTIN")); //this is a GTIN14 generated in a separate JSR223 Sampler
vars.put("Product.MAH_ID",vars.get("MAH"));
vars.put("Product.MAH_Name","LAST_MAH_"+vars.get("MAH"));
vars.put("Product.Batch.ID",batchID);
vars.put("Product.Batch.ExpDate", "251231");
vars.put("Product.Pack.SN.1",builder.toString());
vars.put("NHRN",vars.get("GTIN").substring(5, 13));
1

There are 1 best solutions below

1
On

I think this line causes the problem:

props.put(product, String.valueOf(counter));

as per JMeter Documentation:

Properties are not the same as variables. Variables are local to a thread; properties are common to all threads

So it might be the case the product property value gets overwritten by another thread.

So I would rather recommend either pre-generating the test data somewhere in setUp Thread Group or putting your SOAP payload into the HTTP Request sampler and use ${__counter(FALSE,product)} function directly in the request body.