java 如何为jmeter中的每个线程发送唯一数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32240287/
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-11-02 19:53:03  来源:igfitidea点击:

how to send unique data for each thread in jmeter

javamultithreadingjmeterload-testing

提问by bagui

I'm using jmeter to test my REST API for 10000 hit for which each http hit will store some data in DB. I have followed the below test plan

我正在使用 jmeter 来测试我的 REST API 是否有 10000 次命中,每个 http 命中都会在数据库中存储一些数据。我遵循了以下测试计划

enter image description here

在此处输入图片说明

I'm running 10 threads in parallel with ramp up time 20 sec each and loop 1000 to achieve the same.

我正在并行运行 10 个线程,每个线程的加速时间为 20 秒,然后循环 1000 次以实现相同的效果。

enter image description here

在此处输入图片说明

But the issue here is my threads are not taking unique data set. Whereas my backend HTTP URL expecting unique string for each http hit.

但这里的问题是我的线程没有采用唯一的数据集。而我的后端 HTTP URL 期望每个 http 命中的唯一字符串。

Now I have tried with the below approaches.

现在我尝试了以下方法。

  1. Single CSV data set config with 10000 unique values and all threads in thread groups are reading the same data.

  2. Different CSV Data set for each threads and allocate the csv file with thread using filename${__threadNum}.csv

  3. Using jmeter _RandomString method to generate random strings on runtime for each http hit, in http post body I'm passing like

  1. 具有 10000 个唯一值的单个 CSV 数据集配置,线程组中的所有线程都在读取相同的数据。

  2. 为每个线程设置不同的 CSV 数据,并使用 filename${__threadNum}.csv 为线程分配 csv 文件

  3. 使用 jmeter _RandomString 方法在运行时为每个 http 命中生成随机字符串,在我传递的 http 帖子正文中

{"tenantName":"${__RandomString(15,abcdefghijklmnofqrst1234567#@#%^&*,)}"}

{"tenantName":"${ __RandomString(15,abcdefghijklmnofqrst1234567#@#%^&*,)}"}

  1. Using BeanShell preprocessor to call a java method and generate unique pattern all the time win HTTP Request sampler.
  1. 使用 BeanShell 预处理器调用 java 方法并始终生成唯一模式赢得 HTTP 请求采样器。

Now none of the above approaches works for me. While running the test plan after some point of time 2 threads are trying to use the same data and hit my HTTP url. And I'm getting conflict error from http response. My error count keeps increasing.

现在以上方法都不适合我。在某个时间点后运行测试计划时,2 个线程试图使用相同的数据并点击我的 HTTP url。我从 http 响应中收到冲突错误。我的错误计数不断增加。

Now I really don't understand how these 2 treads trying to hit http with same data?

现在我真的不明白这两个步骤是如何尝试使用相同的数据访问 http 的?

Can some one please explain the issue and help me to set the correct test plan configuration.

有人可以解释这个问题并帮助我设置正确的测试计划配置。

EDIT:

编辑:

CSV data set config for all thread:

所有线程的 CSV 数据集配置:

enter image description here

在此处输入图片说明

HTTP Request :

HTTP请求:

enter image description here

在此处输入图片说明



Adding test plan with CSV dataset:

使用 CSV 数据集添加测试计划:

enter image description here

在此处输入图片说明

回答by sbos61

Beside all the suggestion from Ubik and DmitrT, I would put the CSV configuration element OUTSIDE the thread group. Let me know.

除了来自 Ubik 和 DmitrT 的所有建议之外,我会将 CSV 配置元素放在线程组之外。让我知道。

回答by sa3036

Though I am too late for the question, I thought someone might find it useful.

虽然我提出这个问题为时已晚,但我认为有人可能会觉得它很有用。

  1. Create a Random Variable for the thread group
  2. Assign min and max value for that variable. Please make sure the difference between the min and max is big so that there will be less collisions.
  3. Mark Per Thread(User) as TRUE
  1. 为线程组创建一个随机变量
  2. 为该变量分配最小值和最大值。请确保 min 和 max 之间的差异很大,以便减少碰撞。
  3. 将每个线程(用户)标记为TRUE

Hope it helps.

希望能帮助到你。

Refer to http://blog.developer.bazaarvoice.com/2016/05/19/quick-and-easy-web-service-load-testing-with-jmeter/

参考http://blog.developer.bazaarvoice.com/2016/05/19/quick-and-easy-web-service-load-testing-with-jmeter/

回答by Rich

I have also had the same issue with a User Defined Variables element. In it, I created a UUID and assign it to a variable. I was expecting each thread to generate a different UUID, but this was not the case.

我也遇到了与用户定义变量元素相同的问题。在其中,我创建了一个 UUID 并将其分配给一个变量。我期望每个线程生成不同的 UUID,但事实并非如此。

Solution (which worked for me) Add a Beanshell Sampler. In it, generate the unique value such with a UUID and use the put method to store the value in "vars". Each thread will execute the code and have its own unique value.

解决方案(对我有用)添加一个 Beanshell 采样器。在其中,使用 UUID 生成唯一值,并使用 put 方法将值存储在“vars”中。每个线程都会执行代码并拥有自己的唯一值。

Example: To generate and store a unique accountId for each thread to use

示例:为每个线程生成并存储一个唯一的 accountId 以供使用

String uniqueId = "${__UUID()}";
vars.put("accountId", uniqueId);

I hope this helps!

我希望这有帮助!

回答by UBIK LOAD PACK

The CSV approach is Ok, but how did you configure csv dataset and put it in plan ?

CSV 方法是好的,但是您如何配置 csv 数据集并将其放入计划中?

Ensure you set "Recycle on EOF" to false to ensure no data is reused.

确保将“Recycle on EOF”设置为 false 以确保没有数据被重用。

Can you show this ?

你能展示这个吗?

Can you also show HTTP Request content ?

您还可以显示 HTTP 请求内容吗?

Edit 01 september 2015:

2015 年 9 月 1 日编辑:

  • your csv config does not declare variableNames which should contain tenantName
  • 您的 csv 配置未声明应包含租户名称的 variableNames

Also ib fileName path field replace \ by \ or /

另外 ib 文件名路径字段将 \ 替换为 \ 或 /

回答by Dmitri T

If you need to send unique data which can be random I believe that __UUID()function can help.

如果您需要发送可以随机的唯一数据,我相信__UUID()函数可以提供帮助。

It generates random exclusive GUIDstructures each time when being called and seems to be exactly what you're looking for.

它每次被调用时都会生成随机的独占GUID结构,并且似乎正是您正在寻找的。

For explanation and demo of this and more JMeter Functions see How to Use JMeter Functionsposts series

有关此和更多 JMeter 函数的解释和演示,请参阅如何使用 JMeter 函数帖子系列

回答by Amarjeet Ray

The easiest way to put csv data set config outside thread group and keep shared mode as "ALL THREADS", it will solve your purpose. Even in case of multiple thread groups, you can use this csv data in shared mode and each thread will pick unique data automatically.

将 csv 数据集配置放在线程组之外并将共享模式保持为“所有线程”的最简单方法,它将解决您的目的。即使在多个线程组的情况下,您也可以在共享模式下使用此 csv 数据,每个线程将自动选择唯一的数据。