Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

io - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...