BeanShell is one of the most advanced JMeter built-in components. JMeter has rich built-in plugins which cover many needs of a performance test. For example, you might need some additional scripting while writing some complex tests. In such cases, it’s worth using Beanshell. In this post, we are going to be talking about testing complex logic with JMeter Beanshell and common use cases. Beanshell has the functionality to run java code and has access to JMeter APIs and external classes that are loaded in the JMeter classpath.

JMeter has the following Beanshell enabled components:

  1. Beanshell Sampler.
  2. Beanshell PreProcessor.
  3. Beanshell PostProcessor.
  4. __BeanShell function.
  5. Beanshell Assertion.

Below are the JMeter API classes exposed to Beanshell. If you look at the bottom of Beanshell, you’ll see the list of variables defined for the script.

SampleResult

You can access the member variables and methods of the SampleResult Class in Beanshell.

For example, you can set and get the thread name using setThreadName() and getThreadName() methods respectively.

ResponseCode

This class allows you to set response code manually. In some situations, you may need to set the response code based on the response that you get from server.
Here is a sample use-case:

if (condition) {
  ResponseCode = "200";
}
else {
  ResponseCode = "500";
}

ResponseMessage

This class allows you to set the response message manually. The sample use case is the same as the one for ResponseCode.

IsSuccess:

IsSuccess is a Boolean that reflects whether the sampler succeeded. If it’s set to true, the sampler is considered to have “passed.” Otherwise, it will be marked as “failed”.

if (condition) {
  IsSuccess = true;
}
else {
  IsSuccess = false;
}

ctx

ctx is the most powerful variable exposed to BeanShell. It represents the JMeterContext class, which is virtually JMeter itself. It provides read/write access to the underlying JMeter engine, samplers, and their results as well as variables/properties.

The following code demonstrates the usage of ctx variable:

log.info("Current Sampler class is: " + ctx.getCurrentSampler());
log.info("JMeter Engine class is: " + ctx.getEngine());

log.info("Previous Response Message is: " + ctx.getPreviousResult().getResponseMessage());
log.info("Previous Response Code is: " + ctx.getPreviousResult().getResponseCode());
log.info("Previous Response URL is: " + ctx.getPreviousResult().getURL());
log.info("Previous Response Time is: " + ctx.getPreviousResult().getTime());

log.info("Previous Domain is: " + ctx.getPreviousSampler().getDomain());
log.info("Previous Protocol is: " + ctx.getPreviousSampler().getProtocol());
log.info("Previous Port is: " + ctx.getPreviousSampler().getPort());
log.info("Previous Method is: " + ctx.getPreviousSampler().getMethod());

log.info("Thread Name is: " + ctx.getThread().getThreadName());
log.info("Thread Start Time is: " + ctx.getThread().getStartTime());
log.info("Thread End Time is: " + ctx.getThread().getEndTime());
log.info("Start Next Thread Loop on Error: " + ctx.getThreadGroup().getOnErrorStartNextLoop());
log.info("Stop Test on Error: " + ctx.getThreadGroup().getOnErrorStopTest());

Another use case is if you want to write all Assertions error to an HTML file so you can see which assertion is not working. You can use ctx variable in Beanshell assertion to iterate through all samplers in the current context and write the failures in an html file.

Add a Beanshell assertion and copy the following code to write all the assertions to a html file:

import org.apache.jmeter.services.FileServer;
f = new FileOutputStream("C:\\apache-jmeter-4.0\\bin\\result.html", true);
pt = new PrintStream(f);
pt.println("<html><body>");
for (a: SampleResult.getAssertionResults()) {
  if (a.isError() || a.isFailure()) {
    log.error("URL :"+ ctx.getCurrentSampler().toString());
    log.error(Thread.currentThread().getName()+": "+SampleLabel+": Assertion failed for response: " + new String((byte[]) ResponseData));
    pt.println("URL :"+ ctx.getCurrentSampler().toString());
    pt.println(Thread.currentThread().getName()+": "+SampleLabel+": Assertion failed for response: " + new String((byte[]) ResponseData)); // update here what you want to write
  }
  pt.println("</body></html>");
  pt.close();
  f.close();
}

vars

It’s an instance of the JMeterVariables class and provides read/write access to current variables, is capable of enumerating/changing existing variables, creating new variables, and obtaining nested properties. All JMeter variables are Java strings. If you need to put something else to a JMeter variable, you’ll need to cast it to a string first. The following code snippet demonstrates how to save previous sampler response data into a JMeter variable:

vars.put("ResponceData", prev.getResponseDataAsString());
log.info(vars.get("ResponceData"));

props

Basically, this is the same as vars, but it exposes JMeter properties instead. See JavaDoc on java.util.Properties and JMeter documentation on JMeter properties for more information. The primary distinction between props and vars is that props have a “global” scope, whereas the scope of vars is limited to the current thread group.

log

log represents the Logger class and can be used to append a message into jmeter.log file. The following is the sample use case:

log.info("Test Message with INFO level");
log.error("Test Message with ERROR level");

Some Common Use Cases of Beanshell

Writing custom requests in JMeter.

In some cases, you might want to add parameters based on the response. You can do this using the Beanshell preprocessor.

The sampler.addArgument() method allows you to add arguments before hitting the server manually.

Changing JMeter Variables.

As mentioned earlier, you can update JMeter variables on the fly using Beanshell. Assume that you have a user defined variable called “counter” with the value of “1”. Let’s say you want to increment this counter value by one every time it executes a sampler in while loop. The following code snippet increments the counter value by one and updates the same:

int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));

Using a Custom Jar in the Beanshell Sampler.

Assume that you have a requirement to call one of the methods which is present in a user created class. You can call the methods present in user created JAR file using Beanshell.

  1. Place your User created Jar file in lib/ext folder of JMeter.
  2. Restart JMeter to pick the .jar.
  3. Add a Beanshell sampler to your Test Plan and call the method from .jar file as you would do in Java.

For Example :

Given you have the following class:

package timeStampPack;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TimeStampConversion
{
  public TimeStampConversion() {}
  public static String getTimeStamps(int count)
  {
    String dates = "";
    for (int i = 1; i <= count; i++)
    {
      try {
        Thread.sleep(1L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      dates = dates + "TimeStamp" + i + "==>" + getCurrentDateTime() + "\n";
    }
    return dates;
  }
}

In above class, getTimeStamps() method returns timestamp, packages it as timeStamp.jar and copies the file to the lib/ext folder of your JMeter installation.

Add Beanshell Sampler to your Test Plan and put the following code into the “Script” area.

Import timeStampPack.*;
var time=TimeStampConversion.getTimeStamps(1);
log.info(time);

The above code will call getTimeStamps() method and logs the value.

You can also use Beanshell to parameterize test data using CSV file. Check this article to see details.

How RedLine13 Enhances JMeter Load Testing

JMeter is an excellent open source load testing tool used by thousands of developers. If you’re one of them, you may want to load test. JMeter can be used for load testing. With RedLine13, you can run a JMeter Load Test with your JMX script of any mobile application, web application, or API in 10 minutes.

Comments are closed.