Java 关于linux IO性能
我编写了一个程序,使用FileChannel在java中测试IO性能。写入数据并立即调用force(false)。我的Linux服务器有12个ssd硬盘,sda~sdl,我测试将数据写入不同的硬盘,性能差异很大,我不知道为什么 代码:Java 关于linux IO性能,java,linux,performance,io,Java,Linux,Performance,Io,我编写了一个程序,使用FileChannel在java中测试IO性能。写入数据并立即调用force(false)。我的Linux服务器有12个ssd硬盘,sda~sdl,我测试将数据写入不同的硬盘,性能差异很大,我不知道为什么 代码: publicstaticvoidmain(String[]args)抛出IOException、interruptedeexception{ RandomAccessFile aFile=新的RandomAccessFile(args[0],“rw”); int
publicstaticvoidmain(String[]args)抛出IOException、interruptedeexception{
RandomAccessFile aFile=新的RandomAccessFile(args[0],“rw”);
int count=Integer.parseInt(args[1]);
int idx=计数;
FileChannel=aFile.getChannel();
长时间=0;
长字节=0;
而(--idx>0){
String newData=“写入文件的新字符串…”+System.currentTimeMillis();
字符串buff=“”;
对于(int i=0;i,您需要从使用一些标准工具(如)测量SSD磁盘性能开始
然后,您可以使用fio输出中的数字再次测试您的实用程序
看起来您正在写入Linux写缓存,以便能够解释您的结果:)你能澄清你认为的问题吗?似乎没有足够的信息得出任何结论。0.005秒的变化对磁盘IO没有多大意义,除非它是高度可复制的。我假设驱动器都是一样的,除了sda
上也有操作系统吗?注意:当你为一台台式机写入文件时hmark,您不需要创建虚拟数据(除非数据将被压缩)一个满是零字节的文件和一个包含随机数据的文件是一样的。所有这些磁盘都是同一控制器上的同一型号的驱动器,具有相同的文件系统类型吗?顺便说一句,对于SSD来说,即使是72 MB/s听起来也不是很高。我会寻找400-500 MB/sI,从写入远远超过4 KB的块开始,例如256 KB或2 MB。所有的硬盘驱动器都是我们是同一型号的ext3文件系统,我不知道为什么sda的强制延迟要低得多。
public static void main(String[] args) throws IOException, InterruptedException {
RandomAccessFile aFile = new RandomAccessFile(args[0], "rw");
int count = Integer.parseInt(args[1]);
int idx = count;
FileChannel channel = aFile.getChannel();
long time = 0;
long bytes = 0;
while (--idx > 0) {
String newData = "New String to write to file..." + System.currentTimeMillis();
String buff = "";
for (int i = 0 ; i<100; i++) {
buff += newData;
}
bytes += buff.length();
ByteBuffer buf = ByteBuffer.allocate(buff.length());
buf.clear();
buf.put(buff.getBytes());
buf.flip();
while(buf.hasRemaining()) {
channel.write(buf);
}
long st = System.nanoTime();
channel.force(false);
long et = System.nanoTime();
System.out.println("force time : " + (et - st));
time += (et -st);
}
System.out.println("wirte " + count + " record, " + bytes + " bytes, force avg time : " + time/count);
}