Tuesday, September 23, 2014

JMeter file read methods

We can read a file in different way using JMeter, below are they

   1. FileReader.

            i. Char bu char.
           ii. Line by line.   
   2. FileInputStream.
           i. Char bu char.
          ii. Line by line.



















Below is the performance of the "File Reader" vs "File Input Stream". 





If we observe the results , File Reader is Faster when compared to "File Input Stream". So while using these two methodologies in your script , choose which one is good.
Note: In the script , I am reading single character at once.

File Reader: If we want to read file as a character/characters , we can opt "FileReader" class. User bean shell sampler/Bean shell post processor/Bean shell pre processor  to test this in your Jmeter

   We can read the file in two ways,
            1. char by char 
            2. line by line.

File Input Stream: If we want to read file as a stream/Streams , we can opt "File input Stream" class. User bean shell sampler/Bean shell post processor/Bean shell pre processor  to test this in your Jmeter

   We can read the file in two ways,

            1. char by char 
            2. line by line.

Below is the code
  1. File Reader - Read Char by char :
Java Version:
import java.io.FileReader;
import java.io.IOException;
public static void main(String[] args) throws IOException {
System.out.println("Method 1");

FileReader fileReader=new FileReader("C:/Work/java_practice/IODemo/src/madhu.txt");
int i =fileReader.read();
while(i!=-1){
System.out.print((char)i);
i=fileReader.read();
}

Jmeter Version:


import java.io.FileReader;
FileReader fileReader=new FileReader("C:/Work/java_practice/IODemo/src/madhu.txt");
   int i =fileReader.read();
   while(i!=-1){
    System.out.print((char)i);
    i=fileReader.read();
   }

2. File Reader - Read line by line:

Java Version:


import java.io.FileReader;

import java.io.IOException;
public static void main(String[] args) throws IOException {
FileReader fileReader1=new FileReader("C:/Work/java_practice/IODemo/src/madhu.txt"); BufferedReader bufferReader= new BufferedReader(fileReader1); String line=bufferReader.readLine();
 while(line!=null){
 System.out.println(line);
 line=bufferReader.readLine(); 
 }
 fileReader.close(); 
 bufferReader.close();
}

Jmeter Version:


import java.io.FileReader;
FileReader fileReader1=new FileReader("C:/Work/java_practice/IODemo/src/madhu.txt");
   BufferedReader bufferReader= new BufferedReader(fileReader1);
   String line=bufferReader.readLine();
   while(line!=null){
    System.out.println(line);
     line=bufferReader.readLine();
   }
   fileReader.close();

   bufferReader.close();

3. File Input Stream- Read Char by char :

Java Version:

import java.io.IOException;
import java.io.FileInputStream;
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream=new FileInputStream("C:/Work/java_practice/IODemo/src/madhu.txt");
int j=fileInputStream.read();
while (j!=-1){
System.out.print((char)j);
j=fileInputStream.read();
}
fileInputStream.close();
}

Jmeter Version:

import java.io.FileInputStream;
System.out.println("Method 2");
FileInputStream fileInputStream=new FileInputStream("C:/Work/java_practice/IODemo/src/madhu.txt");
int j=fileInputStream.read();
while (j!=-1){
System.out.print((char)j);
j=fileInputStream.read();
}
fileInputStream.close();


4. File Input Stream -Read line by line:

Java Version:


import java.io.IOException;

import java.io.FileInputStream;
import java.io.InputStreamReader;
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("C:/Work/java_practice/IODemo/src/madhu.txt");
 InputStreamReader inr=new InputStreamReader(in);//An InputStreamReader is a bridge from byte streams to character streams
 BufferedReader br = new BufferedReader(inr);
 String line1=br.readLine();
 while(line1!= null)
 {
  System.out.println(line1);
 }
 inr.close(); 
 in.close();
   }

Jmeter Version:


  FileInputStream in = new FileInputStream("C:/Work/java_practice/IODemo/src/madhu.txt");

 InputStreamReader inr=new InputStreamReader(in);//An InputStreamReader is a bridge from byte streams to character streams
 BufferedReader br = new BufferedReader(inr);
 String line1=br.readLine();
 while(line1!= null)
 {
  System.out.println(line1);
      line1=br.readLine();
 }
 inr.close(); 

 in.close();


Saturday, September 6, 2014

Base64 encode using Jmeter


In my application, we want to encode one of the jmeter varible to base64 variable  . We did this using using Bean shell pre processor. Below is the scenario


Scenario: I want to encode jmeter variable (session)  to Base64 and need to pass this value to Jmeter Sampler. Below is the steps to do this 

Download this project from GitHub

Step 1: Get dynamic session value using Regular expression extractor.
Add Thread Group to Test Plan and Add the sampler to Thread Group  , provide the necessary details to Http Sampler

IP:raw.githubusercontent.com
Port: 80
Path:/mcheepati/api/master/homepage
Method: Get


Step 2: Add the Regular expression extractor post processor as a child to Http Sampler. And add the below values to "Regular expression extractor" 



Step 3:Pass the encoded Base64 value(session) as a variable to http post method in this step.
Add the Http sampler as a child to Thread group 

 

Add the bean shell "pre Processor" as a child to this Http sampler and the below code to bean shell processor 



import org.apache.commons.codec.binary.Base64;

String emailIs= vars.get("email");

byte[] encryptedUid = Base64.encodeBase64(emailIs.getBytes());
      vars.put("genStringValue",new String(encryptedUid));

Note: We added Pre Processor as child, because  pre processor will executes before running the Http sampler.


Step 4:Save and run the Jmeter script and verify the post call values for Base64 encoded value.
Actual Value is              :jdwqoeendwqqm12sdw
Base64 encoded value is:amR3cW9lZW5kd3FxbTEyc2R3




Validate the jmeter Base64 response values from online Base64 http://www.base64encode.org/