1

Does anyone knows how to create an array in bean shell post processor and make it available in other thread groups?

I've been searching for a while and i'm not managing to solve this.

Thanks

luistm
  • 909
  • 3
  • 16
  • 39
  • If you need single value then you definitly have to use User Defined Variables. If you need array then I see the only one way: save values into csv file an in other pre-processor read your csv. Can you explain you intention? – olyv Dec 30 '13 at 17:16
  • Thanks for the answer. I want to save a random number of id's created in a loop controller and use theses id's in another thread group. For each iteration of the loop i want to append an id to the array. – luistm Dec 30 '13 at 17:18
  • You can do it the next way: http://stackoverflow.com/questions/6341600/use-request-value-from-list-of-values-in-jmeter but I would prefer reading CSV. – olyv Dec 30 '13 at 17:27
  • Looks like i have no alternative. I'm going to try the CVS approach. Thanks a lot. – luistm Dec 30 '13 at 17:29

2 Answers2

2

There is no need to do it through writing and reading files. Beanshell extension mechanism is smart enough to handle it without interim 3rd party entities.

Short answer: bsh.shared namespace

Long answer:

assuming following Test Plan Structure:

Thread Group 1 
    Beanshell Sampler 1
Thread Group 2
    Beahshell Sampler 2

Put following Beanshell code to Beanshell Sampler 1

Map map = new HashMap();
map.put("somekey","somevalue");
bsh.shared.my_cool_map = map;

And the following to Beanshell Sampler 2

Map map = bsh.shared.my_cool_map;
log.info(map.get("somekey"));

Run it and look into jmeter.log file. You should see something like

2014/01/04 10:32:09 INFO  - jmeter.util.BeanShellTestElement: somevalue

Voila.

References:

  1. Sharing variables (from JMeter Best Practices)
  2. How to use BeanShell: JMeter's favorite built-in component guide
Dmitri T
  • 119,313
  • 3
  • 56
  • 104
  • Is it possible to iterate trough values using that approach (maybe using thread id as key...) ? In my solution i can easily do that using the CSV Data Set Config. Thnaks – luistm Jan 06 '14 at 10:00
  • In case of Map it has keySet and values collections, you can save them to JMeter variables and feed to i.e. For Each Controller – Dmitri T Jan 06 '14 at 10:08
  • Thanks Dmitri. I will try that next time, to see how it behaves. – luistm Jan 06 '14 at 10:30
  • What you need here is Jmeter's propertries: http://gerardnico.com/wiki/jmeter/property – Shai Alon Feb 27 '17 at 16:18
0

Following some advice, here's how i did it:

The HTTP request has a Regular Expressions Extractor to extract the XPTO variable from the request. Then, a BeanShell PostProcessor saves data to a CSV file:

String xpto_str = vars.get("XPTO");
log.info("Variable is: " + xpto_str);

f = new FileOutputStream("/tmp/xptos.csv", true);
p = new PrintStream(f); 
this.interpreter.setOut(p); 
print(xpto_str + ",");
f.close();

Then, in second thread group, i added a CSV Data Set Config, in which i read the variable from the file. This is really easy, just read the guide (http://jmeter.apache.org/usermanual/component_reference.html#CSV_Data_Set_Config).

Thanks

luistm
  • 909
  • 3
  • 16
  • 39
  • 1
    Why didn't you use property? property variable is accessible across all the thread groups. Writing and reading a file costs a lot of cpu time in terms of load usage. – Shai Alon Feb 27 '17 at 16:13