3
votes

In JMeter I need to pass a value from one JSR223 sampler (groovy) to another one within the same Thread group. For now I use a User Parameter (vars.put(...), vars.get(...)) but it has a disadvantage that extra conversions to and from string are required to pass non-string data. Is there a way to pass an object (e.g. Integer or Date) between two groovy samplers in JMeter?

1

1 Answers

4
votes

As per How to use BeanShell: JMeter's favorite built-in component guide:

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.

If you look into JMeterVariables class JavaDoc by following above link you'll be able to see putObject(String key, Object value) method which seems to be what you're looking for.

So in the first sampler:

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

And in the second sampler:

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

Alternatively you can use props.put(String, Object) and props.get(String, Object) - in that case you will be able to access the values from different Thread Groups.