简体   繁体   English

如何获得打开TCP连接的进程的“延迟”?

[英]How can I get the “Latency” of a process that has a TCP connection open?

I am looking to get the "Latency" field of a TCP connection. 我希望获得TCP连接的“Latency”字段。 I notice windows Resource Monitor has this field, and I was wondering if there was a way I can find it. 我注意到Windows资源监视器有这个字段,我想知道是否有一种方法可以找到它。 Preferrably without using WMI. 最好不使用WMI。

If you are unsure what field I am talking about, open Task Manager, goto the Performance tab and hit the Resource Monitor button. 如果您不确定我在说什么字段,请打开“任务管理器”,转到“性能”选项卡,然后单击“资源监视器”按钮。

Once Resource Monitor is open, expand the TCP Connections area and you will see a Latency field. 资源监视器打开后,展开“TCP连接”区域,您将看到“延迟”字段。 Is there anyway to access this programatically? 无论如何以编程方式访问它?

Thanks! 谢谢!

I'm assuming that the Resource monitor looks at the Round Trip Time (RTT) for a given TCP table entry. 我假设资源监视器查看给定TCP表条目的往返时间(RTT)。 This gives a reasonable indication of overall network delay. 这给出了整体网络延迟的合理指示。

There is an API you can use to access these statistics, namely GetPerTcpConnectionEStats . 您可以使用API​​来访问这些统计信息,即GetPerTcpConnectionEStats This allows you to retrieve plenty of stats about a specific TCP connection. 这允许您检索有关特定TCP连接的大量统计信息。

You basically get the list of tcp connections first using GetTcpTable , then find the row you want, and pass it to GetPerTcpConnectionEStats , with the TcpConnectionEstatsPath as the EstatsType parameter, so that you should get a TCP_ESTATS_PATH_ROD_v0 structure. 您基本上首先使用GetTcpTable获取tcp连接列表,然后找到所需的行,并将其传递给GetPerTcpConnectionEStats ,并将TcpConnectionEstatsPath作为EstatsType参数,以便获得TCP_ESTATS_PATH_ROD_v0结构。

This structure has a number of RTT stats in it, the most useful of which is probably the SumRtt and CountRtt members, which you could use to get an average RTT calculation for that particular TCP table row. 此结构中包含许多RTT统计信息,其中最有用的可能是SumRtt和CountRtt成员,您可以使用它们来获取该特定TCP表行的平均RTT计算。

Note that these functions only exist in Vista and above, but then so does the Resource Monitor, so I reckon that's ok. 请注意,这些功能仅存在于Vista及更高版本中,但资源监视器也是如此,所以我认为没问题。

I don't think you can access this information through any API. 我认为您无法通过任何API访问此信息。 The Resource Monitor likely calculates it by seeing how long it takes for packets to be replied to. 资源监视器可能通过查看数据包被回复所需的时间来计算它。

To do this in your app, do something like this (pseudo-code): 要在您的应用中执行此操作,请执行以下操作(伪代码):

startTime = now
socket = openSocket()
endTime = now
latency = endTime - startTime

It won't be extremely precise, but it should be pretty close to the actual network latency. 它不会非常精确,但它应该非常接近实际的网络延迟。 However, keep in mind that Nagle's algorithm can mess with latency calculations. 但是,请记住, Nagle的算法可能会影响延迟计算。

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

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