In this example, we will demonstrate the use of BeanShell components in Apache JMeter. We will go about writing a simple test case using BeanShell scripting language. These scripts will be part of BeanShell components that we will configure for this example. Before we look at the usage of different BeanShell components, let’s look at the concept.
Apache JMeter is an open source Java based tool that enables you to perform functional, load, performance and regression tests on an application. The application may be running on a Web server or it could be a standalone in nature. It supports testing on both client-server and web model containing static and dynamic resources. It supports wide variety of protocols for conducting tests that includes, HTTP, HTTPS, JDBC, FTP, JMS, LDAP, SOAP etc.
A quick look at some of the features:
- It provides a comprehensive GUI based workbench to play around with tests. It also allows you to work in a non-GUI mode. JMeter can also be ported on the server allowing to perform tests in a distributed environment.
- It provides a concept of template which are pre-defined test plans for various schemes or protocols that can be directly used to create your required test plan.
- It enables you to build test plan structurally using powerful features like Thread Group, Controllers, Samplers, Listeners etc.
- It provides debugging and error monitoring through effective logging.
- It supports parameterized testing through the concept of variables.
- It supports creation of different flavors of test plan that includes Web, Database, FTP, LDAP, Web service, JMS, Monitors etc.
- It allows for remote testing by having different JMeter instances running as servers across nodes and accessed from a single client application.
- It gives you real time test results that covers metrics like latency, throughput, response times, active threads etc.
- It enables you to perform testing based on regular expressions and many more other features.
1.1. What is BeanShell?
BeanShell is a scripting language written in Java. It is part of JSR-274 specification. It in some way is an extension to the mainstream Java language by providing scripting capabilities. It is an embedded interpretor that recognizes strongly typed Java syntax and scripting features like shell commands, loose types and method closures (functions as objects). BeanShell aids 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. The script can also be embedded in the Java code and invoked using the Interpreter API.
BeanShell can also be used as a configuration language as it supports creation of Java based variables like strings, arrays, maps, collections and objects. It also supports what is called as scripting variables or loosely typed variables. BeanShell scripts can also be written in a standalone mode in a 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.
For more details on BeanShell, you can refer to the official website http://www.beanshell.org
1.2. JMeter 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 component allows you to write scripts to conduct your test. JMeter will execute the scripts based on the lifecycle order of the components. For 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 which has certain meaning and context. 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:
|It holds context information about the current thread that includes sampler and its results.
|This is a thread local set of variables stored in a map used by BeanShell components in the same thread.
|These are variables loaded as properties from an external file (jmeter.properties) stored in the classpath.
|It holds the last result from the sampler
|It holds server response data
2. BeanShell By Example
We will now demonstrate the use of BeanShell in JMeter. We will take a simple test case of sorting an array. We will define an array of 5 alphabets (a,b,c,d,e) stored in random order. We will sort the content of the array and convert it into string. After conversion, we will remove the unwanted characters and print the final string value. It should give the output as ‘abcde’.
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 string.
- BeanShell PostProcessor – This component will strip the unnecessary characters from the string.
- BeanShell Assertion – This component will assert our test result (string with sorted content).
Before installing JMeter, make sure you have JDK 1.6 or higher installed. Download the latest release of JMeter using the link here. At the time of writing this article, the current release of JMeter is 2.13. To install, simply unzip the archive into your home directory where you want JMeter to be installed. Set the JAVA_HOME environment variable to point to JDK root folder. After unzipping the archive, navigate to <JMeter_Home>/bin folder and run the command
jmeter. For Windows, you can run using the command window. This will open JMeter GUI window that will allow you to build the test plan.
2.1. Configuring BeanShell Sampler
In this component, we will sort the array. But before we sort the array, it needs to be initialized. You will see the initialization routine in the next section when we create the pre-processor component. Let’s first create the BeanShell Sampler component. We will write the code to sort the array after the initialization routine. Right click on Single User ThreadGroup and select Add -> Sampler -> BeanShell Sampler.
We will provide the name of our sampler as ‘Array Sorter’. The Reset Interpreter field value is retained as ‘False’. This field is only necessary when you have multiple BeanShell samplers configured or if you are running a sampler in the loop. The value of true will reset and create a fresh instance of BeanShell interpreter for each sampler. The value of false will create only one BeanShell interpreter that will interpret scripts for all the configured samplers. From the performance perspective, it is recommended to set this field to true if you have long running scripts with multiple samplers. The Parameter field allows you to pass parameters to your BeanShell scripts. It is usually used with external BeanShell script file, but if you are writing script in this component itself then you can use
bsh.args variable to fetch the parameters. The
Parameters variable will hold the parameters as a string value (retains spaces). The
bsh.args variable will hold the parameters as string array. For this example, we are not passing any parameters to the script. The Script file field is used when you have a BeanShell script defined in an external file. It is important to note, this will override any scripts written inline in this component. We will retain the default value for all the above mentioned fields for all the BeanShell components. The final Script textbox field allows us to write scripts inline in this component itself. It allows you to use certain variables in your scripts. As you can see there is no scripting code currently in this field. We will write the code after our array is initialized in the pre-processor component.
2.2. Configuring BeanShell PreProcessor
Beanshell PreProcessor will be the first component to be executed before your sampler. It becomes a good candidate to perform initialization routines. We will initialize our array, to be sorted, in this component. Right click on Array Sorter sampler and select Add -> Pre Processors -> BeanShell PreProcessor.
We will name the component as ‘Array Initializer’. Let’s see the code in the Script textbox field. First, we are declaring and initializing the array named
strArray. It is a loosely typed variable. The values of the array are not in order. Then we make use of the
vars variable to store the array by calling
putObject() method. The
vars variable will be available to all the BeanShell components that are part of this thread. We will fetch the value of
vars variable in a ‘Array Sorter’ sampler and perform the sort. In the above section, we created the ‘Array Sorter’ sampler, now we will write the following code in that sampler to sort the array. Click on Array Sorter sampler, in the Script textbox field to write the following code:
First, we get the array using
getObject() method of the
vars variable. Then we will sort using the
Arrays class of Java. The
sort() method of the said class will take our array as a parameter and perform the sort. We then convert the array into string by calling
Arrays is a utility class provided by the JDK to perform certain useful operations on array object. We will then put this sorted string as a response data through the use of
SampleResult variable. Our sorted string will look like the following:
[a, b, c, d, e].
2.3. Configuring BeanShell PostProcessor
The BeanShell PostProcessor will strip the unnecessary characters like ‘,’. This component will act more like a filter. Right click on Array Sorter sampler and select Add -> Post Processors -> BeanShell PostProcessor.
We will name the component as ‘Array Filter’. The Script textbox field contains the code that strips the unnecessary characters from our string. If you recall, the string was stored as response data by the Array Sorter sampler. Now here we fetch the string using the function
getResponseDataAsString() of the
prev variable. Next, we use the
replace() method of the
String class to strip ‘’ and ‘,’ characters from the string. We store that string in the
vars variable. This string will now be used by BeanShell Assertion component to assert the final result.
2.4. Configuring BeanShell Assertion
Using this component, we will assert the final result value as ‘abcde’. Right click on Array Sorter sampler and select Add -> Assertions -> BeanShell Assertion.
vars variable, we will get the final string and store it in the
finalString variable. Then we assert by checking if the final string does not contain the value ‘abcde’ then set the
Failure variable to true and provide the failure message using the
FailureMessage variable. The output of the test execution can be see in the command window from where you started the JMeter GUI. The below is the console output after running our tests.
BeanShell scripting language provides scripting capabilities to the Java language. In JMeter, you can use different BeanShell components to write the test scripts and execute the same. Each component is equipped with useful variables that can be used in the scripts to perform the control flow. The scripting feature adds a powerful and useful dimension to the JMeter testing tool. The objective of the article was to show the usage of common Beanshell components and how one can write test scripts to execute test.