【发布时间】:2015-06-27 23:00:45
【问题描述】:
我有一个 SSD 磁盘,每个规格应提供不少于 10k IOPS。我的基准测试证实它可以为我提供 20k IOPS。
然后我创建这样一个测试:
private static final int sector = 4*1024;
private static byte[] buf = new byte[sector];
private static int duration = 10; // seconds to run
private static long[] timings = new long[50000];
public static final void main(String[] args) throws IOException {
String filename = args[0];
long size = Long.parseLong(args[1]);
RandomAccessFile raf = new RandomAccessFile(filename, "r");
Random rnd = new Random();
long start = System.currentTimeMillis();
int ios = 0;
while (System.currentTimeMillis()-start<duration*1000) {
long t1 = System.currentTimeMillis();
long pos = (long)(rnd.nextDouble()*(size>>12));
raf.seek(pos<<12);
int count = raf.read(buf);
timings[ios] = System.currentTimeMillis() - t1;
++ios;
}
System.out.println("Measured IOPS: " + ios/duration);
int totalBytes = ios*sector;
double totalSeconds = (System.currentTimeMillis()-start)/1000.0;
double speed = totalBytes/totalSeconds/1024/1024;
System.out.println(totalBytes+" bytes transferred in "+totalSeconds+" secs ("+speed+" MiB/sec)");
raf.close();
Arrays.sort(timings);
int l = timings.length;
System.out.println("The longest IO = " + timings[l-1]);
System.out.println("Median duration = " + timings[l-(ios/2)]);
System.out.println("75% duration = " + timings[l-(ios * 3 / 4)]);
System.out.println("90% duration = " + timings[l-(ios * 9 / 10)]);
System.out.println("95% duration = " + timings[l-(ios * 19 / 20)]);
System.out.println("99% duration = " + timings[l-(ios * 99 / 100)]);
}
然后我运行这个示例并得到 2186 IOPS:
$ sudo java -cp ./classes NioTest /dev/disk0 240057409536
Measured IOPS: 2186
89550848 bytes transferred in 10.0 secs (8.540234375 MiB/sec)
The longest IO = 35
Median duration = 0
75% duration = 0
90% duration = 0
95% duration = 0
99% duration = 0
为什么它的工作速度比 C 中的相同测试慢得多?
更新:这是提供 20k IOPS 的 Python 代码:
def iops(dev, blocksize=4096, t=10):
fh = open(dev, 'r')
count = 0
start = time.time()
while time.time() < start+t:
count += 1
pos = random.randint(0, mediasize(dev) - blocksize) # need at least one block left
pos &= ~(blocksize-1) # sector alignment at blocksize
fh.seek(pos)
blockdata = fh.read(blocksize)
end = time.time()
t = end - start
fh.close()
Update2:NIO代码(只是一段,不会复制所有方法)
...
RandomAccessFile raf = new RandomAccessFile(filename, "r");
InputStream in = Channels.newInputStream(raf.getChannel());
...
int count = in.read(buf);
...
【问题讨论】:
-
你在 Java 和 C 中使用相同的随机数序列吗?请注意,原始磁盘传输速度无关紧要。对于随机访问,您需要查看查找时间。
-
为什么将 40000 个 .java 文件写入我的袖珍 USB 驱动器需要 8 分钟。与我撕掉的 1 mp4 的 20 秒(相同的累积大小)相比?我要退款(用于 USB 驱动器)
-
用 C 语言发布相同测试的代码,以便读者可以确定正在比较的内容。
-
我怀疑你不是在比较同类。除非您明确使用缓冲,否则不会缓冲 Java IO。除非您使用低级 API,否则默认情况下标准 C API 会缓冲。你的 C 代码是什么样的?
-
有趣的是该类名为 NioTest 但不包含 NIO 代码。虽然有大量证据表明使用 NIO 并不能保证速度提高,但我仍然希望看到使用 FileChannel 完成相同的测试,甚至可能使用 MappedByteBuffer,因为该问题声称 Java 本身存在缺陷。
标签: java performance io java-8 solid-state-drive