DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Trending

  • Unlocking the Benefits of a Private API in AWS API Gateway
  • Docker Base Images Demystified: A Practical Guide
  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • The Modern Data Stack Is Overrated — Here’s What Works

BeanShell Processor in JMeter

Check out what the BeanShell Processor can do that will make it your favorite component to use when performance testing with JMeter.

By 
Canberk Akduygu user avatar
Canberk Akduygu
·
May. 01, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
73.6K Views

Join the DZone community and get the full member experience.

Join For Free

Even though general functionalities of JMeter cover many needs of a performance test, you might need additional scripting. Apache JMeter has the functionality to run Java code snippets during your test execution. BeanShell has access to internal JMeter features and any library located in your JMeter lib folder.

BeanShell can be executed as a pre/postcondition or as a sampler. The only difference is that pre/post conditions will not be listed in JMeter Listeners.

When using BeanShell, you can use “vars” variable to get and set values generated in the test context. This post will cover those functionalities.

BeanShell vars.get() Usage

Create a sampler to request a product from the Amazon website.

Then add a Post CSS/JQuery Extractor and extract the buying price. Whenever this request is executed, JMeter will create a variable on memory with the “Reference Name.” This variable is only available during that test. When the execution is finished, it will be erased from memory.

jquery_extractor

Then add a BeanShell Post Processor to your sampler as a child element. Add the code below.

String price = vars.get("price");
log.info("Buying Price Value is "+price);   
if(price == 0)   
log.info("Error in prices");   
else  
log.info("Price is in valid range");

vars.get(“price”) will get the value extracted by CSS/JQuery Extractor and assign it to a String variable.

Then we do some stuff with the price value. That part is pure Java code. You can do anything you want.

When you execute the test, log.info() will create logs in your Log Viewer Panel.

jmeter_thread

BeanShell vars.put() Usage

You can set variables in your context during your test execution. This can be achieved by using vars.put(“KEY”, “VALUE”) method. Suppose that you have a scenario where you have to create users in a system. Every user needs a unique e-mail address and password. You want to create passwords and emails during a test run.

We have an HTTP sampler with POST methods. We need to pass some parameters to it and define some variables in the panel. Those variables will be created by BeanShell Processor.

http_request

Add Pre BeanShell Processor as a child to it. Add the code below.

import java.util.UUID;   
String uuid = UUID.randomUUID().toString();   
String[] tokenizedUUID = uuid.split("-");              
String Email = tokenizedUUID[4]+"@loadium.com";   
log.info("New User Mail "+Email); 
log.info("New User Password "+tokenizedUUID[4]);   
vars.put("email",Email); 
vars.put("password",tokenizedUUID[4]); 

The code generates a UUID string. We split that UUID with String class’ split function. We do some manipulation and put two values in test context by using vars.put(“KEY”, “VALUE”) function. Now we can create emails, and password variables on the fly.

Now run your test and take a look at the Tree View Listener. Your post data should look like  the data below. Emails and passwords are unique to each request.

POST data:

name=Canberk&password=792e23445aca&email=792e23445aca%40loadium.com&passwordControl=792e23445aca


POST data:

name=Canberk&password=9deb0b5666e5&email=9deb0b5666e5%40loadium.com&passwordControl=9deb0b5666e5


In case you are stuck somewhere during your performance test design, BeanShell Processor will save the day. As a result, it will be your most favorite JMeter component ever.

Note: In case you need additional operations, like CRUD operations to a data source, that JMeter is not able to handle with predefined libraries, just add necessary jar files in the lib folder in JMeter’s installation folder and use them in the BeanShell Processor.

Do you want to run your JMeter test in the cloud? Try Loadium today.

BeanShell

Published at DZone with permission of Canberk Akduygu. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!