TESTEVERYTHING

Thursday, 17 January 2013

Jmeter bean shell script, create file, read jmeter variable value, store script variable value into jmeter variable

Hi All,

Some times in JMeter scripting we need to store the output value in file or wanna show the run time  variable value in console window. OR we need to get the current JMeter script directory path. This can be achieved via scripting in Bean Shell Pre/Post Processor.

Here I am using time and counter function of JMeter to create the file.In bean shell scripting we are reading the JMeter variables value and storing script variable value into JMeter variable or creating Jmeter variable

Here is the  Bean Shell script :

import org.apache.jmeter.services.FileServer;
import org.apache.jmeter.services.FileServer;
import java.util.Date;
import java.text.SimpleDateFormat;

SimpleDateFormat formatter = new SimpleDateFormat( "yyyyMMddHHmmss" );  
String datetime = formatter.format( new java.util.Date() ); 

// Get current running counter value(C refrence name deifned in counter config element)

String counter= vars.get("C");

// Get Jmeter variable value in bean shell script by vars.get method

String timer= vars.get("JmeterTimerVariable");

// here JmeterTimerVariable defined in User Defined variable  config element


//  Display value in Console 

System.out.println("Current counter value = " + counter);

System.out.println("JmeterTimerVariable value = " + timer);


// Store beanshel script variable into Jmeter variable by using vars.put method and we will user this jmetervariable in Wikisearch http request

vars.put("JmeterSearchVariable",datetime+counter);

// get JmeterSearchVariable value in beanshell script

String SearchVariable = vars.get("JmeterSearchVariable");

System.out.println("SearchVariable value = " + SearchVariable);

// Here we can get the directory path of Jmeter script file

String DirPath = FileServer.getFileServer().getBaseDir();

// write into jmeter.log file under Jmeter/bin directory

log.info(DirPath);

System.out.println("Directory path of Jmeter script file = " + FileServer.getFileServer().getBaseDir());

// we will create a file under directory of jmeter script file with name JmeterReords using File system True file will be created if not and data will //append into the file False will create a new file with fresh data

f = new FileOutputStream(FileServer.getFileServer().getBaseDir()+"\\JmeterReords.txt", true); 
p = new PrintStream(f); 
// write data into file 
p.println("Current counter value = " + counter);
p.println("JmeterTimerVariable value = " + timer);
p.println("Directory path of Jmeter script file = " +DirPath);
p.close();
f.close();

// if you want to create unique file for each loop counter refer below script

String uniquefilename = timer+counter;
f = new FileOutputStream(FileServer.getFileServer().getBaseDir()+"\\"+uniquefilename+".log", true); 
p = new PrintStream(f); 
// write data into file 
p.println("Current counter value = " + counter);
p.println("JmeterTimerVariable value = " + timer);
p.println("Directory path of Jmeter script file = " +DirPath);
p.close();
f.close();





Here is the structure of JMeter script

Jmeter Bean Shell script
Jmeter bean Shell script


7 comments:

Anonymous said...

I am new to Jmeter and I was trying to implement what you had done before with Bean shell.
I got stuck where the ${JmeterSerchVariable} where did you define this? Can you please email me more detailed steps to implement this or send me the jmx file. I would greatly appreciate it.
Thanking you very much for your help.
Venkat
vp1515@att.com

RajivKumarNandvani said...

this variable ${JmeterSerchVariable} I definedin beanshell script by using vars.put method

// Store beanshel script variable into Jmeter variable by using vars.put method

vars.put("JmeterSearchVariable",datetime+counter);

hope its clear..

Sankar said...

Wonderful post Rajiv. Really appreciate your efforts.

Much helpful.

Thanks,
Sankar

Anonymous said...

I am unable to run this...do we need to take care other things in order to implement this?

The Life said...

How to define the value for Sampler Name (your example: Wiki Search) and the response time in BeanShell, and print the Header Name into log (Header Name: Sampler, under is: Wiki Search)

armel said...

Hi, can someone help me with a simple beanshell script that doesn't work. I am trying to generate a random uuid:
----------------------------------------------------
import java.util.*;

UUID uuid = UUID.randomUUID().toString();
vars.put("BASELINE_UUID",uuid);
-----------------------------------------------------
There is no error but the Debug Sampler shows that BASELINE_UUID is null. What am I doing wrong?

glinius said...

Thanks for the great guide. From my side I would recommend using Beanshell for something very light. For "heavy" operations it is better to use JSR223 Sampler and "groovy" as a language cause Beanshell interpreter has well known performance limitations and problems and groovy scripts can be compiled into bytecode hence performance will be very close to native Java.

See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide for groovy scripting engine installation instructions and best practices.

Post a Comment

Which one is right ?

Translate







Tweet