JMeter: How do I stop PreProcessors from running extra times - jmeter

JMeter's behavior doesn't make sense here at all. A minimized version of my project looks like this:
Thread group
- Outer PreProcessor
- Outer web request
- Loop controller
..- Inner PreProcessor
..- Inner web request
With a thread and loop count of 1 on the Thread group and a loop count of 6 on the loop controller, I would expect Outer PreProcessor to run once and Inner PreProcessor to run 6 times (in each case, the same number of times that the associated web requests run).
Instead, outer runs 7 times, and inner runs 6 times (as determined by console logging). It doesn't make any sense with the project structure, and those extra PreProcessor runs seem to be messing up my variables.
Why would JMeter do things this way, and how do you make it stop?

In your particular use case, you need JMeter PreProcessor to be a child of a Sampler and not under thread group or test plan. Try to use this convention or move code to Sampler or use JMeter functions.

JMeter Pre Processors are following Scoping Rules, i.e. Pre Processor will be executed for all samplers which are located on the same level (or lower).
Therefore:
Outer PreProcessor preprocessor will be run for Outer web request and for Inner web request
Inner PreProcessor will be run for Inner web request (as well as for all requests on the same level)
Solution will be moving Outer PreProcessor to be a child of the Outer web request.
More information: A Quick Guide to JMeter PreProcessors

Related

Thread is passing same Request Body multiple times. JSR223 PreProcessor is used to generate random request body every time

My test has 2 API requests.
The parameters passed in 2nd API request body are to be unique every time. So I used JSR223 PreProcessor with groovy to generate that using RandomUtils.
The Thread group is set to have 3 threads with 15 sec Ramp up time and used Loop Controller with Loop count as 10. 1st API is in thread group and 2nd API is in Loop Controller as it needs to run multiple times.
But during test execution, for 2nd API one thread is passing same request body with same parameters multiple times. Because of which the test fails. How is that possible?
It's impossible to state "how is that possible" without seeing your Groovy code.
The most common mistake is using JMeter Functions or Variables in Groovy scripts. 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.
If this is your case - refactor your code to use vars shorthand for JMeterVariables class instance instead of JMeter Functions or Variables syntax and it should resolve your issue.

running 2 samplers consecutively like a bundle in Jmeter

I am trying to ensure that 2 samplers(A&B) in a thread are always ran consecutively like a bundle.
The thread group I wanted to do is:
where the controller makes sampler A&B a bundle and always runs them sequentially/consecutively, and where sampler C,D,E are other samplers in thread group 1.
and the desired results in chronological order should be similar to this:
in a way that no matter in what order the rest of the samplers are ran, B is always ran right after A is ran.
I have tried several controllers:
critical section controller would allow me to run B right after A everytime, however it seems that it cannot work with multiple threads and only works when I give enough ramp up time, and I don't find enough information that makes enough sense on this controller (please correct me if you have a better understanding of critical section controller)
loop controller would run B right after A only if I give enough ramp up time
interleave controller would run B right after A only if the only controller in the thread group
I have also read multiple posts on stackoverflow on this creating a bundle matter, however couldn't find a solution to it.
The samplers in a thread are executed in the order they appear in the tree.
I'm not sure if you want C, D and E to execute randomly in the same thread. If they are independent from A&B you can move them to other threadgroups.
Execution order
If you want to exclude all samples from other threads between A and B, you could try setting a property before A, and resetting it after B.
Also A and B should be in a Critical Section controller.
With an if controller you could check if the property is set, to exclude other samples between A and B.
With only one ThreadGroup I think something like this should work:
TG
Critical Section
sampler A
preprocessor Set X=1
sampler B
postprocessor Set X=0
if X!=1
sampler C
if X!=1
sampler D

Jmeter - Unable to move to next iteration when an ERROR occurs

I am using the following code in BeanShell postprocessor to stop the current iteration on an error and move to the next iteration. But in my case, I am handling the duration programmatically(infinite while loop and time functions) and not using the thread group loop/duration. So loop count is set to default one at thread group level.
My jmeter script would be -> 3 transaction + whilecontroller(10 transactions).
My error would occur at 5th transaction inside while loop. so i need to stop at that 5th transaction level and start the thread again.
ctx.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_CURRENT_LOOP);
So in this case where am not using thread group loop count/duration and using the above code in a Beanshell Postprocessor, it is not moving to next iteration(like stop the current wihle loop run and start from initial). Am I missing something, could someone suggest?
It won't, if the error occurs at 5th iteration - "your" code will proceed to 6th iteration without executing any Samplers which are below the Beanshell PostProcessor. According to interpretation of your description with my very limited level of English I believe you need to use START_NEXT_ITERATION_OF_THREAD action instead.
It's recommended to use JMeter's built-in test elements and avoid scripting where possible. Particular your case can be handled using If Controller and Flow Control Action Sampler combination
If you prefer or have to go for scripting - consider using the most performing language which is Groovy, it's recommended to use JSR223 Test Elements and Groovy language for scripting since JMeter 3.1

Jmeter :- Running a bundle of samplers sequentially within concurrent threads

I have a single Thread Group with 2 samplers. Lets say Sampler A and Sampler B.
Sampler B is dependent on response of Sampler A.Thus these 2 samplers always have to run sequentially , First - Sample A and then Sample B.
Now my requirement is run this thread group for multiple users concurrently.
When I execute this for one user , it runs fine. However when I run for more than one user , the samplers are not executed sequentially within individual thread group thereby causing Sampler B to fail most of the times.
I need an advice on how can I achieve this.
I tried using Syncronisation Timer , Transaction Controller to bundle the Samplers but it dint work
Found a solution - Used Critical Controller to bundle up the Samplers together. Hereby the link which provides an example - https://jmeter.apache.org/usermanual/component_reference.html#Critical_Section_Controller
You can achieve desired results by using the controllers in your test plan. You can use If Controller for this.
https://jmeter.apache.org/usermanual/component_reference.html#Simple_Controller
Critical section controller still doesn't fulfill this requirement.
It will still run the threads sequentially in the below order(thread 1, 2 and 3 are executed sequentially) due to name lock on sampler A and sampler B defined within the critical section controller. But the requirement is to run all threads(1, 2 and 3) in parallel, but sampler A and sampler B should execute sequentially for each thread.
Critical section controller behavior:
thread 1 - (sampler A then sampler B),
thread 2 - (sampler A then sampler B),
thread 3 - (sampler A then sampler B),

Using JMeter PreProcessor and User Variables logic

I am new to JMeter and it maybe a stupid question but I still find it hard to understand the concept here.
I have a simple test.
Thread Group with a Single thread with loop count of 2
PreProcessor that place two
variables on the vars map
A loop that execute a request twice based
on the PreProcessor parameters
I expected that the preprocessor will initialize the parameter and it will use the same values twice in the request.
It looks like it’s executing the PreProcessor once pair call.
When I switch the PreProcessor with similar User Defined Variables it reuses the same value on every call.
Can anyone explain the logic here?
I am using JMeter 2.11
A PreProcessor is executed each time the HTTP Request is executed so if you have a total of 2 iterations, you should see log twice, you have it 4 times so maybe number of iteration is different than what your write or you have 2 threads.
When you use User Defined Variables, the value is computed once and then reused. Value will be different per thread.
After reading the documentations and with #UBIK LOAD PACK help I used User Variables and it worked
User Variables - are specific to individual threads.
Pre-Processor is attached to a Sampler element (e.g. http request in our case), then it will execute just prior to that sampler element running
So 4 request for different parameters because it runs before every request
User Defined Variables - It is processed at the start of a test, no matter where it is placed. For simplicity, it is suggested that the element is placed only at the start of a Thread Group. This is why I get the same value all the time

Resources