1

I have a jMeter project I'm working on which has thrown up all sorts of problems. Mainly due to storing and scoping of variable data throughout the run.

I now have things pretty much working and I'm using a Beanshell shared hashmap to store data through the run. I don't need to worry about thread safety due to the way I'm doing it.

It works, but it re-initialises itself each time a thread group runs. Despite putting the initialisation step outside all thread groups.

So, as I understand it, the solution is to put all the startup data into an initialisation file which is only run once on startup. But I can't figure out how I'm supposed to do that? I've copied the code from the Beanshell Preprocessor I was using previously into a ".bshrc" file and updated the jMeter properties file with the location of my ".bshrc" file, but it doesn't seem to work. It doesn't actually seem to have done anything. When I run the test, no values are present and everything fails.

I've tried using:

beanshell.init.file=../bin/data.bshrc and beanshell.preprocessor.init=../bin/data.bshrc

I've tried to find some sort of idiots guide to setting up an init file, but I can't find anything helpful. This is the first time I've had to make much serious use of Beanshell and my Java knowledge is VERY limited at best!

At the moment, I'm getting round it by running my test once with the original Beanshell pre-processor enabled. This sets up the hashmaps and they stay resident in memory from there on. I stop this run, disable the pre-processor, and all subsequent runs work just fine.

Anyone?

ColinMcC
  • 258
  • 5
  • 13

1 Answers1

5

I would suggest using setUp Thread Group which is being executed prior to any other Thread Groups and define your test data there with a Beanshell Sampler like

bsh.shared.myMap = new java.util.HashMap();
bsh.shared.myMap.put("foo","bar");
// any other operations

After that in your main Thread Group(s) you can access myMap values in any Beanshell-enabled test element (Sampler, Pre/Post Processor, Assertion) as

log.info("foo = " + bsh.shared.myMap.get("foo"));

2014/07/22 10:06:48 INFO - jmeter.util.BeanShellTestElement: foo = bar

See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting in Apache JMeter and a kind of Beanshell cookbook.

If you use Beanshell for "heavy" operations I would recommend considering switching to JSR223 Sampler and Groovy language as in that case you'll get performance comparable to native Java code.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Yep. That's exactly what I changed it to shortly after I posted this yesterday. It works well enough. But an external file would be easier to work with so I'd still like to get the initialisation file working. I know it can be done, I'm just not sure how to set it up. I'm only really having to use the Beanshell stuff so I have access to shared arrays over multiple thread groups. Doesn't need to be thread safe as the way the tests are set up, one thread group will never use an instance in use by another. But it makes it much more scalable if I have a centralised data pool. Hence the arrays. – ColinMcC Jul 22 '14 at 07:31