如何在JMeter中将变量从一个线程组传递到另一个线程组

时间:2021-11-18 23:29:10

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). I use BeanShell Assertions and XPath Extractors to parse the returned value (which is XML) and store variables (such as the ids of the items to be purchased).

我有一个带有2个线程组的JMeter测试 - 第一个是单个线程(创建一些库存),第二个有多个线程(购买所有库存)。我使用BeanShell Assertions和XPath Extractors来解析返回的值(即XML)并存储变量(例如要购买的项目的ID)。

But, values that are created in the first Thread Group, whether extracted into standard ${jmeter} type variables, or ${__BeanShell(vars.get("jmeter"))} type vars, are not available in the second Thread Group. Is there anyway to create a variable in the first Thread Group and make it visible to the second?

但是,在第一个线程组中创建的值,无论是提取到标准$ {jm​​eter}类型变量,还是$ {__ BeanShell(vars.get(“jmeter”))}类型变量,在第二个线程组中都不可用。无论如何在第一个线程组中创建一个变量并使其对第二个线程组可见?

9 个解决方案

#1


57  

I was not able to do this with variables (since those are local to individual threads). However, I was able to solve this problem with properties!

我无法用变量做这个(因为它们是各个线程的本地)。但是,我能够用属性解决这个问题!

Again, my first ThreadGroup does all of the set up, and I need some information from that work to be available to each of the threads in the second ThreadGroup. I have a BeanShell Assertion in the first ThreadGroup with the following:

同样,我的第一个ThreadGroup完成了所有设置,我需要来自该工作的一些信息可用于第二个ThreadGroup中的每个线程。我在第一个ThreadGroup中有一个BeanShell Assertion,其中包含以下内容:

${__setProperty(storeid, ${storeid})};

The ${storeid} was extracted with an XPath Extractor. The BeanShell Assertion does other stuff, like checking that storeid was returned from the previous call, etc.

使用XPath Extractor提取$ {storeid}。 BeanShell Assertion执行其他操作,例如检查从前一次调用返回的storeid等。

Anyway, in the second ThreadGroup, I can use the value of the "storeid" property in Samplers with the following:

无论如何,在第二个ThreadGroup中,我可以使用Samplers中“storeid”属性的值,具有以下内容:

${__property(storeid)}

Works like a charm!

奇迹般有效!

#2


13  

According to JMeter documentation:

根据JMeter文档:

16.12 Sharing variables between threads and thread groups

16.12在线程和线程组之间共享变量

Variables are local to a thread a variable set in one thread cannot be read in another. This is by design. For variables that can be determined before a test starts, see Parameterising Tests (above). If the value is not known until the test starts, there are various options:

变量是线程的本地变量,一个线程中的变量集不能在另一个线程中读取。这是设计的。对于可在测试开始前确定的变量,请参阅参数化测试(上文)。如果在测试开始之前未知该值,则有多种选择:

  1. Store the variable as a property - properties are global to the JMeter instance
  2. 将变量存储为属性 - 属性对JMeter实例是全局的
  3. Write variables to a file and re-read them.
  4. 将变量写入文件并重新读取。
  5. Use the bsh.shared namespace - see 16.8.2 Sharing Variables
  6. 使用bsh.shared命名空间 - 请参见16.8.2共享变量
  7. Write your own Java classes
  8. 编写自己的Java类

Another way to pass variable between the threads is to use jmeter-plugins as mentioned by Andrey Botalov below.

在线程之间传递变量的另一种方法是使用下面的Andrey Botalov提到的jmeter-plugins。

But I found that it is a bit confusing to use it first time but it gives full control of variable during passing from thread to thread. Follow my example with BeanShell usage and you see how easy it is:

但我发现第一次使用它有点令人困惑,但它在从线程到线程的过程中完全控制变量。按照我的BeanShell用法示例,你会发现它是多么容易:

如何在JMeter中将变量从一个线程组传递到另一个线程组 Next referring to sections in picture bellow:

接下来参考下图中的部分:

(1.1) Here I created custom variable in User Defined Variables (or you can do it with BSF Proccessor - disabled in this example (1.2))

(1.1)这里我在用户定义变量中创建了自定义变量(或者你可以用BSF Proccessor - 在这个例子中禁用)(1.2))

(2.1)(2.4)I successfully used variable in first thread - nothing special :)

(2.1)(2.4)我在第一个线程中成功使用了变量 - 没什么特别:)

(2.2)Added BeanShell PostProcessor and customized my variable

(2.2)添加了BeanShell PostProcessor并自定义了我的变量

(2.3)Added it to queue

(2.3)将其添加到队列中

(3.1) In second thread - variable is taken from queue - with any name you want. But be careful, use wisely Timeout, because this thread will wait til previous finish so it can get modified variable (experiment with some long response)

(3.1)在第二个线程中 - 变量从队列中获取 - 具有您想要的任何名称。但是要小心,明智地使用Timeout,因为这个线程将等到前一个完成所以它可以得到修改变量(试验一些长响应)

(3.2)(3.3)(3,4)That repeated steps of using and modifying variable

(3.2)(3.3)(3,4)重复使用和修改变量的步骤

(3.5) Variable is sent once again in new queue - so provide new name to it

(3.5)变量在新队列中再次发送 - 因此为其提供新名称

(4.1)(4.2)(4.3) Grabbed modified variable from new queue works like charm

(4.1)(4.2)(4.3)新队列中的抓取修改变量就像魅力一样

Warning

警告

  1. If you add more threads then add some Counter to Thread Group with variable and add this variable name to queue name - do the same in Thread Group where you try to catch queue so queue will have unique name for each thread (write a comment if you need some clearer explenation)

    如果添加更多线程,则将一些计数器添加到带有变量的线程组,并将此变量名称添加到队列名称 - 在线程组中执行相同的尝试捕获队列,以便队列将为每个线程具有唯一的名称(如果您有写入注释)需要一些更清晰的表达方式)

  2. If you have more than one http Request in one Thread Group then add thread communication pre processor as a child of last (or other if you want to achieve some custom thing) http Request

    如果在一个线程组中有多个http请求,则将线程通信预处理器添加为最后一个子节点(或者如果您想实现某些自定义事件,则添加其他)http请求

Play, modify, customize to get best result :) Adding more threads can result in unwanted behavior so you need to be watchful.

播放,修改和自定义以获得最佳结果:)添加更多线程可能会导致不必要的行为,因此您需要注意。

如何在JMeter中将变量从一个线程组传递到另一个线程组

#3


8  

JMeter Plugins has Inter-Thread Communication for this purpose.

JMeter插件具有用于此目的的线程间通信。

There are 2 methods to use it:

有两种方法可以使用它:

  • PostProcessor/PreProcessor
  • PostProcessor中/预处理器
  • Functions __fifoPut and __fifoPop
  • 函数__fifoPut和__fifoPop

In my opinion PostProcessor/PreProcessor is easier to use.

在我看来,PostProcessor / PreProcessor更容易使用。

#4


7  

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

我发现我认为这是完成这项工作的最简单方法。

Use

使用

Bean Shell PostProcessor

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

设置变量(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 are reading from a variable

或者,如果您正在读取变量

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

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

然后从另一个线程组中读取它(http://jmeter.apache.org/usermanual/functions.html#__property)

${__property(PC_CREATED_PROMO_CODE)}

#5


4  

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:

这在JMeter中是不可能的,因为它不是正常的客户端行为(在线程之间共享参数)。而不是使用一个带控制器的Thread-Group:

Thread Group
+ Create inventory
+ + XPath
+ Loop
+ + Purchase inventory

#6


4  

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:

那么这是一种方法;按照这些步骤,它将工作,以后您可以根据您的需要进行调整!线程之间不共享变量(JMeter可能称之为功能:))。但属性是!所以将你的变量设置为如此的属性:

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..)

1)单击您的测试计划并启用“连续运行线程组” - >这使线程组按顺序运行,而不是随机运行。 (你可以稍后调整它,但现在让它工作..)

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

2)创建一个名为'setup'的线程组;在该线程组中添加一个BeanShell Sampler,其代码如下:

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:

所以现在物业已经定下来了!如果要作为属性存储的值是变量allready(例如用户定义变量或reqex变量),则可以执行以下操作:

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:

3)添加一个测试组的“实际测试”,例如,使用大于1的线程数;添加测试并向该测试添加具有以下代码的BeanShell预处理器:

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的变量,它具有系统属性theNameOfYourNewProperty的值。在您的测试中,您现在可以访问它,如:

${theNameOfYourNewProperty}

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

它将适用于每个线程,而不仅仅是第一个线程..

#7


1  

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

让'给主题第二次生命:)在线程之间传递变量的另一种方法是写/读到文件。在线程之间传递变量

#8


1  

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

另一种解决方案是使用Simple Table Server来管理数据集。此功能添加了1.2 JMeter插件。

"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"

“主要的想法是在JMeter插件中使用一个微小的http服务器来管理数据集文件,使用简单的命令来获取/添加文件中的数据行”

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

查看文档:http://jmeter-plugins.org/wiki/HttpSimpleTableServer/

Regards.

问候。

#9


0  

如何在JMeter中将变量从一个线程组传递到另一个线程组

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.

只需添加一个bean shell断言,使用一个属性函数将值赋给一个变量(就像一个全局变量),即使它转到其他线程后也会保存该值。

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

线程组>>添加>>断言>> Bean Shell断言

${__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

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

#1


57  

I was not able to do this with variables (since those are local to individual threads). However, I was able to solve this problem with properties!

我无法用变量做这个(因为它们是各个线程的本地)。但是,我能够用属性解决这个问题!

Again, my first ThreadGroup does all of the set up, and I need some information from that work to be available to each of the threads in the second ThreadGroup. I have a BeanShell Assertion in the first ThreadGroup with the following:

同样,我的第一个ThreadGroup完成了所有设置,我需要来自该工作的一些信息可用于第二个ThreadGroup中的每个线程。我在第一个ThreadGroup中有一个BeanShell Assertion,其中包含以下内容:

${__setProperty(storeid, ${storeid})};

The ${storeid} was extracted with an XPath Extractor. The BeanShell Assertion does other stuff, like checking that storeid was returned from the previous call, etc.

使用XPath Extractor提取$ {storeid}。 BeanShell Assertion执行其他操作,例如检查从前一次调用返回的storeid等。

Anyway, in the second ThreadGroup, I can use the value of the "storeid" property in Samplers with the following:

无论如何,在第二个ThreadGroup中,我可以使用Samplers中“storeid”属性的值,具有以下内容:

${__property(storeid)}

Works like a charm!

奇迹般有效!

#2


13  

According to JMeter documentation:

根据JMeter文档:

16.12 Sharing variables between threads and thread groups

16.12在线程和线程组之间共享变量

Variables are local to a thread a variable set in one thread cannot be read in another. This is by design. For variables that can be determined before a test starts, see Parameterising Tests (above). If the value is not known until the test starts, there are various options:

变量是线程的本地变量,一个线程中的变量集不能在另一个线程中读取。这是设计的。对于可在测试开始前确定的变量,请参阅参数化测试(上文)。如果在测试开始之前未知该值,则有多种选择:

  1. Store the variable as a property - properties are global to the JMeter instance
  2. 将变量存储为属性 - 属性对JMeter实例是全局的
  3. Write variables to a file and re-read them.
  4. 将变量写入文件并重新读取。
  5. Use the bsh.shared namespace - see 16.8.2 Sharing Variables
  6. 使用bsh.shared命名空间 - 请参见16.8.2共享变量
  7. Write your own Java classes
  8. 编写自己的Java类

Another way to pass variable between the threads is to use jmeter-plugins as mentioned by Andrey Botalov below.

在线程之间传递变量的另一种方法是使用下面的Andrey Botalov提到的jmeter-plugins。

But I found that it is a bit confusing to use it first time but it gives full control of variable during passing from thread to thread. Follow my example with BeanShell usage and you see how easy it is:

但我发现第一次使用它有点令人困惑,但它在从线程到线程的过程中完全控制变量。按照我的BeanShell用法示例,你会发现它是多么容易:

如何在JMeter中将变量从一个线程组传递到另一个线程组 Next referring to sections in picture bellow:

接下来参考下图中的部分:

(1.1) Here I created custom variable in User Defined Variables (or you can do it with BSF Proccessor - disabled in this example (1.2))

(1.1)这里我在用户定义变量中创建了自定义变量(或者你可以用BSF Proccessor - 在这个例子中禁用)(1.2))

(2.1)(2.4)I successfully used variable in first thread - nothing special :)

(2.1)(2.4)我在第一个线程中成功使用了变量 - 没什么特别:)

(2.2)Added BeanShell PostProcessor and customized my variable

(2.2)添加了BeanShell PostProcessor并自定义了我的变量

(2.3)Added it to queue

(2.3)将其添加到队列中

(3.1) In second thread - variable is taken from queue - with any name you want. But be careful, use wisely Timeout, because this thread will wait til previous finish so it can get modified variable (experiment with some long response)

(3.1)在第二个线程中 - 变量从队列中获取 - 具有您想要的任何名称。但是要小心,明智地使用Timeout,因为这个线程将等到前一个完成所以它可以得到修改变量(试验一些长响应)

(3.2)(3.3)(3,4)That repeated steps of using and modifying variable

(3.2)(3.3)(3,4)重复使用和修改变量的步骤

(3.5) Variable is sent once again in new queue - so provide new name to it

(3.5)变量在新队列中再次发送 - 因此为其提供新名称

(4.1)(4.2)(4.3) Grabbed modified variable from new queue works like charm

(4.1)(4.2)(4.3)新队列中的抓取修改变量就像魅力一样

Warning

警告

  1. If you add more threads then add some Counter to Thread Group with variable and add this variable name to queue name - do the same in Thread Group where you try to catch queue so queue will have unique name for each thread (write a comment if you need some clearer explenation)

    如果添加更多线程,则将一些计数器添加到带有变量的线程组,并将此变量名称添加到队列名称 - 在线程组中执行相同的尝试捕获队列,以便队列将为每个线程具有唯一的名称(如果您有写入注释)需要一些更清晰的表达方式)

  2. If you have more than one http Request in one Thread Group then add thread communication pre processor as a child of last (or other if you want to achieve some custom thing) http Request

    如果在一个线程组中有多个http请求,则将线程通信预处理器添加为最后一个子节点(或者如果您想实现某些自定义事件,则添加其他)http请求

Play, modify, customize to get best result :) Adding more threads can result in unwanted behavior so you need to be watchful.

播放,修改和自定义以获得最佳结果:)添加更多线程可能会导致不必要的行为,因此您需要注意。

如何在JMeter中将变量从一个线程组传递到另一个线程组

#3


8  

JMeter Plugins has Inter-Thread Communication for this purpose.

JMeter插件具有用于此目的的线程间通信。

There are 2 methods to use it:

有两种方法可以使用它:

  • PostProcessor/PreProcessor
  • PostProcessor中/预处理器
  • Functions __fifoPut and __fifoPop
  • 函数__fifoPut和__fifoPop

In my opinion PostProcessor/PreProcessor is easier to use.

在我看来,PostProcessor / PreProcessor更容易使用。

#4


7  

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

我发现我认为这是完成这项工作的最简单方法。

Use

使用

Bean Shell PostProcessor

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

设置变量(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 are reading from a variable

或者,如果您正在读取变量

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

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

然后从另一个线程组中读取它(http://jmeter.apache.org/usermanual/functions.html#__property)

${__property(PC_CREATED_PROMO_CODE)}

#5


4  

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:

这在JMeter中是不可能的,因为它不是正常的客户端行为(在线程之间共享参数)。而不是使用一个带控制器的Thread-Group:

Thread Group
+ Create inventory
+ + XPath
+ Loop
+ + Purchase inventory

#6


4  

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:

那么这是一种方法;按照这些步骤,它将工作,以后您可以根据您的需要进行调整!线程之间不共享变量(JMeter可能称之为功能:))。但属性是!所以将你的变量设置为如此的属性:

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..)

1)单击您的测试计划并启用“连续运行线程组” - >这使线程组按顺序运行,而不是随机运行。 (你可以稍后调整它,但现在让它工作..)

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

2)创建一个名为'setup'的线程组;在该线程组中添加一个BeanShell Sampler,其代码如下:

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:

所以现在物业已经定下来了!如果要作为属性存储的值是变量allready(例如用户定义变量或reqex变量),则可以执行以下操作:

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:

3)添加一个测试组的“实际测试”,例如,使用大于1的线程数;添加测试并向该测试添加具有以下代码的BeanShell预处理器:

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的变量,它具有系统属性theNameOfYourNewProperty的值。在您的测试中,您现在可以访问它,如:

${theNameOfYourNewProperty}

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

它将适用于每个线程,而不仅仅是第一个线程..

#7


1  

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

让'给主题第二次生命:)在线程之间传递变量的另一种方法是写/读到文件。在线程之间传递变量

#8


1  

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

另一种解决方案是使用Simple Table Server来管理数据集。此功能添加了1.2 JMeter插件。

"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"

“主要的想法是在JMeter插件中使用一个微小的http服务器来管理数据集文件,使用简单的命令来获取/添加文件中的数据行”

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

查看文档:http://jmeter-plugins.org/wiki/HttpSimpleTableServer/

Regards.

问候。

#9


0  

如何在JMeter中将变量从一个线程组传递到另一个线程组

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.

只需添加一个bean shell断言,使用一个属性函数将值赋给一个变量(就像一个全局变量),即使它转到其他线程后也会保存该值。

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

线程组>>添加>>断言>> Bean Shell断言

${__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

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