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();


No comments: