Tuesday, June 28, 2016

Jmeter Beanshell 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() ); 
1 Get current running counter value(C refrence name deifned in Counter config element)
String counter= vars.get("C");
2 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
3 Display value in Console
System.out.println("Current counter value = " + counter);

System.out.println("JmeterTimerVariable value = " + timer);
4 Store beanshell script variable into Jmeter variable by using vars.put method and we will use this jmetervariable in Wikisearch http request
vars.put("JmeterSearchVariable",datetime+counter);
5 Get JmeterSearchVariable value in beanshell script
String SearchVariable = vars.get("JmeterSearchVariable");

System.out.println("SearchVariable value = " + SearchVariable);
6 Here we can get the directory path of Jmeter script file
String DirPath = FileServer.getFileServer().getBaseDir();
7 Write into jmeter.log file under Jmeter/bin directory
log.info(DirPath);

System.out.println("Directory path of Jmeter script file = " + FileServer.getFileServer().getBaseDir());
8 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); 
9 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();
10 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); 
11 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(); 
 
 
 
 
I ran into a similar situation a while ago where I had to upload a signature file as part of a POST request. This signature file lived relative to the folder structure in which my JMeter scripts were located.
Try using the following BeanShell expression (which can be accessed as a system-level property) to resolve the relative path from the location in which your JMeter test-cases are located/executed.
${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
  • You can enter this with the name of the file you want to send/POST in the "Send Files With the Request" field by simply appending the file name.
    ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}fileName.sig
  • A more cleaner implementation would be declare this as a variable (local or global depending on the scope) and use this variable within the appropriate fields.
    absolutePath = ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
    ${absolutePath}fileName.sig
NOTE: This approach is super useful if you intend to plug your JMeter test files into a Continuous Integration system such as Jenkins. It makes your test-cases more maintainable.