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
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With