JMeter loop index always return 0 in Loop Controller - jmeter

When I use the command ${__jm__myLoopControllerName__idx} in JSR223 Sampler, it always returns 0 as the index.
The sampler is in the Loop Controller. But I can see that the CSV file I'm looping is done for each line because in the listener "View Result Tree" I can see in the Request Headers, that the data is from each line. What am I doing wrong ?
Thanks for your help.
M.

Use vars to get variable in JSR223 script:
String index = vars.get("__jm__myLoopControllerName__idx");
variable inside ${} syntax is getting cached
ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}. Instead use : vars.get("varName")

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.
So either put your ${__jm__myLoopControllerName__idx} to "Parameters" section and refer it as "Parameters" in your script:
or use vars shorthand for JMeterVariables class instance like:
vars.get('__jm__myLoopControllerName__idx')

Related

Code in JMeter JSR223 Sampler comments is executed

Please tell me why the code in comments (both /*something*/ and //something) is executed using JSR223 Sampler & BeanShell sampler?
For example, I have:
and in the next JSR223 Sampler I have:
and the result is:
and the question is: why this code: "/${__setProperty(checkProperty, 50)};/" is executed regardless of that it is in comment and it is in wrong condition?
JMeter Functions are being executed in the place where they're found, no matter where it is, in Sampler label, comments section, sampler body, etc.
Actually inlining JMeter Functions and/or Variables into JSR223 scripts is not the best idea as
it might conflict with Groovy's string interpolation syntax
the function or variable might resolve into something causing script compilation failure or logic error
and last but not the least Groovy will cache the first occurrence and use it for subsequent iterations
So if you need to set a property - use props.put() function like
props.put('foo', 'bar')
And finally I'm not sure that using props.clear() is a good idea because there are some pre-defined JMeter properties (you can check yourself using Debug Sampler and View Results Tree listener combination) and it might result into unexpected behaviour if a test element will be relying on that property existence and/or value

Jmeter using LoopCount instead of the ThreadNum variable(to save variable for every count and use it in another request)

I am setting my Jmeter variable using this using beanshell assertion:
${__setProperty(id_${__threadNum},${id},)}
And using this in another request like this:
${__P(id_${__threadNum},)}
and running the requests like this
However I want to run the loop count instead of the thread. So that i can run the request one by one. How can i change it to so it is loopNum instead of the thread
Can i use this below when i want to use the loop instead of the thread??
I am setting my Jmeter variable using this:
${__setProperty(id_${__iterationNum},${id},)}
And using this in another request like this:
${__P(id_${__iterationNum},)}
For getting the current iteration (1st loop - 1, 2nd loop - 2, etc.):
props.put("id_" + vars.getIteration(), vars.get("id"));
For getting all iterations (each time it will return 10)
props.put("id_" + ctx.getThreadGroup().getSamplerController().getProperty("LoopController.loops"), vars.get("id"));
Few more hints:
Don't inline JMeter Functions or Variables into Scripts, either use "Parameters" section or go for code-based equivalents instead, like in my above examples props stand for JMeter Properties, vars - for JMeter Variables, etc.), see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more details.
Don't use Beanshell for scripting, since JMeter 3.1 you are supposed to use JSR223 Test Elements and Groovy language

How to use StringFromFile function within a JSR223 Sampler?

I can't find any good docs/examples on how to use the StringFromFile function to get multiple records from a source data file from within a JSR223 Sampler. Could somebody please help me?
My code which doesn't work:
String filename = "filename.csv";
String record = StringFromFile(filename,"id");
You shouldn't be using it directly in the scripts, as per JSR223 Sampler documentation:
When using this feature, ensure your script code does not use JMeter variables directly in script code as caching would only cache first replacement. Instead use script parameters.
So the solution would be using File.readLines() function which reads the file into a List of strings
More information: The Groovy Templates Cheat Sheet for JMeter

How do I use a JMeter Variable declared in an extractor in a User Defined Variable config

I have an http request that uses an extractor to set a JMeter variable (lets call it test) from the body. When I look at the debug controller I can see this works fine. Next I want to append something to the beginning of the variable so I add a user defined variable node and add a variable with the name new and I set the value to ${test}. However when I look in the debug response I see ${test} instead of the value.
I tried the same thing setting the value manually in 2 different UDV nodes and it works fine, so how do I append to a JMeter variable declared in an extractor?
As per JMeter 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.
So the User Defined Variables element will be read only once and only when the Test Plan is started.
If you need to overwrite the current variable with a new value you can go for i.e. __groovy() function, the relevant syntax would be something like:
${__groovy(vars.put('foo'\, 'some_prefix_' + vars.get('foo')),)}
Demo:
vars is a shorthand for JMeterVariables class instance, it provides read-write access to all JMeter Variables in the current thread scope. Check out The Groovy Templates Cheat Sheet for JMeter to learn what else you can do with Groovy scripting in JMeter tests
UDVs can't be used in dynamic way because they are process once at the start of the test.
Don't use UDV, use JSR223 Sampler (or PostProcessor) with vars;
vars.put("new", "prefix"+ vars.get("test"))
Another option is to use Set Variables Action plugin

How to put value of User Defined Variable into Bean shell Sampler Variable - Jmeter

I try to move a User Defined variable to variable in beanshell sampler.
(I need the User defined variable to be part of a bigger string.)
When I try to move it, or make a copy of it I get error 500
can someone please advise how can I put the value of user defined field in bean-shell variable and than use it (not need to change the user defined variable just want it value)
In this script I Want to put the value of $Expected_Offer_ID to String variable Expected_Offer
There are (at least) 2 options:
Put ${Expected_Offer_ID} to "Parameter" section of the Sampler. You will be able to access it as Parameters in your script
Use vars.get("Expected_Offer_ID); where required. vars is a shorthand to JMeterVariables class instance, it provides read/write access to all JMeter Variables
Remember 2 things:
Never refer JMeter Variables and/or Functions in the Script body like ${myVar}, either use aforementioned "Parameters" section or code-based equivalents as they might resolve into something which can cause script interpretation failure or unexpected behaviour. Moreover, in case of Groovy language it prevents compiled scripts caching
Don't use Beanshell for scripting. Since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language as Groovy is more Java-compliant and performs much better. See Apache Groovy - Why and How You Should Use It article for more detailed explanation, benchmarks and some tips on Groovy JDK enhancements usage.
JMeter variables are accessed through vars object, use:
String Expected_Offer = vars.get("Expected_Offer");

Resources