jMeter beanshell - 如何用毫秒变量计算一个数字

jMeter beanshell - How to calculate a number with milliseconds variable

我有一个 javaScript 变量说:

${__javaScript(Math.round(new Date().getTime() / 1000 -18000 )*1000)}

我的豆壳代码是:

start  = vars.get ("startTime-5");
newStart = start + 1000;

startTime-5 是 javaScript 代码的变量。

当我打印 newStart 时,我希望得到 start+1000 的计算结果,但我得到的是添加“1000”作为字符串。 例如,如果 start=14456789000 - 我得到的是 144567890001000 而不是 14456790000

根据Beanshell Intro

BeanShell emulates typed variables and parameters when they are used. This allows you to "seed" your code with strong types where appropriate. You can "shore up" repeatedly used methods as you work on them, migrating them closer to Java. Eventually you may find that you want to compile these methods and maintain them in standard Java. With BeanShell this is easy. BeanShell does not impose a syntactic boundary between your scripts and Java.

所以从 Java 开始就把 Beanshell 想起来了。

类似于:

long start = Long.parseLong(vars.get("startTime-5"));
long newStart = start + 1000;

vars.put("newStart", String.valueOf(newStart)); // if you need the JMeter Variable

应该能帮到你。

有关 JMeter 中的 Beanshell 脚本编写的更多详细信息和一种 Beanshell 食谱,请参阅 How to Use BeanShell: JMeter's Favorite Built-in Component 指南。