如何将 csv 文件中的特定行用于线程组中不同数量的线程。如何从 bean shell 处理器创建和存储 csv 数据

问题描述 投票:0回答:1

我有以下要求:-

  1. 我有一个用户 CSV 文件,其中定义了用户名和密码,我使用该文件从 CSV 文件创建了所有用户。
  2. 现在我为不同的应用程序页面定义了几个线程组,它们将具有不同的 API。
  3. 对于所有线程组,我分配了不同的线程数。(比方说,对于线程组1,我从用户CSV文件中有5个线程,可以从第1行到第5行,线程组2可以有3个线程,可以去从 6 号线到 8 号线等)。我想从上述文件中获取用户和密码详细信息,该文件只需要文件中的用户名和密码。
  4. 对于线程组中使用的所有用户,将为我想要附加到文件中的每个用户生成令牌(如果可能,我们可以将其附加到每个用户的同一个用户文件中)然后稍后我想用它进行其他活动。

第二个要求:-

  1. 我有一个 API 调用,其中我在响应标头中获取了 Total_count(假设为 3500)。
  2. 我想将此计数与定义为变量的任何特定 no 进行比较(假设 ${max_count} = 5000)
  3. 现在我想用一个条件迭代循环: 如果total_count < ${max_count}, then loop should iterate for max_count else :- loop count should iterate for total_count only.

我怎样才能用豆壳处理器做到这一点。

我尝试过的解决方案:

  1. 对于第一点,我使用了设置线程,它应该是第一个要运行的线程。

  2. 对于第二点和第三点,我已经为每个线程定义了具有不同 CSV 文件的线程组(我不希望将其作为解决方案),因为它可能会上传超过 100 个 csv 文件并用作 no每组中的线程数不相同。

  3. 对于第4点,我还没有任何解决方案,第二个要求也是如此。

jmeter dataset beanshell
1个回答
0
投票

首先,Beanshell 脚本是某种形式的性能反模式,从 JMeter 3.1 开始,您应该使用 JSR223 测试元素和 Groovy 语言e 进行脚本编写。有关 Jmeter 中 Groovy 脚本编写的更多信息,请参阅 Apache Groovy:Groovy 有何用途?

实现“以下要求”的示例代码如下所示:

def engine = ctx.getEngine(); def test = engine.getClass().getDeclaredField("test"); test.setAccessible(true); def testPlanTree = test.get(engine) def threadGroupSearch = new org.apache.jorphan.collections.SearchByClass<>(org.apache.jmeter.threads.ThreadGroup.class) testPlanTree.traverse(threadGroupSearch) def threadGroups = threadGroupSearch.getSearchResults() def threadGroupIndex = ((ctx.getThread().getThreadName() =~ ctx.getThreadGroup().getName() + ' (\\d+)')[0][1]) as int int offset = 0 0.upto(threadGroupIndex - 1, { if (threadGroupIndex > 1) { offset += threadGroups.toArray()[it - 1].getNumThreads() } }) def threadNum = 0 if (threadGroupIndex == 1) { threadNum = ctx.getThread().getThreadNum() } else { threadNum = ctx.getThread().getThreadNum() - 1 } def lineFromCSV = offset + threadNum def line = new File('test.csv').readLines().get(lineFromCSV) //do what you need with this "line" here
关于“第二个要求”,您可以将以下 

__groovy() 函数放入 Loop Controller 的“循环计数”输入中:

${__groovy((vars.get('total_count') as int) < (vars.get('max_count') as int) ? vars.get('max_count') : vars.get('total_count'),)}
    
© www.soinside.com 2019 - 2024. All rights reserved.