How to run JavaScript in Jmeter?

In this article we are going to see how to run JavaScript in Jmeter.

In Jmeter , we can execute JavaScript in 3 ways.

1. Using Run time Function:
This is one of the most easy way of using java Script. Syntax :
${__javaScript(<Your desire script>),<JmeterVariableName>}

JmeterVariableNames is Optional

How it works : JMeter support javaScript over Java using Rhino. So, it has limitations to support only Rhino supported functions. So, if you function involving page rendering or system specific logic, it might not work. For detail declarative use full java Script runner samplers. 

When to use?
->When we need to run small, simple javaScript function

Where to use ? 
->Use this where ever you are adding parameters or values
->If you use a jmeter variable name, you can call this by that variable name.

Example : If we use this function helper ,

we can get this results by ${DateNow}

For help inside Jmeter , use Function Helper  (Menu ->Option -> Function Helper)

2. Using BSF Samplers : 

In Jmeter there is BSF (Bean Scripting Framework) sampler.
If we add from Samplers -> BSF Sampler, we can see this scripting interface supporting a list of language to run on top of jmeter. JavaScript is one of them.

Select JavaScript as you language and use this. That's all

The editor is in below. Scripting part is in below with JSR223 as both are similar.

How it works : Apache Bean Scripting Framework runs on top of Java applications. As Jmeter is a java application , it runs on top of it. From Java 6, it shipped free with JSR223 so, both BSF and JSR223 sampler actually works same way on top of JSR223 script specified language. Think like as interpreter running over JVM.

3. Using JSR223 Sampler :
We can use JSR223 sampler to write JavaScript. This is JSR223 specified script interface. So, it supports multiple language and JavaScript is one of them. From samplers ->  JSR223 sampler


And select JavaScript as you language and that's all to use this.

How it works : It works same like as BSF from Java 6. For scripting, JSR sampler has spatial property in If you are adding long script, you may increase buffer size(in KB).

This size applies in both BSF and JSR sampler. Default 100kb.

Scripting Basics for both BSF/JSR224 Samplers:
1. We can get parameters from parameter field. This is like as argument passing inside script.

2. We can use following Jmeter function access like as other script languages.
FileName, Label, log - the Logger, Parameters , args , SampleResult ,sampler , ctx, vars, props, OUT 

In the example I have use put.

3. We can access to all jmeter variables directly  by ${variableName} inside script.

Example : I use this a JSR223 sampler, BSF sampler, and Dummy sampler to show the results. The JMX can be found in here.Use Jmeter with Plugins.

In JavaScript function example I use this : (This will only add two value)

In BSF & JSR223 I use this JavaScript : (This will show current date in full format)
var currentdate = new Date();
var datetime = "Current Date and Time: " + currentdate.getDate() + "/"
                + (currentdate.getMonth()+1)  + "/"
                + currentdate.getFullYear() + " @ " 
                + currentdate.getHours() + ":" 
                + currentdate.getMinutes() + ":"
                + currentdate.getSeconds();

[I changed this JSR_output to BSF_output to separate jmeter variable names ]