Running preprocessor once per thread loop in JMeter - jmeter

If I use a JSR223 Preprocessor with the following code:
log.info("" + ${rand});
where ${rand} is a random variable, how can I make this variable change every time I loop this thread?
Changing the number of threads will indeed have the variable change each run, with loop it just takes one value and keeps it for all the other loops.
Putting it in a JSR223 Sampler yields the same result. I basically want the code to behave as a User Parameter.

Don't inline JMeter Functions or Variables in JSR223 Test Elements because:
They may resolve into something which will cause compilation failure
The syntax conflicts with Groovy's GStrings feature
If you tick Cache compiled script if available box the first occurrence will be cached and used on subsequent iterations, if you don't - you will loose performance benefits of Groovy
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 move your ${rand} variable to "Parameters" section and change your code to
log.info("" + Parameters);
or use vars shorthand to JMeterVariables class instance, in this case change your code like:
log.info("" + vars.get("rand"));

You need to use vars to avoid getting cached/same value
vars.get("rand")
See JSR223 Best practices
 script does not use any variable using ${varName} as caching would take only first value of ${varName.. Instead use vars.get("varName")

Related

How to save __evalVar function value into a variable and re-use it for comparison

We have scenario where two variables are extracted from two samplers, and these variables needs to be compared. For this, have created two samplers with each one having 1 regex with matchno: -1, and used ForEach controller>under it 'counter' been used where i have used function ${__evalVar(test_${test_all})}
For this, need to store this ${__evalVar(test_${test_all})} value to a variable so that can be re-used in other places to compare.
Is storing a value from the __evalVar function possible and how can it be achieved?
There is __jexl3() function which can evaluate your expression and store the value into a variable of your choice, i.e. varFromJexl3:
${__jexl3("${__evalVar(test_${test_all})}",varFromJexl3)}
You can consider switching to __groovy() function which is probably the most powerful and flexible JMeter Function I can think of. The relevant syntax to evaluate test_${test_all} variable and store it into varFromGroovy variable would be something like:
${__groovy(vars.get('test_' + vars.get('test_all')), varFromGroovy)}
Demo:

How to evaluate not equals in bean shell?

I'm trying to evaluate a not equals in a if condition in bean shell but though the logic seems to be correct. I'm not getting the expected results.
This is for bean shell post processor in jmeter
r = ctx.getPreviousResult().getResponseCode();
if (!r.equals(200))
{
log.info("vin IS --> "+"${vin}");
p.println(r +","+ "${vin}" + ",");
}
I'm intending to print only non 200 response code but it prints 200 response codes too.
thanks in advance for your help
The code :
if (!r.equals(200))
Should be:
if (!r.equals("200"))
And by the way, you should not use Beanshell anymore, prefer JSR223 Test Elements + Groovy as per this :
https://www.ubik-ingenierie.com/blog/jmeter_performance_tuning_tips/
You're comparing a String with an Integer, you need to either cast it to the Integer first like:
r = Integer.parseInt(ctx.getPreviousResult().getResponseCode());
You're using Beanshell which is a some form of performance anti-pattern. It's recommended to use JSR223 Test Elements and Groovy language for any form of scripting as Groovy has much better performance comparing to Beanshell.
You're inlining JMeter Variables into scripts, it is not very safe as variables might resolve into something which cause compilation failure or unexpected behavior. Moreover in case of Groovy variables will either be resolved only once or clash with GString templates / compilation caching feature. So consider changing:
log.info("vin IS --> "+"${vin}");
to
log.info("vin IS --> "+vars.get("vin"));

How to use while loop in jmeter

I am new to Jmeter and trying to do a while loop operation with a condition. So please someone provide solution for the below query.
Query: I am trying to do DELETE request for 50 times using the id as reference. So I kept the condition as "${startId}<=${endId}" in the while loop. But the while loop is executing infinitely. Is there any simple mechanism to iterate the loop for 50 times by increment the startId till it reaches endId.
While Controller accepts function or variable. So you need to either:
provide a variable which has value of "true" and becomes "false" somewhere else
provide a function which returns "false" to exit from While loop.
With your condition it won't evaluate your expression hence it will never become "false". The solution is to wrap your statement into i.e. __javaScript function as:
${__javaScript(${startId}<=${endId},)}
For more information on JMeter functions see How to Use JMeter Functions post series.
You may take help of loop controller and pre-processor.
snaps :

Check if a variable is used in Smarty template

Is there a way to find out which variables are used by a Smarty template? Consider a function taking in a template as an argument and assigning variables to it. Some of the variables require much computation, and hence I don't want to compute them and assign them to the template if they are not needed. I would like something like this:
function addVariables($tpl) {
if($tpl->usesVariable('foo'))
$tpl->assign('foo', computationallyHeavyFunction());
return $tpl;
}
If $tpl = "some text using some variable {$bar}", foo should not be assigned, but if $tpl = "some text using some variable {$foo}", foo should be computed and assigned. Is this possible?
I don't think so. But probably a better(/working) approach is to create a lazy-loading wrapper plugin and use it instead of direct variable calling.
This way your plugin would be called only if it is used anywhere and if you do the computation here/call the computation heavy parts here you can be sure that the computation will be done only if really required.

Lua Debugging - Detect when the value of a variable changes

Is it possible to detect when the value of a variable has changed using the lua debug library.
Something like A callback function which would give details like the function in which the value was changed, previous value, etc.
Is such a thing possible?
I read about hooks, but I'm not sure hooks can be set to variables.
If you don't mind using a debugger, then some debuggers allow you to set Watch expressions, which will be triggered when the condition in the expression is true. I'll show how this can be done in MobDebug (it is using lua debug library, but there is no direct way to detect a variable change as far as I know).
Let say we have a script start.lua like the one below and want to detect where foo gets value 2:
print("Start")
local foo = 0
for i = 1, 3 do
local function bar()
print("In bar")
end
foo = i
print("Loop")
bar()
end
print("End")
Download mobdebug.lua and make it available to your scripts (the simplest way is to put it into the folder with your scripts).
Start the server using lua -e "require('mobdebug').listen()" command.
Start the client using lua -e "require('mobdebug').loop()" command.
You will see the prompt in the server window: '>'. Type load start.lua to load the script.
Type step and then step again. You will see "Paused at file start.lua line 3".
Let's see what the value of foo is. Type eval foo and you should see 0.
Now we can set up our watch. Type setw foo == 2. You can specify any Lua expression after setw command; the execution of your script will be stopped when the condition is evaluated as true.
Continue execution of the script using "run" command.
The watch now fires, which will show you the message like: "Paused at file start.lua line 8 (watch expression 1: [foo == 2])". This means that the previous expression changed the value of foo to 2 and the execution is stopped at line 8. You can then inspect your script and the current values (you can use "eval" and "exec" commands to run any Lua code to be evaluated in your script environment) to find what triggered the change.
The benefit of this approach is that you are not limited to monitoring table values and can specify any expression. The main disadvantage is that your script runs under a debugger and the expression is evaluated after each step, which may get really slow.
You can do this to a certain extent in Lua by using metatables and keeping a "proxy" table, and using the __newindex function call to detect attempts to add a variable.
This is covered here in the Programming in Lua book under the section "Tracking Table Accesses":
http://www.lua.org/pil/13.4.4.html
See Also
http://www.gammon.com.au/forum/?id=10887

Resources