为什么 ByteBuffer.allocate() 和 ByteBuffer.allocateDirect() 之间的奇数性能曲线存在差异
我正在研究一些代码,这些代码在直接字节缓冲区中效果最好 - 寿命长且很大(每个连接数十到数百兆字节)。在用s计算出确切的循环结构时,我对vs进行了一些微基准测试。 性能。SocketChannel
SocketChannel
FileChannel
ByteBuffer.allocate()
ByteBuffer.allocateDirect()
结果中有一个我无法真正解释的惊喜。在下图中,传输实现的 256KB 和 512KB 处有一个非常明显的悬崖 - 性能下降了约 50%!似乎还有一个较小的性能悬崖。(%增益序列有助于可视化这些变化。ByteBuffer.allocate()
ByteBuffer.allocateDirect()
缓冲区大小(字节)与时间 (MS) 的关系
为什么 ByteBuffer.allocate() 和 ByteBuffer.allocateDirect()
之间的奇数性能曲线存在差异?幕后究竟发生了什么?
它可能非常依赖于硬件和操作系统,所以以下是这些细节:
- MacBook Pro 带双核酷睿 2 CPU
- 英特尔 X25M 固态盘驱动器
- OSX 10.6.4
源代码,根据要求:
package ch.dietpizza.bench;
import static java.lang.String.format;
import static java.lang.System.out;
import static java.nio.ByteBuffer.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
public class SocketChannelByteBufferExample {
private static WritableByteChannel target;
private static ReadableByteChannel source;
private static ByteBuffer buffer;
public static void main(String[] args) throws IOException, InterruptedException {
long timeDirect;
long normal;
out.println("start");
for (int i = 512; i <= 1024 * 1024 * 64; i *= 2) {
buffer = allocateDirect(i);
timeDirect = copyShortest();
buffer = allocate(i);
normal = copyShortest();
out.println(format("%d, %d, %d", i, normal, timeDirect));
}
out.println("stop");
}
private static long copyShortest() throws IOException, InterruptedException {
int result = 0;
for (int i = 0; i < 100; i++) {
int single = copyOnce();
result = (i == 0) ? single : Math.min(result, single);
}
return result;
}
private static int copyOnce() throws IOException, InterruptedException {
initialize();
long start = System.currentTimeMillis();
while (source.read(buffer)!= -1) {
buffer.flip();
target.write(buffer);
buffer.clear(); //pos = 0, limit = capacity
}
long time = System.currentTimeMillis() - start;
rest();
return (int)time;
}
private static void initialize() throws UnknownHostException, IOException {
InputStream is = new FileInputStream(new File("/Users/stu/temp/robyn.in"));//315 MB file
OutputStream os = new FileOutputStream(new File("/dev/null"));
target = Channels.newChannel(os);
source = Channels.newChannel(is);
}
private static void rest() throws InterruptedException {
System.gc();
Thread.sleep(200);
}
}