Beanshell in JMeter

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).

  • BeanShell helps in quick development and test of Java application.
  • One can use it for quick or rapid prototyping or quickly testing a small functionality or a process.
  • BeanShell can also be used as a configuration language as it supports the creation of Java-based variables like strings, arrays, maps, collections, and objects.
  • It also supports what is called scripting variables or loosely typed variables.
  • BeanShell scripts can also be written in a standalone mode in an external file which then can be loaded and executed by the Java program.
  • BeanShell also provides the concept of UNIX like shell programming.
  • You can give BeanShell commands interactively in a GUI shell and see the output instantly.
  • Different handlers can be used like vars, log, ctx, prev, props, and many more.
  • Scripting can be used in both BeanShell sampler as well as BeanShell preprocessor.
  • The Beanshell sampler has pre-defined variables that you can use in the script. By default, JMeter supports Beanshell so you can write and run Beanshell scripts in the Beanshell sampler.
BeanShell Components:

JMeter provides the following components that can be used to write BeanShell scripts :

  • BeanShell Sampler
  • BeanShell PreProcessor
  • BeanShell PostProcessor
  • BeanShell Assertion
  • BeanShell Listener
  • BeanShell Timer

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 :

  • BeanShell PreProcessor : This component will define or initialize our array.
  • BeanShell Sampler : This component will sort the array and convert it into a string.
  • BeanShell PostProcessor: This component will strip the unnecessary characters from the string.
  • BeanShell Assertion : This component will assert our test result (a string with sorted content).
  • Open the JMeter and then add Thread Group to the Test Plan.
  • Next, Add Beanshell Sampler to the Thread Group. (right-click on the Thread Group-->Add-->Sampler-->Beanshell Sampler)
  • In the BeanShell Sampler page, you will see the script console.
  • Inside the script console, enter the script which you want to print, I am going to print as below.
    log.info("Hello");‚Äč
  • Now save the Test Plan.
  • Click on the Options tab on the top of the JMeter window and then select the Log Viewer.
    log-viewer-under-options-jmeter
  • Whatever we entered inside the log.info will be printed in the console.
  • Run the Test Plan, after the execution you will see the Hello has been printed in the console.
    hello-printed-in-console-jmeter
  • In case if you don't want to print any line means you can comment that line as below.
    //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:
output-of-var-in-beanshell-scripting-jmeter

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.
putting-value-of-into-var-jmeter

PreProcessor and PostProcessor in JMeter

A processor is used to modify the Samplers in its scope.

There are two types of processors :

  • Pre-processor : Pre-processor executes some action before making a Sampler Request.
  • Post-processor: Post-processor executes some action after making a Sampler Request.
The following Example demonstrates the Pre-Processor and Post-Processor.
  • Open the JMeter and then add Thread Group to the Test Plan.
  • Next, add HTTP sampler request to the Thread Group and enter the www.google.com in the Server IP address.
    http-request-with-goole-address-jmeter
  • Next, add View Result Tree sampler to the thread group.
  • Now save and run the Test Plan, after the execution, the request has been executed.
    execution-of-http-request-jmeter
  • Now, what I wanted to do is, I want to perform some other actions before executing the current request. So for that, I need to add User Parameters to the Thread Group.
    add-user-parameter-pre-processor-jmeter
  • Now entering one parameter and specify the name of that parameter as below(To enter the parameter, click on Add Variable)
    creating-parameter-jmeter
  • The Calendar Parameter will execute before the HTTP Request, so I am using this parameter in the HTTP request.
  • Mention the /${param} in the HTTP request as below.
    enter-param-in-http-request-in-jmeter
  • Save and run the Test Plan, After the execution, open any HTTP request in the View Result Tree, you can see that the HTTP request uses the variable which we mentioned in the User Parameters.
    view-result-tree-in-http-request-jmeter
  • Next, we need to have a Post Processor.
  • Here I am adding Result status Action Handler post-processor to the thread group.
    reault-status-action-handler-jmeter
  • Whatever the actions which we select in the Result Status Action Handler, Post Processor will be executed after the execution of the HTTP request.
    result-status-action-action-handler-jmeter
  • If any user Parameter goes wrong, then the post-processor will stop execution if we select the stop test now.
  • Select the Number of Users as 5, ramp-up period as 1, and loop count as 5.
  • And then enter the wrong parameter in the User Parameters as /calendar123
    enter-wrong-parameter-jmeter
  • Now save and run the Test Plan.
  • We have given the wrong parameter, so the First request itself will fail so the execution will be stopped.
    execution-og-request-has-been-stopped-jmeter
  • In this way, you can select any actions in the post-processor and execute the pre-processor and post-processor.
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions