问题描述
当我使用 10 gig 网络接口从一台 solaris m/c 跟踪路由到另一台时,40 字节数据包需要 0.073 毫秒.
When i do traceroute from one solaris m/c to another using 10 gig network interface it takes 0.073 ms for 40 bytes packets.
当我在 Java 中做同样的事情时,时间会更长.即使经过 10K 次迭代,它也更长.可能是什么原因 ?
When i do the same thing in java, the time is way longer. It is longer even after 10K iteration. What could be the reason ?
Java:发件人(代码段)
Java: Sender (Snippet)
Socket sendingSocket = new Socket(address, RECEIVER_PORT);
sendingSocket.setTcpNoDelay(true);
OutputStream outputStream = sendingSocket.getOutputStream();
byte[] msg = new byte[64]; // assume that it is populated.
for (int i = 0; i < 10000; i++) {
long start = System.nanoTime();
outputStream.write(msg,0,64);
outputStream.flush();
inputStream.read(msg,0,64); // inputStream is initialized like outputstream
long end = System.nanoTime();
}
它需要更长的时间 69 毫秒,它甚至不取决于字节大小.即使我将其减少为 1 个字节的数组,它仍然需要 69 毫秒.任何评论/建议?
It takes way longer 69 millis and it does not even depends upon the byte size. Even if i reduce it to say 1 byte array, it still takes 69 millis. Any comment/Suggestion ?
其他观察:1. OutputStream.write 和flush 只需要6 微秒.2.同样在另一端TCPReceiver端接收和写回,只需要6微秒.
Other Observation:1. OutputStream.write and flush only takes 6 micros.2. Similarly on the other end TCPReceiver side which receives and writes back, it only takes 6 micros.
解决方案:谢谢大家对这个问题的回复.我发现这是由于套接字缓冲区大小:
Solution:Thank you all you responded for this query.I found that it was due to the socket buffer size:
在 solaris m/c 上设置的默认缓冲区大小.
Default buffer size set on solaris m/c.
接收缓冲区大小 49152.
Received Buffer Size 49152.
发送缓冲区大小 7552.
Sending Buffer Size 7552.
我增加了套接字缓冲区大小,性能几乎与 traceRoute 匹配.
I increased the socket buffer size and the performance almost matches traceRoute.
推荐答案
你不是在比较喜欢和喜欢.ICMP 和 TCP 是不同的协议.
You are not comparing like with like. ICMP and TCP are way different protocols.
如果您想确定延迟是在您的代码、JVM、Solaris TCP 堆栈还是网络中,您必须从 tcpdump/wireshark 等开始.
If you want to decide if the latency is in your code, the JVM, Solaris TCP stack or the network you'll have to start with tcpdump / wireshark etc.
这篇关于使用java计算Tcp往返时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!