I think I figured how to do this in two steps:
1. Create a JSR223 sampler of type BeanShell and declare a class with all
the intended util methods. In the below example I have 3 utility methods
sum,difference and multiply.
Instantiate an object of the type of new util class and use vars.putObject
to store the object.
/*-------------JSR223 to create Object with desired utility methods
-----------------------------*/
public class CrazyBeanShell{
public int sum(int a, int b){
log.info("Sum of a and b ="+ (a+b));
return (a+b);
}
public int diff(int a, int b){
log.info("Difference of a and b ="+ (a-b));
return (a-b);
}
public int multiply(int a, int b){
log.info("Product of a and b ="+ (a*b));
return (a*b);
}
}
vars.putObject("crazyBeanAsObj", new CrazyBeanShell())
/*------------------------------------------------------------------------------------------------------*/
2. Now when you want to use the object in the threadGroup use
vars.getObject() at the intended location using another JSR223 sample as
shown in the below example:
/*-----JSR223 to consume the shared Util Object---- */
Object instanceOfCrazyBeanObject = vars.getObject("crazyBeanAsObj");
int sumOfValues = instanceOfCrazyBeanObject.sum(4,5);
int diffOfValues = instanceOfCrazyBeanObject.diff(4,5);
int productOfValues =instanceOfCrazyBeanObject.multiply(4,5);
/*--------------------------------------------------------------------*/
I tested the above approach and it works.
Here is the test output:
2015/04/05 23:50:05 INFO - jmeter.protocol.java.sampler.JSR223Sampler: Sum
of a and b =9
2015/04/05 23:50:05 INFO - jmeter.protocol.java.sampler.JSR223Sampler:
Difference of a and b =-1
2015/04/05 23:50:05 INFO - jmeter.protocol.java.sampler.JSR223Sampler:
Product of a and b =20
Thanks
Chaitanya M Bhatt
http://www.performancecompetence.com
On Sun, Apr 5, 2015 at 10:17 PM, Bob <[email protected]> wrote:
> Thanks Chaitanya!
>
> "Parameterized Controller" won't work as I'm using Beanshell assertion.
>
> If you have a lot of methods in your beanshell I would recommend you to
>> convert it into a java archive(jar) and use it as lib extension.
>>
>
> This option will work but I don't know if it's worse just for 3 methods.
>
> Maybe it's possible to define in jmeter.properties?
>
>
>
>
> On 05/04/15 13:27, chaitanya bhatt wrote:
>
>> Check out the plugin called "Parameterized Controller".
>> Link : http://jmeter-plugins.org/wiki/ParameterizedController/
>>
>> If you use this plugin in conjunction with Module controller you can
>> achieve your goal. Your beanshell should probably scope each method in a
>> switch statement case. The caller of this beanshell should pass the
>> intended method name as one of the parameters. The only pain point with
>> this approach is to exchange several parameters in the form of
>> vars.get/vars.getObject and vars.put/vars.putObject in your beanshell.
>>
>> If you have a lot of methods in your beanshell I would recommend you to
>> convert it into a java archive(jar) and use it as lib extension.
>>
>> Thanks
>> Chaitanya M Bhatt
>> http://www.performancecompetence.com
>>
>>
>> On Sat, Apr 4, 2015 at 8:49 PM, Bob <[email protected]> wrote:
>>
>> Hi,
>>>
>>> Is it possible to include once Beanshell script and re-use it in child
>>> samplers, something like "HTTP Request Defaults"? Now I have to duplicate
>>> this code each time when I want to use it's methods:
>>>
>>> source("${script.dir}/${include.dir}/${include.assertions}");
>>>
>>> What I want is just include source("${script.dir}/${
>>> include.dir}/${include.assertions}"); once and re-use it anywhere I
>>> want.
>>> Thanks!
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [email protected]
>>> For additional commands, e-mail: [email protected]
>>>
>>>
>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>