JMeter - Why once only controller runs more than once

prashant picture prashant · Jan 21, 2011 · Viewed 17k times · Source

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 

Answer

BlackGaff picture BlackGaff · Jan 21, 2011

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: