我正在尝试对我的硬盘进行基准测试,并找到以 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 硬盘 @ 5400 Rpm,Intel i7 四核 @2.1Ghz,8GB ddr 3 Ram。有人可以告诉我吞吐量是否会那么低,还是我使用了错误的方法?