Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure Disk Speed in Java for Benchmarking

I would like to know how can you measure disk speed using Java API.

Random read,sequential read and Random and sequential write.

If someone thinks it's not a real question. Please explain so before closing it.

Thanks

like image 821
Nomad Avatar asked Dec 16 '22 14:12

Nomad


1 Answers

You can take a look at a disk utility I wrote in java. It may not be super fancy but it works.

https://sourceforge.net/projects/jdiskmark/

Here is a snippet of the write measurement code:

try (RandomAccessFile rAccFile = new RandomAccessFile(testFile,mode)) {
    for (int b=0; b<numOfBlocks; b++) {
        if (App.randomEnable) {
            int rLoc = Util.randInt(0, numOfBlocks-1);
            rAccFile.seek(rLoc*blockSize);
        } else {
            rAccFile.seek(b*blockSize);
        }
        rAccFile.write(blockArr, 0, blockSize);
        totalBytesWrittenInMark += blockSize;
        wUnitsComplete++;
        unitsComplete = rUnitsComplete + wUnitsComplete;
        percentComplete = (float)unitsComplete/(float)unitsTotal * 100f;
    }
}
long endTime = System.nanoTime();
long elapsedTimeNs = endTime - startTime;
double sec = (double)elapsedTimeNs / (double)1000000000;
double mbWritten = (double)totalBytesWrittenInMark / (double)MEGABYTE;
long bwMbSec = mbWritten / sec;
System.out.println("Write IO is " + bwMbSec + " MB/s"
    + "(MB written " + mbWritten + " in " + sec + " sec)");

The code is on gitlab: https://gitlab.com/jamesmarkchan/jDiskMark/

jDiskMark screenshot

like image 132
simgineer Avatar answered Dec 18 '22 04:12

simgineer