如何在 JMeter 中将变量从一个线程组传递到另一个线程组
我有一个包含 2 个线程组的 JMeter 测试 - 第一个是单个线程(创建一些库存),第二个有多个线程(购买所有库存)。 我使用 BeanShell Assertions 和 XPath Extractors 来解析返回值(XML)并存储变量(例如要购买的商品的 id)。
但是,在第一个线程组中创建的值,无论是提取到标准 ${jmeter}
类型变量中,还是 ${__BeanShell(vars.get("jmeter"))} 类型变量在第二个线程组中不可用。 是否有办法在第一个线程组中创建一个变量并使其对第二个线程组可见?
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).
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我无法使用变量来执行此操作(因为这些变量是各个线程的本地变量)。 但是,我能够通过属性解决这个问题!
同样,我的第一个 ThreadGroup 完成了所有设置,并且我需要该工作中的一些信息可供第二个 ThreadGroup 中的每个线程使用。 我在第一个 ThreadGroup 中有一个 BeanShell 断言,其内容如下:
${storeid} 是使用 XPath 提取器提取的。 BeanShell 断言还执行其他操作,例如检查上一次调用是否返回了 storeid 等。
无论如何,在第二个 ThreadGroup 中,我可以在 Samplers 中使用“storeid”属性的值,如下所示:
非常有效!
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:
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.
Anyway, in the second ThreadGroup, I can use the value of the "storeid" property in Samplers with the following:
Works like a charm!
根据 JMeter 文档:
在线程之间传递变量的另一种方法是使用 jmeter-plugins,如下面 Andrey Botalov 提到的那样。
但我发现第一次使用它有点令人困惑,但它在从一个线程传递到另一个线程期间提供了对变量的完全控制。
按照我的 BeanShell 使用示例,您会发现它是多么简单:
接下来参考下图中的部分:
(1.1)这里我在用户定义变量中创建了自定义变量(或者您可以使用BSF处理器来完成它 - 在本例(1.2)中禁用)
(2.1)(2.4)我成功地在第一个中使用了变量线程 - 没什么特别的:)
(2.2)添加了 BeanShell PostProcessor 并自定义了我的变量
(2.3)将其添加到队列
(3.1) 在第二个线程中 - 变量从队列中取出 - 使用您想要的任何名称。 但要小心,明智地使用超时,因为该线程将等待直到先前完成,以便它可以获取修改后的变量(用一些长响应进行实验)
(3.2)(3.3)(3,4)重复使用和修改变量的步骤
(3.5) )变量在新队列中再次发送 - 因此为其提供新名称
(4.1)(4.2)(4.3)从新队列中抓取修改后的变量就像魅力
警告
如果添加更多线程,则向线程组添加一些计数器使用变量并将此变量名称添加到队列名称中 - 在尝试捕获队列的线程组中执行相同的操作,以便队列将为每个线程拥有唯一的名称(如果需要更清晰的解释,请写一条注释)
如果一个线程组中有多个 http 请求,则将线程通信预处理器添加为最后一个(或其他,如果您想实现某些自定义事物)http 请求的子级
播放、修改、自定义以获得最佳结果:) 添加更多线程可能会导致不必要的行为,因此您需要保持警惕。
According to JMeter documentation:
Another way to pass variable between the threads is to use jmeter-plugins as mentioned by Andrey Botalov below.
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:
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))
(2.1)(2.4)I successfully used variable in first thread - nothing special :)
(2.2)Added BeanShell PostProcessor and customized my variable
(2.3)Added it to queue
(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.2)(3.3)(3,4)That repeated steps of using and modifying variable
(3.5) Variable is sent once again in new queue - so provide new name to it
(4.1)(4.2)(4.3) Grabbed modified variable from new queue works like charm
Warning
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)
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
Play, modify, customize to get best result :) Adding more threads can result in unwanted behavior so you need to be watchful.
我发现我认为这是完成此任务的最简单方法。
使用
在一个线程组中设置
来设置变量( http://jmeter.apache.org/usermanual/best-practices.html#bsh_variables)
或者如果您想将其设置为另一个变量中包含的值
获取其他线程组
然后从另一个线程组中,通过 (http://jmeter.apache .org/usermanual/functions.html#__property)
I found which I believe is the most simple way to get this done.
Use
Set in one Thread Group
to set the variable (http://jmeter.apache.org/usermanual/best-practices.html#bsh_variables)
OR if you want to set it to a value contained in another variable
Get in the other Thread Group
And then from the other thread group, read it via (http://jmeter.apache.org/usermanual/functions.html#__property)
为此,JMeter 插件具有 线程间通信。
有两种方法可以使用它:
__fifoPut
和__fifoPop
在我看来,PostProcessor/PreProcessor 更容易使用。
JMeter Plugins has Inter-Thread Communication for this purpose.
There are 2 methods to use it:
__fifoPut
and__fifoPop
In my opinion PostProcessor/PreProcessor is easier to use.
只需添加一个 bean shell 断言,使用属性函数将值分配给变量(如全局变量) )即使在它转到其他线程之后也会保留该值。
,然后在另一个线程中您可以调用这个全局变量,并且可以使用它,
下面是您需要用来调用存储值的函数:
https://medium.com/@priyank.it/jmeter-passing-variables- Between-threads-a4dc09903b59
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.
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:
https://medium.com/@priyank.it/jmeter-passing-variables-between-threads-a4dc09903b59
这在 JMeter 中是不可能的,因为它不是正常的客户端行为(在线程之间共享参数)。 相反,使用一个带有控制器的线程组:
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 可能将此称为一个功能:))。 但属性是! 因此,将变量设置为属性,如下所示:
1) 单击测试计划并启用“连续运行线程组”-> 这使得线程组有序运行,而不是随机运行。 (您可以稍后调整它,但现在要使其正常工作..)
2)例如创建一个名为“setup”的线程组; 在该线程组中添加一个 BeanShell Sampler,代码如下:
现在属性已设置! 如果您要存储为属性的值是变量 allready(例如用户定义的变量或 reqex 变量),您可以执行以下操作:
3) 添加一个测试组“实际测试”,例如线程数大于 1;
添加一个测试,并向该测试添加一个带有以下代码的 BeanShell 预处理器:
现在您已在该线程中创建了一个名为 theNameOfYourNewProperty 的变量,该变量具有系统属性 theNameOfYourNewProperty 的值。 在您的测试中,您现在可以像这样访问它:
它将适用于每个线程,而不仅仅是第一个线程。
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:
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:
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:
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:
And it will work for each thread, not only just the first thread..
让主题重获新生:) 在线程之间传输变量的另一种方法是写入/读取文件。 在线程之间传递变量
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
另一种解决方案是使用简单表服务器来管理数据集。
此功能是在 1.2 JMeter 插件中添加的。
“主要思想是在 JMeter 插件中使用小型 http 服务器来管理数据集文件,并使用简单的命令获取/添加文件中的数据行”
查看文档:
http://jmeter-plugins.org/wiki/HttpSimpleTableServer/
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/
我使用设置属性函数和拆解拆解组从 setupthreadgroup 获取值,但拆解线程组收到 401 错误。
我检查了所有标题,一切看起来都很好。不确定是否拆解失败。
任何想法请建议我
I used set property function and teardown teardowngroup getting values from setupthreadgroup but teardown threadgroup getting 401 error .
I checked all the headers everything look like fine .not sure y teardown failed .
Any idea plz advice me