How do I pass a variable from one Thread Group to another in JMeter

后端 未结 9 1566
星月不相逢
星月不相逢 2020-11-27 04:54

I have a JMeter test with 2 Thread Groups - the first is a single thread (which creates some inventory) and the second has multiple threads (which purchase all the inventory

相关标签:
9条回答
  • 2020-11-27 05:17

    JMeter Bean Shell Assertion

    Just add a bean shell assertion use a property function to assign the value to a variable (like a global variable) which will hold the value even after it goes to other thread.

    thread group >> Add >> Assertions >> Bean Shell Assertion

    ${__setProperty(Global_variable_Name,${Variable_name_whose_Value_to_be_Passed})}
    

    and then in the other thread you can to call this global variable and can use it

    below is the function you need to use to call the stored value:

    ${__property(global_variable_name)}
    

    https://medium.com/@priyank.it/jmeter-passing-variables-between-threads-a4dc09903b59

    0 讨论(0)
  • 2020-11-27 05:23

    This is not possible in JMeter, because it is not normal client behavior (sharing parameters between Threads). Instead of this use one Thread-Group with Controllers:

    Thread Group
    + Create inventory
    + + XPath
    + Loop
    + + Purchase inventory
    
    0 讨论(0)
  • 2020-11-27 05:27

    Another solution is to use the Simple Table Server to manage the dataset. This feature was add the 1.2 JMeter Plugins.

    "The main idea is to use a tiny http server in JMeter Plugins to manage the dataset files with simple commands to get / add rows of data in files"

    Look at the documentation : http://jmeter-plugins.org/wiki/HttpSimpleTableServer/

    Regards.

    0 讨论(0)
  • 2020-11-27 05:28

    Let' give a topic a second life :) One more way to transfer variables between threads is to write/read to file. Passing variables between threads

    0 讨论(0)
  • 2020-11-27 05:30

    Well this is one way to do it; follow these steps and it will work, later you can adjust it to your needs! Variables are not shared among threads (JMeter calls this a feature probably :) ). But properties are! So set your variable as a propery like so:

    1) Click your testplan and enable 'Run Thread Groups consecutively' -> this makes the thread groups run ordered and not randomly. (you can later adjust it, but for now to get it to work..)

    2) create a threadgroup called 'setup' for instance; in that thread group add a BeanShell Sampler with the following code:

    import org.apache.jmeter.util.JMeterUtils;
    JMeterUtils.setProperty("theNameOfYourNewProperty", "theValueOfYourPropery");
    

    So now the property has been set! If the value you want to store as a propery is a variable allready (User definded variable or reqex variable for instance) you can do:

    JMeterUtils.setProperty("theNameOfYourNewProperty", vars.get("theNameOfYourVariable"));
    

    3) add a testgroup 'actual test' for instance with a number of threads higher than 1; add a test and to that test add a BeanShell Preprocessor with the following code:

    import org.apache.jmeter.util.JMeterUtils;
    vars.put("theNameOfYourNewProperty", JMeterUtils.getProperty("theNameOfYourNewProperty"));
    

    So now you've created a variable in that thread called theNameOfYourNewProperty which has the value of your system property theNameOfYourNewProperty. In your test you can now access it like:

    ${theNameOfYourNewProperty}
    

    And it will work for each thread, not only just the first thread..

    0 讨论(0)
  • 2020-11-27 05:34

    I found which I believe is the most simple way to get this done.

    Use

    Bean Shell PostProcessor
    

    Set in one Thread Group

    to set the variable (http://jmeter.apache.org/usermanual/best-practices.html#bsh_variables)

    import org.apache.jmeter.util.JMeterUtils;
    JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", "value");
    

    OR if you want to set it to a value contained in another variable

    import org.apache.jmeter.util.JMeterUtils;
    JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", vars.get("Extracted_PC_CREATED_PROMO_CODE"));
    

    Get in the other Thread Group

    And then from the other thread group, read it via (http://jmeter.apache.org/usermanual/functions.html#__property)

    ${__property(PC_CREATED_PROMO_CODE)}
    
    0 讨论(0)
提交回复
热议问题