如何在 JMeter 中的两个 JSR223 (groovy) 采样器之间传递非字符串值?

How to pass a non-string value between two JSR223 (groovy) samplers in JMeter?

在 JMeter 中,我需要将一个 JSR223 采样器 (groovy) 的值传递给同一线程组中的另一个采样器。现在我使用用户参数 (vars.put(...), vars.get(...)),但它有一个缺点,即需要额外的字符串转换才能传递非字符串数据。有没有办法在 JMeter 中的两个 groovy 采样器之间传递对象(例如整数或日期)?

根据 How to use BeanShell: JMeter's favorite built-in component 指南:

vars

vars is the most frequently used component which represents JMeter Variables. It’s an instance of org.apache.jmeter.threads.JMeterVariables class and provides read/write access to current variables, capable of enumerating/changing existing, creating new ones and obtaining nested properties.

如果您按照上述 link 查看 JMeterVariables class JavaDoc,您将能够看到 putObject(String key, Object value) 方法,这似乎正是您要查找的内容。

所以在第一个采样器中:

Date now = new Date();
vars.putObject("now", now):

在第二个采样器中:

Date then = vars.getObject("now");

或者,您可以使用 props.put(String, Object)props.get(String, Object) - 在这种情况下,您将能够访问来自不同线程组的值。