Jmeter:在CSV行中执行计算,并在测试中使用计算出的值。

huangapple go评论87阅读模式
英文:

Jmeter : Perform calculation in CSV rows and use calculated values in test

问题

我一直在尝试创建 Jmeter 测试,以便使用 CSV 进行更逼真的定时执行。我有一个包含时间的 CSV 行。我想根据连续两个时间之间的差异重复地在不同线程上运行测试。我的 CSV 大致如下。我想计算类似 Time = Time2-Time1,Time= Time3-Time2 的时间差。将所有这些时间差保存在另一个 Excel 中。

图片描述

我尝试运行 BeanShell 脚本,并参考了一些文章,但并没有成功。

例如:${Test}=${__BeanShell(${__CSVRead(Test.csv,1)}${__CSVRead(Test.csv,next)}-${__CSVRead(Test.csv,1)})}

这对于单个值有效,但我不确定如何自动化处理 CSV 的所有值,以及如何将其保存在特定行中。非常感谢您提供任何帮助。

英文:

I have been trying to create Jmeter tests to perform on more realistic timings using CSV.
I have a CSV which contains a row with timing. I would like to run tests based on difference between 2 consecutive timings in repeated manner on different threads.
My CSV is something like below. I would like calculate timings like Time = Time2-Time1, Time= Time3-Time2, etc. Save all these timings in another excel.

Jmeter:在CSV行中执行计算,并在测试中使用计算出的值。

I was trying to run BeanShell script and followed couple of articles but didn't work out.

For example : ${Test}=${__BeanShell(${__CSVRead(Test.csv,1)}${__CSVRead(Test.csv,next)}-${__CSVRead(Test.csv,1)})}

This worked for single value but not sure how to automate this for all values of CSV and how to save it in specific row.
Any help would be very very appreciable.

答案1

得分: 0

自从 JMeter 3.1 版本开始,您应该使用 JSR223 测试元素和 Groovy 语言进行脚本编写,因此考虑将您的代码更改为以下形式:

def lines = new File('old.csv').readLines()

lines.eachWithIndex { line, index ->
    new File('new.csv') << line
    if (index < lines.size() - 1) {
        def currentTime = line.split(',')[1] as long
        def nextTime = lines.get(index + 1).split(',')[1] as long
        def delta = nextTime - currentTime
        new File('new.csv') << ',' << delta << System.getProperty('line.separator')
    }
}

new File('new.csv') << ',' << '0'

脚本的适当位置是 JSR223 Sampler,可以将其放置在 setUp Thread Group 中。如果您不希望采样器出现在测试结果中,请在脚本开头添加 SampleResult.setIgnore()

英文:

Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider changing your code to something like:

def lines = new File(&#39;old.csv&#39;).readLines()

lines.eachWithIndex { line, index -&gt;
    new File(&#39;new.csv&#39;) &lt;&lt; line
    if (index &lt; lines.size() - 1) {
        def currentTime = line.split(&#39;,&#39;)[1] as long
        def nextTime = lines.get(index + 1).split(&#39;,&#39;)[1] as long
        def delta = nextTime - currentTime
        new File(&#39;new.csv&#39;) &lt;&lt; &#39;,&#39; &lt;&lt; delta &lt;&lt; System.getProperty(&#39;line.separator&#39;)
    }

}

new File(&#39;new.csv&#39;) &lt;&lt; &#39;,&#39; &lt;&lt; &#39;0&#39;

The good place for the script is JSR223 Sampler somewhere in setUp Thread Group, if you don't want the sampler to present in the test results add SampleResult.setIgnore() in the beginning of the script

答案2

得分: 0

我所做的类似于:

  • 创建循环控制器。
  • 使用CSVread函数读取两行,并使用_Jexl2存储它们的差异。
  • 运行循环控制器,直到CSV文件的末尾。
  • 在该控制器中创建一个计数器,并将此计数器用于数组${variable${Index}}。
  • 使用jexl将先前计算的差异存储在另一个带有数组的变量中。
  • 运行Beanshell脚本,并将此数组抛出到CSV文件中。
    我知道这个解决方案非常长,但它有效 Jmeter:在CSV行中执行计算,并在测试中使用计算出的值。
英文:

What I did was something like :

  • Create loop controller.
  • Read two rows using CSVread function and store there difference using _Jexl2.
  • Run the loop controller until EOF of the csv.
  • Create a counter in this controller and use this counter for array ${variable${Index}}.
  • Store previous calculated difference in another variable with array using jexl
  • Run beanshell script and throw this array in the csv.
    I know it really long solution but it worked Jmeter:在CSV行中执行计算,并在测试中使用计算出的值。

huangapple
  • 本文由 发表于 2020年9月27日 08:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64083636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定