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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/707832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:34:57  来源:igfitidea点击:

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

javatestingjmeterbeanshell

提问by Todd R

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?

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

采纳答案by Todd R

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 断言,其内容如下:

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

${storeid} 是用 XPath Extractor 提取的。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!

奇迹般有效!

回答by Mork0075

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
+ Create inventory
+ + XPath
+ Loop
+ + Purchase inventory

回答by Fico

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

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

回答by Andrei Botalov

JMeter Plugins has Inter-Thread Communicationfor this purpose.

为此,JMeter 插件具有线程间通信

There are 2 methods to use it:

有2种使用方法:

  • PostProcessor/PreProcessor
  • Functions __fifoPutand __fifoPop
  • 后处理器/预处理器
  • 功能__fifoPut__fifoPop

In my opinion PostProcessor/PreProcessor is easier to use.

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

回答by pbaranski

According to JMeter documentation:

根据 JMeter 文档:

16.12 Sharing variables between threads and thread groups

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. Write variables to a file and re-read them.
  3. Use the bsh.shared namespace - see 16.8.2 Sharing Variables
  4. Write your own Java classes

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

变量是线程的局部变量,在一个线程中设置的变量不能在另一个线程中读取。这是设计使然。有关可以在测试开始之前确定的变量,请参阅参数化测试(上文)。如果在测试开始之前不知道该值,则有多种选择:

  1. 将变量存储为属性 - 属性对于 JMeter 实例是全局的
  2. 将变量写入文件并重新读取它们。
  3. 使用 bsh.shared 命名空间 - 参见 16.8.2 共享变量
  4. 编写自己的 Java 类

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

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

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 用法示例,您会看到它是多么容易:

Project stuctureNext 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) 这里我在 User Defined Variables 中创建了自定义变量(或者你可以用 BSF 处理器来实现 - 在这个例子中禁用 (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) 在第二个线程中 - 变量取自队列 - 使用您想要的任何名称。但要小心,明智地使用超时,因为这个线程会一直等到上一个完成,所以它可以得到修改的变量(一些长响应的实验)

(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

  1. 如果您添加更多线程,然后将一些计数器添加到带有变量的线程组并将此变量名称添加到队列名称 - 在尝试捕获队列的线程组中执行相同操作,以便队列将为每个线程具有唯一名称(如果您需要一些更清晰的解释)

  2. 如果您在一个线程组中有多个 http 请求,则添加线程通信预处理器作为 last 的子进程(或者其他,如果您想实现某些自定义的东西)http 请求

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

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

Information about project structure

项目结构信息

回答by olyv

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

让主题重获新生 :) 在线程之间传输变量的另一种方法是写入/读取文件。在线程之间传递变量

回答by Vincent Daburon

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/ addrows of data in files"

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

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

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

Regards.

问候。

回答by Joviano Dias

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

回答by Vajjala Vinay Rao

JMeter Bean Shell Assertion

JMeter Bean Shell 断言

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