Search code examples
javajmeterbeanshell

Bean shell utility in Jmeter


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);

Solution

  • 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.