New Webinar: Modernising Without Destabilising: How Bread Financial Is Building Confidence Through Change

Learn more

New webinar with Bread Financial

Learn more
Contact us

Blogs

Using the power of Java programming in your JMeter scripts (BeanShell)

<span id="hs_cos_wrapper_name" class="hs_cos_wrapper hs_cos_wrapper_meta_field hs_cos_wrapper_type_text" style="" data-hs-cos-general-type="meta_field" data-hs-cos-type="text" >Using the power of Java programming in your JMeter scripts (BeanShell)</span>

Date 29 June 2026

Author Team Capacitas

When creating a script in JMeter, you may have instances where you need to perform manipulation on variables or arrays you’ve captured using a Regular Expression Extractor or XPath Extractor. This is where BeanShell can come in handy.

What is BeanShell?

BeanShell is a scripting language which allows you to tap into the power of the Java programming language in order to perform manipulation on your Jmeter variables.

The general steps to achieving this are:

  • create a BeanShell Listener after your RegularExpression/XPath extractor object
  • get your variable into the BeanShell Listener by using ‘vars.get(“NameofVariable”)
  • perform processing
  • put your processed variable(s) back into the specified Jmeter variable

Let’s take a look at an example. Assume you have captured values in a JMeter array containing “10,6,5,8” and you wanted to manually add another element to get “10,6,5,8,12”, your BeanShell Listener would contain the following:


// first we get the number of elements our Jmeter array contains
int count = Integer.parseInt(vars.get("JMeterArr_matchNr"));
String comma = "%2C"; // %2C is the ASCII code for a comma
// create a new string buffer object which will
// store our generated data
StringBuffer sb = new StringBuffer();
// loop through the Jmeter array
for(int i=1;i<=count;i++)
{

// append each Jmeter array element to the string buffer
sb.append(vars.get("dayValueX_" + i));
sb.append(comma); // append a comma after each element

}
// by this time the loop will have finished and now we can
// simply add our own element to our generated string
sb.append("12");
// put our string buffer contents back into a JMeter variable
// called ‘outputString’
vars.put("outputString",sb.toString());
// ${outputString} contains ‘10%2C6%2C5%2C8%2C12’

Of course this is a very simple example but demonstrates manipulating Jmeter variables using BeanShell. This could be extended to manipulate more than one variable, substituting the value of a particular variable for another etc. As well as manipulating your JMeter variables, you could also use a BeanShell Sampler to test for conditions such as whether your JMeter variable is holding an expected value. If not, you could flag up an error message which in turn terminates your script.

Conclusion

Overall, BeanShell can be useful when performing tasks which JMeter might not directly support. It is a lightweight Java scripting language and is definitely worth spending time looking into if you use Jmeter on a regular basis.

To learn more about the benefits of performance testing and the secrets of its delivery – download our ebook here.

Download the Performance Testing Primer

Team Capacitas
About the author

Team Capacitas

Capacitas is a cloud and AI value partner. We translate rapid technological change into enduring commercial advantage by converting every unit of compute into enterprise value.

FinOps and AI: Building the Financial Discipline for the Next Wave of Enterprise Intelligence

AI FinOps represents an evolution rather than a replacement of traditional FinOps. It extends the model into a domain where financial, technical, and product decisions are tightly interconnected.

Read insight

Confidence Under Load: How We Verified AKS Readiness for Peak

How Capacitas verified AKS readiness for peak demand by validating workload performance, autoscaling, cluster capacity, monitoring, and incident response.

Read insight

Building Cloud Resilience: Lessons from the AWS Outage

Learning from the Latest Outage. Events like this week’s AWS disruption highlight one clear truth: resilience must be designed, not assumed.

Read insight

Bringing Order to Chaos: A Practical Guide to Chaos Testing in the Cloud

In today’s cloud-native environments, resilience is not optional—it’s critical. Chaos testing has emerged as a key practice for validating system behaviour under failure conditions.

Read insight