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:
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”:
To utilize, you must cast JMeter Property with the same name as the variable “some variable” (such as in another thread group).
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.
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:
The following TestPlan structure is used to show a simple BeanShell use case with Loadium:
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 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:
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!