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

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

One of the most sophisticated JMeter built-in components is BeanShell. It understands Java syntax and adds scripting capabilities such as…

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

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. In this post, you will learn this great way, namely how to use BeanShell.

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”:

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

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.

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

  • In case of a “failure”, the test is 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:

Loadium + 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 —
  • 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.

Test Report Beanshell on Loadium

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.

Enjoyed this article? Check out Loadium for more!