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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Trending

  • Building a Real-Time Audio Transcription System With OpenAI’s Realtime API
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • How to Merge HTML Documents in Java
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot

BeanShell Processor Tutorial 2: Advanced Usage

Learn about working with dates, file writing, and thread groups or responses in BeanShell.

By 
Canberk Akduygu user avatar
Canberk Akduygu
·
Nov. 07, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.9K Views

Join the DZone community and get the full member experience.

Join For Free

BeanShell is the most powerful component of Apache JMeter. You can execute any Java code by using BeanShell scripts. If you are new to BeanShell, please take a look basic usage of BeanShell Processor.

In this BeanShell tutorial, I want to deep dive into BeanShell and show you some other example of its usage. If you’re familiar with Java or any other language, it is very easy to get used to it.

Sample 1: Dealing With Dates

Dates are the most frightening component of any developer and testers also. In case you need deal with dynamic dates during your test, just follow these steps.

In this BeanShell example, we will create two dates for a reservation system.


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, Integer.parseInt("5"));
Calendar calReturn = Calendar.getInstance();
calReturn.add(Calendar.DAY_OF_YEAR, Integer.parseInt("10"));

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

vars.put("outFormatDate",df.format(cal.getTime()));
vars.put("returnFormatDate",df.format(calReturn.getTime()));

log.info("Departure Date "+vars.get("outFormatDate"));
log.info("Arrival Date "+vars.get("returnFormatDate"));


After running the code, you will have output:


2018-05-07 12:18:15,386 INFO o.a.j.u.BeanShellTestElement: Departure Date 2018-05-12
2018-05-07 12:18:15,387 INFO o.a.j.u.BeanShellTestElement: Arrival Date 2018-05-17


Our code added 5 and 7 days to today’s date in order to create two new Date objects. Then we assigned those variables by using vars.put() method to be able to use them in JMeter scripts.

Sample 2: File Writing

You might need some extra logging during your test. Basic FileOutputStream and PrintStream are enough to achieve this need.


FileOutputStream fout = new FileOutputStream(PATH+"/log.txt");
PrintStream out = new PrintStream(fout);
out.println("Departure Date "+vars.get("outFormatDate"));
out.println("Arrival Date "+vars.get("returnFormatDate"));
out.close();


This will create a log.txt file under the PATH you specified. PrintStream will write anything you want in that text file.

Sample 3: Dealing With Thread Groups or Responses

You can enable your BeanShell Processor to get information from your Thread.



log.info("THREAD Name "+ctx.getThread().getThreadName());
log.info("THREAD Name "+ctx.getThread().getStartTime());
log.info("Response Time is: " + prev.getTime().toString());
log.info("Connect Time is: " + prev.getConnectTime().toString());
log.info("URL is: " + prev.getURL());
log.info("The result is passed: " + prev.isSuccessful().toString());
log.info("Headers are: " + prev.getResponseHeaders());


CTX variable gives you the control over your current Thread. There are many functions that you can use.

PREV variable gives you control over your sampler’s response.

Then you have the response:


2018-05-07 13:02:00,387 INFO o.a.j.u.BeanShellTestElement: THREAD Name Thread Group 1-1
2018-05-07 13:02:00,387 INFO o.a.j.u.BeanShellTestElement: Thread Start Time is: 0
2018-05-07 13:02:00,387 INFO o.a.j.u.BeanShellTestElement: Thread End Time is: 0
2018-05-07 13:02:00,387 INFO o.a.j.u.BeanShellTestElement: Response Time is: 955
2018-05-07 13:02:00,388 INFO o.a.j.u.BeanShellTestElement: Connect Time is: 326
2018-05-07 13:02:00,388 INFO o.a.j.u.BeanShellTestElement: URL is: https://loadium.com/
2018-05-07 13:02:00,388 INFO o.a.j.u.BeanShellTestElement: The result is passed: true
2018-05-07 13:02:00,388 INFO o.a.j.u.BeanShellTestElement: Headers are: HTTP/1.1 200 OK
Date: Mon, 07 May 2018 10:02:00 GMT
Server: Apache/2.4.18 (Ubuntu)
Strict-Transport-Security: max-age=63072000
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Link: <https://loadium.com/wp-json/>; rel="https://api.w.org/"
Link: <https://loadium.com/>; rel=shortlink
Vary: Accept-Encoding
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8


As you can see, the sky is the limit. Keep on load testing by using BeanShell Processors.

Happy load testing!

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!