gpt4 book ai didi

java - 关于linux IO性能

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:54 24 4
gpt4 key购买 nike

我使用 FileChannel 编写了一个程序来测试 java 中的 IO 性能。立即写入数据并调用 force(false)。我的linux服务器有12个ssd硬盘,sda~sdl,我测试写数据到不同的硬盘,性能差别很大,不知道为什么?

代码:

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);
}

结果是这样的:
sda:写入 1000000 条记录,4299995700 字节,强制平均时间:273480 ns
sdb:写入 100000 条记录,429995700 字节,强制平均时间:5868387 ns

平均时间差异很大。

这里是一些IO监控数据。
安全数据: iostat data image
安全数据库: iostat data image

最佳答案

您需要先使用一些标准工具(如 fio)测量您的 SSD 磁盘性能.

然后您可以使用 fio 输出的数字再次测试您的实用程序。

看起来您正在写入 Linux 写入缓存,以便可以解释您的结果:)

关于java - 关于linux IO性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com