如何在性能测试期间计算JMeter中变量的绝对值?

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

How can I calculate the absolute value of a variable in JMeter during performance testing?

问题

如何在jmeter中获取变量的幅度或绝对值?

我在JMeter中没有找到任何用于查找变量绝对值的函数。
例如,如果一个变量包含值-12,我想要将12存储在该变量中,而不是将-12存储在其中。

英文:

How to get magnitude or absolute value of a variable in jmeter?

I didn't found any function to find the absolute value of a variable in JMeter.
Like if a variable holds the value -12 I want to store 12 instead of storing -12 in that variable.

答案1

得分: 0

您可以使用__groovy()函数来替换负变量为整数的绝对值。

类似于:

${__groovy(vars.put('myVar', Math.abs(Integer.parseInt(vars.get('myVar'))) as String),)}

演示:

如何在性能测试期间计算JMeter中变量的绝对值?

有关在JMeter中使用Groovy脚本的更多信息:Apache Groovy:Groovy用于什么?

另一种解决方案是使用__strReplace()函数,它可以作为Custom JMeter Functions包的一部分使用,使用JMeter Plugins Manager安装。

${__strReplace(${myVar},-,,myVar)}

如何在性能测试期间计算JMeter中变量的绝对值?

英文:

You can use i.e. __groovy() function in order to replace the negative variable with the absolute value of the integer.

Something like:

${__groovy(vars.put('myVar'\, Math.abs(Integer.parseInt(vars.get('myVar'))) as String),)}

Demo:

如何在性能测试期间计算JMeter中变量的绝对值?

More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

Alternative solution would be using __strReplace() function, it can be installed as a part of Custom JMeter Functions bundle using JMeter Plugins Manager

${__strReplace(${myVar},-,,myVar)}

如何在性能测试期间计算JMeter中变量的绝对值?

huangapple
  • 本文由 发表于 2023年5月29日 18:36:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356607.html
匿名

发表评论

匿名网友

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

确定