1

I am quite new to JMeter and I was trying to increment a counter variable pre-defined in User Defined Variables using a Loop Controller and a JSR223 PostProcessor and it seems not working well. I looked at various examples on JMeter loop and counter examples to work this out but the config element Counter was also not incrementing with the loop. Could anybody please let me know what I am doing wrong?

This is the user defined variable counter:

This is the user defined variable counter

This is how I do loop:

This is how I do loop

And this is how I am trying to increment the counter using post processor and the log displays that the counter is not incrementing for each loop. I want to get the counter upto 5:

And this is how I am trying to increment the counter using post processor and the log displays that the counter is not incrementing for each loop. I want to get the counter upto 5

[EDIT] I guess I wasn't clear on why I used ${counter} to test out. I was trying to evaluate the counter variable within another variable as '${__V(transaction_${counter})}' because this needs to be appended to a text file. For example, if transaction_3 has a value "110001", if I do '${__V(transaction_'+vars.get('counter')+')}' to append, the text stored shows "transaction_3" and if I do '${__V(transaction_${counter})}' then I get the correct value "110001" stored in the text file while in next iteration the counter does not increment. Is there a possible way to solve this problem?

This is how I was using the variable within variable and it shows that the values are not changing because the counter is not changing.

And this image is how I am trying to do with vars.get() and it shows that it is just putting the variable name instead of evaluating the value of each item.

2 Answers 2

3

According to JSR223 Sampler documentation:

JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.

props.get("START.HMS");
props.put("PROP1","1234");

So amend the last line of your script to look like:

log.info(vars.get('counter'))

Demo:

enter image description here

Also be aware that it's much easier to use:

  1. Counter test element or __counter() function, check out How to Use a Counter in a JMeter Test article for more details
  2. Loop Controller exposes ${__jm__Loop Controller__idx} JMeter Variable which holds current iteration number
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot! I edited my question to be more specific on the problem!
You still use JMeter Functions and JMeter Variables inside groovy script despite the fact I told you not to do so in the first sentence of my answer
I see it now! Thank you!!
0

According to the JMeter Manual Best Practices -> 16.12 JSR223 Elements

When using JSR 223 elements, it is advised to check Cache compiled script if the available property to ensure the script compilation is cached if the underlying language supports it. In this case, ensure the script does not use any variable using ${varName} as caching would take only the first value of ${varName}. Instead use: vars.get("varName")`

If you change log.info('${counter}') to log.info('${vars.get("counter")}') that should do the trick!

Or uncheck this:

JSR223 preprocessor screenshot

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.