0
votes

How to create the utility bean shell script in jmeter. In Jmeter i have 5 thread group which is contains the bean shell samplers.i want to make use beanshell class across my thread group.The given below is sample beanshell class which is contains one method called add().

 SampleBeanShell(){

     int add(int a,int b){
         return a+b;
       }
      return this;
    }

    obj=SampleBeanShell();
    int result=obj.add(10,20);
    log.info("REsult is"+result);

I would like to use the add() method in another in thread group bean sampler or bean shell assertion.

if (bsh.shared.myObj == void){
        myObj=SampleBeanShell(); 
    }
    int result1=myObj.add(12,20);
 log.info("REsult is"+result1);

I have added the above snippet,I am not able call this method in another bean sampler and another thread group as well.

int result=bsh.shared.myObj.add(20,21);
log.info("2.Bean sampler result"+result);
2

2 Answers

1
votes

Startup File:

You can define this in a startup file. There is a property beanshell.init.filefor this purpose. Please the definition in a .bsh file and use this property to read the file while starting the test.

Bsh Shared Namespace:

You can also take a look at bsh.shared namespace to share object across thread groups.

I am adding a setup thread group to define the SampleBeanShell at first. Later, In all other thread Groups, I just use bsh.shared.myUtil to access the SampleBeanShell object.

Usage: bsh.shared.myUtil.add(10,20)

enter image description here

enter image description here

Check here for more information - #Sharing Variables.

0
votes

According to JMeter Best Practices it's better to avoid scripting and use JMeter build-in test elements and functions where possible.

You can add 2 (or more) values and store result into a JMeter variable by using __intSum() or __longSum() function.

See How to Use JMeter Functions post series for more information on above and others JMeter functions.

For implementing your scenario in Beanshell you need to make the method available in the other Thread Groups like:

  • In 1st Thread Group:

    bsh.shared.myObj=SampleBeanshell();
    
  • In 2nd Thread Group:

    bsh.shared.myObj.add(50,60);
    

Your code doesn't work as you don't store anything into myObj in 1st Thread Group