英文:
Measuring time between hops in network (JAVA)
问题
我正在尝试收集从网络中的一个节点到另一个节点的HTTP请求所花费的时间数据,这里是我正在处理的一个简单网络拓扑结构:
我正在使用树莓派4型号B
PC ---- RaspPi(1) ---- RaspPi(2) ---- RaspPi(n) ---- ...
这些节点中的每一个都有自己的应用程序,可以处理HTTP请求,收集数据的想法是:
假设我有一个以RaspPi(n)为目标的HTTP请求,现在一旦请求通过每个节点,我就记录下它到达节点时的时间戳,从那时起,我可以计算出DeltaT
,即请求在两个连续节点之间传输所需的时间。
我尝试过使用:
Date now = Calendar.getInstance();
TimeStamp ts = new TimeStamp(now.getTime());
以及
System.currentTimeMillis()
来获取时间戳,问题是,我收集到的数据有负的DeltaT,例如:RaspPi(2)的时间戳在RaspPi(1)之前。我在周围进行了一些搜索,发现我上面使用的两种方法是不单调的(来源1和来源2)。
所以我正在考虑的另一种方法是使用System.nanoTime()
,但这似乎在不同的JVM上不起作用,而我所有的网络节点都在不同的JVM上运行。
我不知道是否有更好的方法来收集这些数据,或者是否有一些解决我使用的那些方法的方法。
如果我没有表达清楚,请告诉我。谢谢阅读。
英文:
I'm trying to gather data of time that a HTTP request take to travel from 1 node to another in a network, here's a simple network topology that I'm working on:
I'm using Raspberry Pi 4 model B
PC ---- RaspPi(1) ---- RaspPi(2) ---- RaspPi(n) ---- ...
Each of these nodes have their own application that can work with HTTP, the idea to gather data is:
Suppose I have a HTTP request that has RaspPi(n) as its destination, now once the request traverse through each node, I logged out the TIMESTAMP when it reaches the node, from then I can calculate DeltaT
, which is the time it takes for my request to travel between 2 consecutive nodes.
I have tried to use:
Date now = Calendar.getInstance();
TimeStamp ts = new TimeStamp(now.getTime());
And
System.currentTimeMillis()
to get the TIMESTAMP, the problem is, data that I gathered have negative DeltaT, which for example: TIMESTAMP at RaspPi(2) is before TIMESTAMP at RaspPi(1). I've done some searching around and found that the 2 methods I used above are not monotonic (Source 1 and Source 2).
So the other method I'm thinking is to use System.nanoTime()
but this doesn't seem to work on different JVMs, which is all of my network nodes.
I don't know if there is a better approach to gather these data, or some work around that I can do to fix those methods I used.
Please let me know if I haven't make myself clear. Thanks for reading.
答案1
得分: 0
获取墙上时钟时间使用System.currentTimeInMillis
。
System.nanoTime
可能是相当任意的,应该用于测量同一节点/机器上操作的持续时间。如果您尝试使用它来测量两台不同计算机之间的时间差,您将得到任意的结果。
在某些情况下,System.curentTimeInMillis可能会倒退,但我猜您更可能观察到由于这些机器上的系统时钟不完全同步而引起的问题 - 这在一般情况下是具有挑战性的,还取决于您通常希望测量什么时间间隔(微秒、毫秒?)。
查看NTP协议以及如何保持时钟同步,并且要对您获得的结果持保留态度。
关于这个主题的一些很好的信息在这里:https://codeburst.io/why-shouldnt-you-trust-system-clocks-72a82a41df93 - 一个有趣的观点:
> 根据谷歌的说法,一个每30秒同步一次的时钟会有6毫秒的漂移
与该主题相关的一篇有趣的论文:单向延迟测量的时钟同步:一项调查
英文:
For getting the wall-clock time use System.currentTimeInMillis
.
System.nanoTime
can be pretty much arbitrary and should be used for measuring durations of an operation on the same node/machine. You will get arbitrary results if you try to use it to measure the time difference between two different computers.
In some cases, System.curentTimeInMillis may go backward but I guess you're more likely to observe issues caused by system clocks on those machines being not perfectly synchronized - this is in general challenging and also depends on what time intervals you generally expect to measure (microseconds, milliseconds?).
Check out NTP protocol and how to keep clocks synchronized and take the results you get with a grain of salt.
Here's some good info about this topic: https://codeburst.io/why-shouldnt-you-trust-system-clocks-72a82a41df93 - an interesting piece:
> According to google, there is a 6ms drift in a clock which is synchronised every 30s
An interesting paper relevant to the topic: Clock Synchronization for One-Way Delay Measurement: A Survey
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论