Enes Pekkaya
Enes Pekkaya

Reputation: 304

How to set variables to custom function in Jmeter?

I have a basic function in BeanShellSampler.bshrc at Jmeter 4.0

String getMyString(String strParam) {
return "MyString: "+strParam; 
}

I called in BeanShell Sampler as below

String N = "123123";
log.info("${__BeanShell(getMyString("${__V(Var${N})}"),)}");

When I run Sampler output is somthing like that.

2018-06-18 15:25:40,080 INFO o.a.j.u.BeanShellTestElement: MyString: Var${N}

How can I set string variable to my function?

I read function articles in Jmeter web site

Thank you.

Upvotes: 0

Views: 1879

Answers (2)

Rohit
Rohit

Reputation: 611

For each BeanShell Program type there are different beanshell.*.init properties defined in bin/user.properties

beanshell.function.init=BeanShellFunction.bshrc

beanshell.preprocessor.init=BeanShellSampler.bshrc

beanshell.postprocessor.init=BeanShellSampler.bshrc 

beanshell.assertion.init=BeanShellFunction.bshrc

Hence the same function which needs to be called from any program(preprocessor, postprocessor, etc) we need to copy the function to every .bshrc file OR use same .bshrc file for every program init property.

In your case if you are using local string variable N and passing it along with the script. If you use ${Variable} there must be a JMeter variable defined so that JMeter can pick its value. To do that you can use vars.put , write N value to JMeter variables and use ${N} .

I have defined Var123123 value as FinalValue as shown below enter image description here

And 2 beanshell samplers one is to put String variable N to Jmeter variable and one is beanshell script as shown below enter image description here enter image description here

You can see in the log its printed VAR123123's value which is FinalValue

The reason why i took 2 beashell samplers is if i write N to JMeter variables and use it in same script its not updating N value until the sampler executed..

References :

Configuring JMeter

JMeter Beanshell

Please let me know if it helps

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168157

  1. Add the next line to user.properties file:

    beanshell.sampler.init=BeanShellSampler.bshrc
    
  2. Amend your code to look like:

    String N = "123123";
    log.info(getMyString(N));
    
  3. That's it, you should get MyString: 123123 in jmeter.log file

    JMeter Beanshell Function


Be aware that starting from Jmeter 3.1 it is recommended to use Groovy for all forms of scripting as Groovy performance is much better comparing to Beanshell so consider taking the following steps instead:

  1. Create a file, i.e. foo.groovy in "bin" folder of your JMeter installation and put your function there:

    String getMyString(String strParam) {
        return "MyString: " + strParam;
    }
    
  2. Add the next line to user.properties file:

  3. You should be able to refer your custom code from __groovy() function like:

    ${__groovy(log.info(getMyString("123123")),)}
    

    functions can be used anywhere in the Test Plan

    JMeter Groovy Function

Upvotes: 1

Related Questions