我正在尝试对我的硬盘进行基准测试,找出吞吐量( Mb/s )和延迟(毫秒)。这是我的代码。
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();
}
}我得到的输出是
56.065550577
10.0
Throughput=0.17836264688538242 Mb/s
Latency=0.057668 ms我有一台速度相当快的电脑,硬盘1TB,转速5400rpm,英特尔i7,四核2.1 8GB,8 8GB DDR3 Ram。有人能告诉我吞吐量是否会那么低,还是我使用了错误的方法?
发布于 2013-01-28 01:56:50
这个数字对我来说似乎是正确的。您正在进行大量的系统调用。178K系统调用/秒对于2.1 GB的i7来说是合适的。在3.5 GHz的I7上,每秒可以获得大约300K的系统调用。
当你第一次运行一个方法时,它必须被加载,这会减慢它的运行速度。即使在这个阶段它没有编译成本机代码,也需要做一些工作,对于第一次调用来说,57微秒的延迟似乎是合理的。
发布于 2013-01-29 20:56:58
我相信有很多磁盘基准测试...谷歌给出了很多链接。看看适合基准测试的网站,他们可能会发布他们的工具来使结果可重复。使用它们,这样您就可以与其他地方获得的结果进行比较。除非你有兴趣对一些非常特殊的情况进行基准测试。在这种情况下,你最好测量你的情况下的性能,包括整个堆栈:程序,编译器,操作系统和硬件。
发布于 2016-04-10 06:19:47
在我看来,只要你在测试的所有硬盘或平台上使用相同的代码,这似乎是合理的。
这是我用java写的一个实用程序,用来对磁盘IO进行基准测试:https://sourceforge.net/projects/jdiskmark/,如果我能像你的代码那样添加延迟检查,那就更好了,但它在带宽方面做得还不错。您可以在gitlab上查看代码。
https://stackoverflow.com/questions/14549456
复制相似问题