0

I have a Javascript function in JSR223 Sampler and I would like to call this function in my HTTP Request sampler. Any idea how to call the function?

Example:

I know I can do something like this and use the "total" variable from my HTTP Request Sampler.

var total;
function sum(a, b){
    return a+b;
};
vars.put("total", sum(2,4));

But is there a way to call the function sum(a,b) directly from HTTP Request sampler? If not, is there any other way to do this? A sample example will really help me a lot.

1 Answer 1

1

First of all, are you aware of __intSum() function which does exactly what you're trying to achieve, the syntax would be:

${__intSum(2,4,total)}

If you still want to proceed with coding:

  1. Don't use JavaScript as it might become a performance bottleneck. Since JMeter 3.1 it is strongly recommended to use Groovy for scripting.
  2. Create a file, i.e. sum.groovy in "bin" folder of your JMeter installation and put the following code into "Script" area:

    int sum (int a, int b) {
        return a + b;
    }
    
  3. Run JMeter as follows:

    jmeter -Jgroovy.utilities=sum.groovy
    

    If you want to make the change permanent - add the relevant line to user.properties file

    groovy.utilities=sum.groovy
    

    See Apache JMeter Properties Customization Guide for more detailed information on setting and overriding JMeter properties

  4. You will be able to sum numbers with __groovy function like:

    ${__groovy(sum(2\, 4),total)}
    
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I am aware of __intSum() function. I just gave an example. Anyway, thank you for your answer. I didn't know that we are not supposed to use JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.