8
votes

I have a JMeter test plan with following http request samplers.

  1. Login
  2. Call some functionality which needs a logged in user
  3. Logout

When I execute the test plan with 5 parallel threads, I see that the sampler 2 is called before calling sampler 1 for some threads, which then fails the response assertions.

Is there any way to specify a sequence of samplers to be executed ?

2
What makes you think that Sampler 2 is executed ahead of Sampler 1? Requests are executed sequentially, top down, and this is very unlikely to fail so perhaps there's an issue with how you're interpreting the results.Oliver Lloyd
Because I see in the view result tree that when I start with 5 parallel threads the login is called 4 times then the other page which fails the assertion that there should be a logout button.rangalo
"View result tree" doesn't reflect the per-thread sequence, it's not a reliable source (because some thread may execute faster than the others).Alex Balabanov
But the issue for me remains that the assertion fails exactly for the tests where view result tree doesn't show the correct sequence.rangalo
@rangalo You should read the third paragraph from my answer. You probably have a problem with context.Oliver Lloyd

2 Answers

11
votes

This should ensure that they are executed sequentially :

enter image description here

So let's start with thread group.

Number of Threads(users) is 5.

So assuming you have the logic work out for your login sampler. Just add additional sampler to it. So right click on that sample Add > Post Processors > BSF PostProcessor, inside this post processor big script space write ${__setProperty(ThreadValue,${__threadNum},)}.

This will save the thread number to your property called ThreadValue. Make sure you select your language as beanshell in the dropdown list.

Then after the login sampler add the if controller. Add this to the condition field (${JMeterThread.last_sample_ok}==true) && (${__property(ThreadValue,,)} == ${__threadNum})

What this means is that -> do only logged in stuff while the actual login is successful and if the login thread matches the thread you're currently in.

That's it you do your login stuff only inside the if controller. If you want to be sure that you logout the right user place additional if controller arround it.

Cheers

2
votes

What you need to consider is that each thread is a separate entity, like a real user, and that it has it's own session, but that JMeter is designed to execute these threads asynchronously, ie. in parallel. The View Results Tree listener displays all activity, from all threads as it happens regardless of the logical thread sequence. If you would rather have 1 thread make 5 iterations then simply change the thread count to 1 and use a loop of 5 and this will preserve the sequence (although this defeats the point of using a load testing tool like JMeter!).

If you do change the thread count to 1 then you can see the true, logical sequence of execution in the results tree. This will show each sampler being executed, in sequence, top down.

Now, in your case, I suspect you have a problem not because things are being run out of sequence but rather because the server is losing the session context between requests. This is very common and more often than not you can address the problem using a HTTP Cookie Manager or with the use of a Regular Expression Extractor.