Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMeter - Why once only controller runs more than once

Tags:

jmeter

I added two request one for login to store the session id and another request to check the load test which requires session id.

I made login request as once only, by adding it as a child for once only controller.

But when I test it by adding about 100 or 200 thread the login is also running that much of time. I want to run login request for only starting thread. Is it possible? Below I added my test case hierarchy.

 ThreadGroup: 
 HTTP request default 
 HTTP cookie manager
     once only controller
 login HTTP request
 HTTP request for number of users 
like image 811
prashant Avatar asked Jan 21 '11 05:01

prashant


People also ask

What is the use of once only controller in JMeter?

The Once Only Logic Controller tells JMeter to process the controller(s) inside it only once per Thread, and pass over any requests under it during further iterations through the test plan.

What is run time controller in JMeter?

Runtime Controller controls the execution of its samplers/requests for the given time. For example, if you have specified Runtime(seconds) to “20”, JMeter will run your test for 20 seconds only.

How throughput controller works in JMeter?

How Does Throughput Controller Work? What the throughput controller basically does is; control the execution amount for its child requests. For example, in a script where a request is going to be called 100 times in total, it can be reduced up to 50 times with a throughput controller that has a 50% execution parameter.


2 Answers

The "ONLY ONCE" controller doesn't work the way you think it does.

It runs "only once" PER THREAD. Thus, if you have 100 threads, it will run 100 times.

If you want it to run ONCE PER TEST, do the following:

Test Plan (Set thread groups to "run consecutively"
 - Cookie Manager
 - Thread Group A (1 thread, 1 loop)
 - - -  Login Logic
 - Thread Group B
 - - - Rest of test

Please note, if you need to share any variables between threadgroups A and B, you need to set them as properties. Variables cannot be shared between thread groups, but properties can. You'll need to use the properties function for this.

The function __setProperty automatically stores the value as a global variable. The cleanest way to initiate __setProperty would be to create a POST-processor Beanshell script as a child to the sampler that creates the cookie in THREAD A. To retrieve the value in THREAD B, you add the __property function as the VALUE for the parameter that needs the cookie value.

The Beanshell script would look something like this:

props.put("COOKIENAME","COOKIEVALUE");   //creates a property "COOKIENAME" with value "COOKIEVALUE" 
print(props.get("COOKIENAME"));   //prints the value out to the console

The code above would always have the same value for COOKIENAME, less then idea. So, we need to make sure "COOKIEVALUE" is dynamic. I would recommend putting a POST-PROCESSOR regular expression to extract the cookie value, and then pass that into the beanshell script.

So, our test plan now looks like this:

Test Plan (Set thread groups to "run consecutively"
 - Thread Group A (1 thread, 1 loop)
 - - -  Login Logic
 - - - - -  Regex to grab cookie, store as "regexCookie"
 - - - - -  Beanshell to set property
 - Thread Group B
 - - - Rest of test

And our beanshell script now looks like:

props.put("COOKIENAME",vars.get("regexCookie"));   //creates a property "COOKIENAME" with value from "regexCookie"
print(props.get("COOKIENAME"));   //prints the value out to the console

Links to the User Manual:

  • http://jmeter.apache.org/usermanual/functions.html#__setProperty
  • http://jmeter.apache.org/usermanual/functions.html#__property
  • http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PostProcessor
like image 67
BlackGaff Avatar answered Sep 29 '22 13:09

BlackGaff


In the new versions of JMeter you can add a "setUp Thread Group" that does exactly what you need.

A special type of ThreadGroup that can be utilized to perform Pre-Test Actions. The behavior of these threads is exactly like a normal Thread Group element. The difference is that these type of threads execute before the test proceeds to the executing of regular Thread Groups.

http://jmeter.apache.org/usermanual/component_reference.html#setUp_Thread_Group

like image 30
Hugo Baés Avatar answered Sep 29 '22 11:09

Hugo Baés