jmeter pass variable between threads - multithreading

TestPlan Thread Group HTTP Request1 ->Regular Expression Extractor - Return 10 Results - URLs -- Single Thread
ForEach Controller - Using variable from extractor - Successfully Loops through above results HTTP Request2 ->Regular Expression Extractor - Return 10 Results
This above is under 1 thread
I want to have ForEach Controller under different thread --run multiple therads and use the URLs extracted from 1 thread HTTP Sampler -- I tried to use these two approaches
https://www.blazemeter.com/blog/knit-one-pearl-two-how-use-variables-different-thread-groups
How do I pass a variable from one Thread Group to another in JMeter
but somehow now managed it to work
Please help

ForEach Controller will not work with JMeter Properties, it is designed to work only with JMeter Variables so if you want to pass them between different Thread Groups you will need to do some scripting.
Add JSR223 PostProcessor after the Regular Expression Extractor and put the following code into "Script" area
vars.entrySet().each { var ->
if (var.getKey().startsWith('foo')) {
props.put(var.getKey(), var.getValue())
}
}
replace foo with what you have as a Reference Name in the Regular Expression Extractor. The above code will convert all variables which names start with foo into the relevant JMeter Properties
Add Test Action sampler to the second Thread Group (you don't need to measure time of properties to variables conversion, do you)
Add JSR223 PreProcessor as a child of the Test Action sampler
Put the following code into "Script" area
props.entrySet().each {prop ->
if (prop.getKey().startsWith('foo')){
vars.put(prop.getKey(),prop.getValue())
}
}
the above code convert JMeter Properties into JMeter Variables so you will be able to use them in the ForEach Controller in the second Thread Group. Again, replace this foo with the reference name of your own variable.
See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests

Related

Mule 4 Groovy Script

Is there any way to call mule "flow" instance using groovy ? like When I use the groovy script and it will call the another flow, that means ( calls the another flow -- > sets the variable value and get back with the results). = Expected Behaviour.
Requirement :
Like, Mule Flow A is running independently and keep on incrementing variable value.
Mule flow B wants to access the Flow A variable (Incremental value) ( only variable ) using groovy or python Script.
Note: Script should not execute flow A for getting variable value.
Is there method like to get instance of variable in groovy ? like
flow=registry.lookupByName('A-flow').getInstance(VariableName) ?
// Need to get only variable value.
Groovy Script to call a Mule flow:
import org.mule.runtime.api.message.Message;
import org.mule.runtime.core.api.event.CoreEvent;
import org.mule.runtime.core.api.event.EventContextFactory;
flow=registry.lookupByName('A-flow').grep();
msg = Message.builder().value(payload).build();
event =CoreEvent.builder(EventContextFactory.create(flow, org.mule.runtime.dsl.api.component.config.
DefaultComponentLocation.fromSingleComponent("add-location"))).message(msg).build();
result =flow.process(event);
Mule flow B wants to access the Flow A variable with out deploying or
starting the flow A (as it is running independently ) using groovy or
python Script.
Variables live inside a Mule Event, which is really what contains the status of an execution triggered from a flow. However even if you get a list of all active events you may not know which one is the one you are interested. It is also a bad practice trying to use Mule internals inside an application.
Instead you should share the value of the variable you are interested using a standard method for Mule, like an object store, a queue or database. It really depends on what you are trying to do and the design of your application what is the method that will fit best.

JMeter in preprocessor disabling sampler has no effect

I have a Groovy-driven JSR223 preprocessor set on the ThreadGroup in JMeter plan with the idea that when the certain condition like a timeout is met, allow threadGroup to run to completion but disable some samplers based on the naming convention. Something like the following:
if (abortTest == true) {
String name = sampler.getName();
if (!name.endsWith("crement_count")) { // do not apply to increment or decrement count
log.warn("Test was aborted. Disabling sampler " + sampler.getName());
sampler.setEnabled(false);
}
}
Unfortunately regardless of my sampler.setEnabled(false) call I can see the matching samplers executing their logic. What am I missing here and how can I achieve the desired effect?
You cannot enable or disable test elements while it's running (I mean efficiently) for JMeter 5.4.3 , the easiest would be putting your Samplers under the If Controller and use the __groovy() function as the If Controller's condition.

JMeter share Regular Expression array variables between Thread Groups

In Thread Group #1 I have a Regular Expression with Match Number set to -1 and I want to use the complete variable in Thread Group #2.
I am currently able to share normal variables using props.put but I am not able to share the complete array to then obtain the values using __V function on Thread Group #2.
Is this feasible?
Just add JSR223 PostProcessor (make sure it goes after Regular Expression Extractor) and use the following code:
vars.entrySet().each { var ->
if (var.getKey().startsWith('foo')) {
props.put(var.getKey(), var.getValue())
}
}
In another Thread Group you can convert properties into variables using the same approach:
props.entrySet().each {prop ->
if (prop.getKey().startsWith('foo')){
vars.put(prop.getKey(),prop.getValue())
}
}
Just replace foo with your variable reference name and that would be it.
You can find information regarding Groovy scripting in JMeter in Apache Groovy - Why and How You Should Use It guide if needed.

JMeter Groovy Set a variable for each sampler

Is there a possibility to have variables that depend on the listener where there lay in?
So I want to execute two samplers which are working with a JSR223 Assertion. I use a groovy code which asks for the value of the variable "name". If I execute Sampler A it should say "Tom" and if i execute Sampler B it should say "Paul".
It has to be possible to execute both at the same time.
You have sampler shorthand in the JSR223 Assertion which stands for this or that Sampler. So you can check its name by calling sampler.getName() function which will basically execute underlying AbstractTestElement.getName() method. The relevant code would look like:
if (sampler.getName().equals('Sampler A')) {
log.info('Tom')
}
else if (sampler.getName().equals('Sampler B')) {
log.info('Paul')
}
You can set a JMeter Variable from Groovy code using vars shorthand which in its turn stands for JMeterVariables class instance like:
vars.put('foo', 'bar')
Once done you will be able to refer created variable as ${foo} where required - it will have the value of bar
Check out Scripting JMeter Assertions in Groovy - A Tutorial article for more details.

Why do we use context in Groovy

I have used context in groovy in and as:
def gu = new com.eviware.soapui.support.GroovyUtils(context)
What does it refer to?
Context holds information about the environment and is required to freely navigate in the mentioned environment.
#dmahapatro:
context available in groovy scripts in SoapUI is the context of executions of a test case/test suit.
From the documentation:
When scripting inside some kind of "run", there is always a context variable available for getting/setting context-specific variables. The contexts are:
SubmitContext - available within one submit of a request
TestRunContext - available from within all scripts in a TestCase run
LoadTestRunContext - available in LoadTest setup/tearDown scripts and
from the exectued TestCase context via the LoadTestContext context
variable
MockRunContext - available in MockService startup/shutdown scripts
and MockOperation/MockResponse dispatch scripts
All these inherit from the PropertyExpansionContext interface which
has methods for setting/getting properties and an expand method which
can be used to expand arbitrary strings containing
Property-Expansions, read more on this in the soapUI User-Guide.

Resources