简体   繁体   English

测量网络上的经过时间

[英]Measuring elapsed time over network

I have developed a server and client application for streaming video frames from one end to another using RTSP. 我开发了一个服务器和客户端应用程序,用于使用RTSP将视频帧从一端流式传输到另一端。 Now, in order to gather statistics which will assist me in improving my applications, I need to measure the elapsed time between sending the frame and receiving the frame. 现在,为了收集有助于我改进应用程序的统计数据,我需要测量发送帧和接收帧之间经过的时间。

At the moment I am using the following formula: 目前我正在使用以下公式:

Client_Receive_Timestamp - Server_Send_Timestamp = Elapsed_Time

Problem 问题

It seems to me that the elapsed time is about 100-200ms too high. 在我看来,经过的时间大约是100-200ms。 I think the reason is that the server clock and client clock are not in sync and have a difference of about 100-200ms. 我认为原因是服务器时钟和客户端时钟不同步,差异大约为100-200ms。

Question

How can I accurately measure the elapsed time between the two mashines? 如何准确测量两个mashines之间的经过时间?

The topic Accurately measuring elapsed time between machines suggests calculating a round-trip delay. 主题准确测量机器之间的经过时间建议计算往返延迟。 However, I can't use this solution as the client doesn't request the frames. 但是,我不能使用此解决方案,因为客户端不请求帧。 It simply receives frames via RTSP. 它只是通过RTSP接收帧。

Assuming 假设

then you can simply subtract the "sent timestamp" from the "received timestamp" to obtain the latency duration. 然后您可以简单地从“接收的时间戳”中减去“发送的时间戳”以获得延迟持续时间。 The observed error will be less than the sum of both clock errors. 观察到的误差将小于两个时钟误差的总和。 If the time scales are small enough (probably anything smaller than an hour) you can reasonably ignore slew effects. 如果时间尺度足够小(可能小于一小时),你可以合理地忽略转换效果。

If ntpd is not already running on both machines, and if you have the necessary permissions, then you can 如果ntpd尚未在两台计算机上运行, 并且您具有必要的权限,则可以

$ sudo ntpdate -v pool.ntp.org

to force a synchronization with the pool of publicly-available time servers. 强制与公共可用时间服务器池同步。

Then you can use the c++11 high_resolution_clock to calculate the duration: 然后你可以使用c ++ 11 high_resolution_clock来计算持续时间:

/* hrc.cc */
#include <chrono>
#include <iostream>

int main(int,char**){
  using std::chrono::high_resolution_clock;
  // send something                                                                                                                      
  high_resolution_clock::time_point start = high_resolution_clock::now();
  std::cout << "time this" << std::endl ;
  // receive something                                                                                                                   
  high_resolution_clock::time_point stop = high_resolution_clock::now();
  std::cout
    << "duration == "
    << std::chrono::duration_cast<std::chrono::nanoseconds>(stop-start).count()
    << "ns"
    << std::endl
    ;
  return 0;
}

Here's what the previous example looks like on my system: 这是上一个示例在我的系统上的样子:

$ make hrc && ./hrc
c++     hrc.cc   -o hrc
time this
duration == 32010ns

I need to measure the elapsed time between sending the frame and receiving the frame. 我需要测量发送帧和接收帧之间经过的时间。

You don't need precise timestamps for this. 您不需要精确的时间戳。 You can average the estimated latency. 您可以平均估计的延迟。

If A sends the packet (or a frame) to B, B responds immediately (*) : 如果A将数据包(或帧)发送给B,则B立即响应(*)

A (sendTime) ---> B ---> A (receivedTime) A (sendTime) ---> B ---> A (receivedTime)

then you can calculate the latency easily: 然后你可以轻松地计算延迟:

latency = (receivedTime - sendTime) / 2

This assumes of course that the latency is symmetrical. 这当然假设延迟是对称的。 You can find more elaborate algorithms if you research "network latency estimation algorithm" phrases. 如果您研究“网络延迟估计算法”短语,您可以找到更复杂的算法。

Having the estimated latency you can of course estimate time difference (but it doesn't seem necessary): 有了估计的延迟,你当然可以估计时差(但似乎没有必要):

A (sendTime) ---> B (receivedTimeB) -- (receivedTimeB) --> A A (sendTime) ---> B (receivedTimeB) - (receivedTimeB) - > A.

timeDelta = sendTime + latency - receivedTimeB

Note that even if you average many results, this algorithm is probably highly biased. 请注意,即使您平均很多结果,此算法也可能具有高度偏差。 This is just posted as a simple example to the general idea. 这只是作为一般概念的简单示例发布的。


(*) The fact that it does not happen really immediately induces an error of course. (*)它不会立即发生的事实当然会导致错误。 This depends on how heavily machine B is loaded. 这取决于机器B的装载量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM