Sunday, July 5, 2020

Jmeter Automation using Java

Introduction:

Even though I come from a development background, I had to work as a support team member for some time for one of the project. It's quite common in IT industry that depending upon the project need your role will change accordingly even if it doesn't match with your skills set. 

There were many repeated tasks on daily basis which we have to do. Like - sending reports to business teams, reprocess exceptions, close alerts, process stuck orders etc. I did not like this repeated work and thought of automating few things. I observed the pattern for few tasks was like -
  • Connect to DB
  • Execute a query to DB
  • Export the output to CSV file
  • Execute existing Jmeter script which reads data from CSV file and invokes API/Service
So, I decided to automate the tasks which comes under this pattern using java. I did it and it saved approx 1 hour of my time everyday. Later it was used by my team members and client appreciated it as well.

I though of sharing this with you, if in case it helps you or anyone of your team members.

How to Setup the application:

Required Softwares:
  • Install and setup Apache Jmeter 3.2 or higher
  • Install and setup JDK 1.8 or higher
Required Jars in the classpath to execute java class:
  • DB jar to connect to Database (db2jcc.jar for DB2 or ojdbc.jar for Oracle)
  • JRE 1.8 or higher
  • Jmeter jars (<jmeter_home>/lib/*.jar, <jmeter_home>/lib/ext/*.jar)
  • OpenCSV jar (opencsv-3.4.jar or higher)
Java Code:

There are total 4 java class required -
  • DatabaseConnection.java (class to make DB connection and get result set from DB)
  • OpenCsvWriterByResultSet.java (class to write DB result set to csv file using openCSV writer)
  • JMeterFromExistingJMX.java (class to execute Jmeter script from existing jmx file)
  • JmeterAutomationWrapper.java (Main class to execute Jmeter Automation. It will require a property file name as command line argument. Structure of property file has been explained below.)

Required files to run the program:

property file - JmeterAutomationWrapper is the main class, it requires a properties file path as a command line argument which will have the required property values to execute the program

This is an example of property file for Windows OS. Change the values according to your setup:


How to Run the program:
  • Export the above java project as jar file as 'jmeter_automation.jar'
  • Create a batch file like below for Windows OS and change the values according to your setup
  • output is redirected to Test.log file. you can change it according to your setup. This is a sample output after successful execution. It has number of records written to CSV file and jmeter summary as well.

How to automate the process:
  • If you are using Windows OS, then you can use Windows Task scheduler to automate the program execution. This link has steps given with screen shots to setup basic task scheduler.
  • If you are using Linux OS, then you can use Cron Jobs to automate the program execution. This link has steps given with screen shots to setup cron jobs.

If you think this is useful and want me to create more articles like this, then Please use comment box to give your valuable feedback and suggestions.


1 comment: