Search code examples
javaiobenchmarking

Benchmarking HDD


I am trying to benchmark my hard disk and find the throughput in Mb/s and latency in milliseconds. This is my code.

public class OneMB implements Timer {
public static void main(String a[]) throws IOException {
    OneMB oneMB = new OneMB();
    oneMB.process();

}
public void process() throws IOException{
    RandomAccessFile randomAccessFile=null;
    try{            
        File file=new File("oneMByte.txt");
        byte[] b=new byte[1024];
        randomAccessFile=new RandomAccessFile(file, "rw");
        randomAccessFile.setLength(1024*1024*10);
        long endLatency=0;
        int i=0;
        long startWrite = this.getTimer();
        randomAccessFile.writeBoolean(true);
        endLatency=this.getTimer();                 
        for (i = 0; i < 1024*10*1024-1; i++) {
            randomAccessFile.writeBoolean(true); //Writes a boolean to the file as a one-byte value.
        }
        long endWrite = this.getTimer();
        randomAccessFile.seek(0);
        randomAccessFile.readFully(b);
        long endRead=this.getTimer();
        double timeTaken=(endRead-startWrite)/1000000000.0;
        double data=10.0;
        double throughput=data/timeTaken;
        double latency=(endLatency-startWrite)/1000000.0;//time for the reception of 1 byte
        System.out.println(timeTaken);
        System.out.println(data);
        System.out.println("Throughput="+throughput+" Mb/s");
        System.out.println("Latency="+latency+" ms");
        randomAccessFile.close();
    }
    catch (Exception e) {
        e.printStackTrace();
        randomAccessFile.close();
    }
}

@Override
public long getTimer() {
    // TODO Auto-generated method stub
    return System.nanoTime();
}
}

I get the output as

56.065550577
10.0
Throughput=0.17836264688538242 Mb/s
Latency=0.057668 ms

I have a reasonably fast computer with 1TB harddisk @ 5400 Rpm, Intel i7 with quad core @2.1Ghz, 8GB ddr 3 Ram. Could someone tell me if the throughput would be that low or am I using a wrong approach?


Solution

  • The number appears to be about right to me. You are making a very large number of system calls. 178K system calls/s is about right for a 2.1 GB i7. On a 3.5 GHz I7 you can get about 300K system calls per second.

    The very first time you run a method it has to be loaded and this slows it down. Even though it is not compiled to native code in this phase, some work has to be done and a latency of 57 micro-seconds seems reasonable for a first call.