JMeter - Why once only controller runs more than once

前端 未结 6 1120
情深已故
情深已故 2021-02-04 09:08

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 ad

6条回答
  •  太阳男子
    2021-02-04 09:33

    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

提交回复
热议问题