java 如何测量使用 UDP 协议进行通信的服务器和客户端之间的响应时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/616896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to measure the response time between a server and a client that communicate using the UDP protocol?
提问by Robert S. Barnes
The aim of the test is to check the shape of the network response time between two hosts (client and server). Network response = the round trip time it takes to send a packet of data and receive it back. I am using the UDP protocol. How could I compute the response time ? I could just subtract TimeOfClientRequest - TimeOfClientResponseRecieved. But I'm not sure if this is the best approach. I can't do this only from inside the code and I'm thinking that the OS and computer load might interfere in the measuring process initiated by the client. By the way, I'm using Java.
测试的目的是检查两个主机(客户端和服务器)之间的网络响应时间的形状。网络响应 = 发送数据包和接收数据包所需的往返时间。我正在使用 UDP 协议。我怎么能计算响应时间?我可以减去 TimeOfClientRequest - TimeOfClientResponseRecieved。但我不确定这是否是最好的方法。我不能仅从代码内部执行此操作,并且我认为操作系统和计算机负载可能会干扰客户端启动的测量过程。顺便说一句,我正在使用Java。
I would like to listen to your ideas.
我想听听你的想法。
回答by Robert S. Barnes
Just use ping - RTT ( round trip time ) is one of the standard things it measures. If the size of the packets you're sending matters then ping also lets you specify the size of the data in each packet.
只需使用 ping - RTT(往返时间)是它衡量的标准内容之一。如果您发送的数据包的大小很重要,那么 ping 还可以让您指定每个数据包中数据的大小。
For example, I just sent 10 packets each with a 1024 byte payload to my gateway displaying only the summary statistics:
例如,我只向网关发送了 10 个数据包,每个数据包的负载为 1024 字节,仅显示摘要统计信息:
ping -c 10 -s 1024 -q 192.168.2.1
ping -c 10 -s 1024 -q 192.168.2.1
PING 192.168.2.1 (192.168.2.1) 1024(1052) bytes of data.
PING 192.168.2.1 (192.168.2.1) 1024(1052) 字节数据。
--- 192.168.2.1 ping statistics ---
--- 192.168.2.1 ping 统计 ---
10 packets transmitted, 10 received, 0% packet loss, time 9004ms
发送 10 个数据包,接收 10 个数据包,0% 数据包丢失,时间 9004ms
rtt min/avg/max/mdev = 2.566/4.921/8.411/2.035 ms
rtt min/avg/max/mdev = 2.566/4.921/8.411/2.035 毫秒
The last line starting with rtt ( round trip time ) is the info you're probably looking for.
以 rtt (往返时间)开头的最后一行是您可能正在寻找的信息。
回答by Eric Petroelje
I think the method you mention is fine. OS and computer load might interfere, but their effect would probably be negligible compared to the amount of time it takes to send the packets over the network.
我觉得你说的方法没问题。操作系统和计算机负载可能会干扰,但与通过网络发送数据包所需的时间相比,它们的影响可能可以忽略不计。
To even things out a bit, you could always send several packets back and forth and average the times out.
为了稍微平衡一下,您可以始终来回发送多个数据包并平均超时。
回答by Alnitak
If you have access to the code, then yes, just measure the time between when the request was sent and the receipt of the answer. Bear in mind that the standard timer in Java only has millisecond resolution.
如果您有权访问代码,那么是的,只需测量发送请求和收到答案之间的时间。请记住,Java 中的标准计时器只有毫秒分辨率。
Alternatively, use Wireshark to capture the packets on the wire - that software also records the timestamps against packets.
或者,使用 Wireshark 捕获线路上的数据包 - 该软件还会针对数据包记录时间戳。
Clearly in both cases the measured time depends on how fast the other end responds to your original request.
很明显,在这两种情况下,测量的时间都取决于另一端对您的原始请求的响应速度。
If you really just want to measure networklatency and control the far end yourself, use something like the echo 7/udpservice that many UNIX servers still support (albeit it's usually disabled to prevent its use in reflected DDoS attacks).
如果您真的只想测量网络延迟并自己控制远端,请使用echo 7/udp许多 UNIX 服务器仍然支持的服务(尽管它通常被禁用以防止在反射 DDoS 攻击中使用它)。
回答by siddhadev
it would be nice if you could send ICMP packages - I guess, because they are answered directly by the network layer, your answer will loose no time in user mode on the server.
如果您可以发送 ICMP 包,那就太好了 - 我猜,因为它们是由网络层直接回答的,所以您的回答不会在服务器上的用户模式下丢失。
Sending ICMP packages in java seems however not to be possible. You could:
然而,在 java 中发送 ICMP 包似乎是不可能的。你可以:
boolean status = InetAddress.getByName(host).isReachable(timeOut)
this will send an ICMP package, but that is not what you want.
这将发送一个 ICMP 包,但这不是您想要的。
However if you start the responder deamon on the server side with a higher priority, you will reduce the effect of server load.
但是,如果在具有更高优先级的服务器端启动响应者守护进程,则会减少服务器负载的影响。
Actually server load does not play a role, as long as it is bellow 100% CPU.
实际上服务器负载没有任何作用,只要低于 100% CPU 即可。
回答by Peter Lawrey
Use ping first, but you can measure the RTT by sending a packet and having the other end sending the packet back.
首先使用 ping,但您可以通过发送数据包并让另一端将数据包发回来测量 RTT。
It is important that you measure when the boxes are under typicalload because that will tell you the RTT you can expect to typically get.
测量盒子何时处于典型负载下很重要,因为这会告诉您通常可以获得的 RTT。
You can average the latencies over many packets, millions or even billions to get a consistent value.
您可以平均许多数据包的延迟,数百万甚至数十亿,以获得一致的值。
回答by foxty
Besides few answers mentioned about use ICMP ping to measure the RTT time is a good way.
除了提到使用ICMP ping测量RTT时间的几个答案是一个好方法。
I'd like to provide another way to measure the RTT via UDP if you can both control the server&client side. The basic flow as follow:
如果您可以同时控制服务器和客户端,我想提供另一种通过 UDP 测量 RTT 的方法。基本流程如下:
- Send a UDP packet P1 with time-stamp C1 from client to server
- Server put current time-stamp S1 to packet while received P1.
- Server side processing
- Server put current time-stamp S2 to packet before send it back to client.
- Client put current time-stamp C2 to packet while received P1.
- 从客户端向服务器发送带有时间戳 C1 的 UDP 数据包 P1
- 服务器在收到 P1 时将当前时间戳 S1 放入数据包。
- 服务器端处理
- 服务器将当前时间戳 S2 放入数据包中,然后再将其发送回客户端。
- 客户端在收到 P1 时将当前时间戳 C2 放入数据包。
After that, we can calculate the RTT = (C2-C1) - (S2-S1).
之后,我们可以计算 RTT = (C2-C1) - (S2-S1)。
It is may not accurate as ICMP ping and need extra control both on client&server side, but it is manageable.
它可能不像 ICMP ping 那样准确,并且需要在客户端和服务器端进行额外控制,但它是可以管理的。
回答by Sebastian Jespersen
While ping is a good start to measure latency, it is using the ICMP protocol instead of UDP. Packets of different protols usually have different priorities on routers etc.
虽然 ping 是衡量延迟的良好开端,但它使用的是 ICMP 协议而不是 UDP。不同协议的数据包通常在路由器等上具有不同的优先级。
You could use netperf to measure the UDP roundtrip time: http://www.netperf.org/netperf/training/Netperf.html#0.2.2Z141Z1.SUJSTF.9R2DBD.T
您可以使用 netperf 来测量 UDP 往返时间:http: //www.netperf.org/netperf/training/Netperf.html#0.2.2Z141Z1.SUJSTF.9R2DBD.T

