BeanShell is a scripting language written in Java. It is part of the JSR-274 specification. It is an extension of the mainstream Java language by providing scripting capabilities.
It is an embedded interpreter that recognizes strongly typed Java syntax and scripting features like shell commands, loose types, and method closures (functions as objects).
JMeter provides the following components that can be used to write BeanShell scripts :
Each of these components allows you to write scripts to conduct your test. JMeter will execute the scripts based on the lifecycle order of the components.
Example: It will first invoke PreProcessor then Sampler and then PostProcessor, and so on.
Data can be passed between these components using thread-local variables that have certain meanings and contexts. Every component provides you with pre-defined variables that can be used in the corresponding script.
The following table shows some of the common variables used by the BeanShell components :
| Variable Name | Description |
| ctx | It holds context information about the current thread that includes sampler and its results |
| vars | This is a thread-local set of variables stored in a map used by BeanShell components in the same thread |
| props | These are variables loaded as properties from an external file (jmeter.properties) stored in the classpath. |
| prev | It holds the last result from the sampler |
| data | It holds server response data |
Variables can be defined and assigned values via BeanShell script. Dynamic data like userIDs can also be generated via scripting if CSVDataSetConfig need not be used. There are many basic functions used in the scripting e.g. getThreadNum() which can return the current threadnumber.
You can print anything in the Beanshell Script and which will be reflected in the Script console. the Syntax to print in the Beanshell script is as below.
log.info("Hi CherCher.Tech");
We will make use of the following BeanShell components to implement our test case :
log.info("Hello");
//log.info("Hello");
The following example demonstrates how to define a variable and printing some of the two variables.
Open the Beanshell Sampler and then define the variable as follow.
//Defining variables
var a=12;
var b=13;
//Adding Variables
var sum=a+b;
//Printing the Sum of the two varibales
log.info("Result is"+sum);
When we will run the test plan, we will get the output as below:
We can put the value of the Sum into any defined variables like a,b, let us see how to do
vars.put("a","sum");
log.info(vars.get("a"));
When you run the test plan the will be as below.
A processor is used to modify the Samplers in its scope.
There are two types of processors :
/${param} in the HTTP request as below.5, ramp-up period as 1, and loop count as 5./calendar123