How to Use JMeter’s Favorite Built-in Component: BeanShell

By | 2021-06-30T15:28:03+03:00 June 30th, 2021|

One of the most sophisticated JMeter built-in components is BeanShell. It understands Java syntax and adds scripting capabilities such as loose types, commands, and method closures. If your test case is unusual, and implementing it using integrated JMeter components is difficult or impossible, BeanShell might be a great way to fulfill your goals.

BeanShell entities in JMeter have access to both internal JMeter APIs and any external classes loaded into the JMeter classpath (be sure to drop required jars into your JMeter installation’s /lib/ext folder and insert the relevant “import” lines at the start of your BeanShell scripts).

The following BeanShell-enabled components are available in all JMeter versions:

  • Beanshell Sampler: A sampler that may be used on its own.
  • Beanshell PreProcessor: A sampler pre-processor that runs before the sampler and may be used to set up prerequisites (i.e. to generate some input).
  • Beanshell PostProcessor: A post-processor that is executed after the sampler and can be used for recovery or clean-up.
  • Beanshell Assertion: A powerful assertion that gives you complete access to the JMeter API. The assertion outcome can be set using Java conditional logic.
  • Beanshell Function: A JMeter Function that allows execution of custom BeanShell code during a sampler run.

Two Basic Examples

  • JMeter Variables Changed on the Fly.

Assume you have a user-defined variable called “continue” in the While loop with the value “true.” With one easy command, you may change it to “false”:

How to Use JMeter's Favorite Built-in Component: BeanShell

  • JMeter Variable to JMeter Property Conversion.

To utilize, you must cast JMeter Property with the same name as the variable “some variable” (such as in another thread group).

How to Use JMeter's Favorite Built-in Component: BeanShell2

Examples of Advanced Levels

  • Cookies are Sent Across Thread Groups.

The following code can transform JMeter cookies into JMeter properties if the HTTP Cookie Manager is enabled.

How to Use JMeter's Favorite Built-in Component: BeanShell3

You can rebuild cookies in another thread group after all cookie information is saved as a JMeter attribute.

How to Use JMeter's Favorite Built-in Component: BeanShell4

  • In The Event of A Failure, The Test Will Be Stopped.

Consider a 48-hour SOAK test that is scheduled to run over the weekend and is dependent on a CSV data set config element that specifies the presence of source CSV files. On your developer machine, you tried it with one user and one loop, and it worked great. When you upload it to your production environment and run it headless, however, JMeter is unable to detect the CSV file, and the entire 48-hour period is lost. Use the following check in the very first BeanShell Sampler to avoid this situation:

How to Use JMeter's Favorite Built-in Component: BeanShell5

  • Loadium Plus All of The Above

The following TestPlan structure is used to show a simple BeanShell use case with Loadium:

  • Thread Group (1 user, 1 second ramp-up, forever)
  • While Controller with empty condition  (forever)
  • Counter (start – 1, increment  – 1, reference name – counter)
  • HTTP request (server name – example.com)
  • BeanShell Post Processor

If the HTTP sampler response contains the example domain, the BeanShell post-processor checks it. If it doesn’t, the test will fail and will come to a halt right away. After the third repetition, if it does, the test will end. Something is written to the jmeter.log in both situations.

How to Use JMeter's Favorite Built-in Component: BeanShell6

Performance testing overview report

Test Report Beanshell on Loaidum

For more blog about BeanShell you can visit here

How the Loadium Load Testing Cloud Complements and Strengthens JMeter

While JMeter is a powerful and appealing load testing tool, we advocate pairing it with Loadium, which allows you to simulate up to millions of people in a single developer-friendly, self-service platform. You can test the performance of any mobile app, website, or API in under 10 minutes using Loadium. Here are some of the reasons why we think the Loadium/JMeter combo will appeal to developers:

  • Simple Scalability: Creating large-scale JMeter tests is simple. Loadium allows you to run far bigger loads than you could with an in-house lab.
  • Loadium’s recorder makes it easy to get started with JMeter right immediately, and Loadium also offers comprehensive lessons and suggestions.
  • Web-based Interactive Reports: Overcome the constraints of JMeter’s standalone UI by simply sharing findings across remote teams.
  • Built-in Intelligence: Loadium offers on-demand geographic load distribution, as well as built-in CDN-aware testing.

Request a demo or enter your URL in the form below to get started with Loadium in minutes.

Enjoy load testing:)