Using BeanShell in JMeter if controller to stop thread - jmeter

The accepted answer in this thread is from 2014 and does not seem to work any more. I can't figure out why: JMeter. How to determine when thread group is finished
I have 2 threadgroups (ignore the setUp group)
Based on the accepted answer in the thread above I added
A BeanShell Preprocessor vars.put("DONE", "FALSE");
A BeanShell Postprocessor
int activeThreadCount = org.apache.jmeter.threads.JMeterContextService.getNumberOfThreads();
if (activeThreadCount <= 1)
{
vars.put("DONE", "TRUE");
}
A If Controller ${__BeanShell(vars.get("DONE") != null && vars.get("DONE")=="TRUE")};
... with a Flow Control Action terminating all threads via Stop now if triggered.
via print statements in the postprocessor I was able to verify that the variable "DONE" is correctly set and the condition vars.get("DONE") != null && vars.get("DONE")=="TRUE" is evaluated correctly (when used in the postprocessor).
However when I use the condition inside of the If Controller it does not seem to be evaluated correctly no code inside of child elements of the If Controller is executed. The "Thred Group: ETL" just keeps on going even if the condition should evaluate to true.
My assumption would be that this has to do with the "Interpret Condition as Vairable Expression"-checkbox or the interpreter behind the If Controller. But unfortunately I don't know enough about JMeter to figure this out.

No Semicolon at the end of an If Controller expression is allowed. After removing the semicolon the controller works as intended

First of all get familiarized with JMeter Scoping Rules as your Beanshell Pre and Post processors are executed before/after each Sampler and it doesn't seem to me that this is something you really want to achieve
Second, since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting
If Controller accepts something which resolves to true or false, in your case it's true; so If Controller's children will never be executed
It is possible to stop all the threads directly from the PostProcessor itself as simple as prev.setStopTest(true)

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 ForController not seeing Variables

I'm trying to iterate through a User Defined Array using a ForEach Controller. I'm looping through the array and for each node, creating a new variable with the iteration count appended to the name. However, the ForEach Controller doesn't ever fire.
I can see that the values are created by logging them, but the ForEach Controller doesn't ever make it's HttpRequests. Please note that this is a prototype, our actual use case will have 1000-5000 nodes in the array and will do more then just an HttpRequest. Thanks for your help.
The variables must exist prior a thread (virtual user) reaches the ForEach Controller
PreProcessors are executed before Samplers and are not executed without Samplers, so your PreProcessor gets executed only before "HTTP Request Not In ForEach"
So the options are in:
Replace ForEach Controller with the HTTP Request Not In ForEach so the latter one would appear earlier in the Test Plan
Convert JSR223 PostProcessor to JSR223 Sampler, Samplers are executed consecutively and upside down. If you don't want it to generate a result add SampleResult.setIgnore() line somewhere in your script. See The Groovy Templates Cheat Sheet for JMeter for more Groovy-related tips and tricks
PreProcessor is executed before samplers in its scope, so that's why you don't enter the ForEach Controller.
You can change it to JSR223 Sampler, or add Sampler before the For ForEach Controller

Why is 'while controller' not working in 'loop controller'?

I need to implement this use case.
I have to use a while controller test fragment in a loop controller and then run a request. After that, I need to run another request which is in a while loop. It should repeat 2 times.
Loop set to =2.
It runs successfully the first time, but the second time it just skips the while controller request.
While Controller can be "skipped" only in case when the "Condition" is (or becomes) false so maybe your test fragment is setting some variable to false or increments a counter to some specific value.
I would recommend adding a Debug Sampler as a last request in the Test Fragment and put the same expression you use in the While Controller condition as its label. Run your test and check the label using View Results Tree listener. If it's false - find a way to make it true, otherwise your fragment won't be executed second time.
See Using the While Controller in JMeter article to learn more about implementing "While" loops in JMeter tests.
It worked by setting newValue to variable.
Added BeanShell post processor and added code like
vars.put("Id_job","newValue");

Jmeter If controller condition statement

I am trying to built a test plan in jmeter where i want to run test for specific HTTP request based on their names. I used if controller but I dont know what condition to write.
I am writing ${__samplerName()}=="HTTPRequestName" in the condition but it isn't executing.
kindly help me as soon as possible.
You need to surround ${__samplerName()} with quotation marks as follows:
"${__samplerName()}"=="HTTPRequestName"
See How to use JMeter's 'IF' Controller and get Pie. guide for more details on If Controller use cases and clauses.
In case if you need to run samplers basing on some condition, you can use JMeter Properties as follows:
Launch JMeter providing sampler name property like jmeter -Jrunsomesampler=true
Add If Controller with the following condition: ${__P(runsomesampler,)} == true
Add desired HTTP Requests as a children of the IF Controller

Jmeter - pause when response assertion triggers?

I've got a Jmeter test up and running on an API I'm building.
The API returns this if it gets overloaded:
{"status":{"type":"failure","cause":"internal","http":500}}
What I'd like to do is have JMeter also PAUSE if it gets that result.
I already have set up a Response Assertion that captures these errors. It seems like this should be a simple thing to set up, but I'm not seeing it. I see that I can add an If Controller, but that only works with Javascript vars.
Do I need to add a Regular Expression Extractor, grab that 'failure' as 'type' variable, and then add that to the If Controller?
Seems a little over-complicated?
Am I missing something?
Try to use BeanShell Timer to handle this situation.
It
allows scripting so you can program timer behavior how you need;
has access to jmeter context - via ctx variable - so you can handle ResponseCode condition.
Here you can find good article with example how to use.
And here - how to access beanshell variables and handle response code.
Your condition will be something like this one:
if (prev.getResponseCode().equals("500") == true) {
. . .
}
PLEASE NOTE: script used in BeanShell Timer should return value is used as the number of milliseconds to wait.
Another question is what the reason to do this.
If you have load/stress-test scenario then these results are something you should get during your tests and analyze then.
If you have kind of "functional" test-scenario so you have to handle this situation in more general way, adding any kind of properly configured timer to each sampler - to avoid overload or simulate "real-life" scenario.
Am I missing something?

Resources