如何在 Linux 中测量 TCP 连接时间

如何在 Linux 中测量 TCP 连接时间

我想测量创建 TCP 连接的开销。

我知道很多像hping和这样的工具netperf,但它们似乎侧重于测量延迟。

我想知道三次握手需要多长时间,分配缓冲区等,然后关闭它。所以我想打开一个真实、合法的 TCP 连接,然后关闭它。

是否有任何工具可以做到这一点并帮助我衡量绩效?

答案1

我认为您要问的是如何测量 TCP 连接上从第一个 SYN 数据包到第一个非零有效负载数据包所需的时间?

在这方面,Tcpdump 是一个很有用的工具,而 Theo Schlossnagle 则书面如何实现这一点。我已经使用了他描述的技术,效果很好。

答案2

对于简单的情况,curl使用该-w选项可以打印这些操作所花费的时间。

Curl 只会打印从一开始的累计时间,因此您必须自己减去这些数字。由于 tcp 连接仅在 dns 查找后启动(假设没有重定向),因此您必须计算time_connect - time_namelookup。我不确定 curl 在进行以下重定向时如何准确测量时间。请注意,这也适用于非 Web 服务,因为 curl 只会在建立 tcp 连接后才发现它不是 http。

示例命令:

$ curl -s -o /dev/null -w "dns:%{time_namelookup}, redir:%{time_redirect}, tcp:{time_connect}\n" poohl.de
dns:0.046345, redir:0.000000, tcp:0.067986

所以建立 tcp 连接的时间是0.067986s - 0.046345s = 0.021641s

来自手册页:

time_appconnect
    The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed.
time_connect
    The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
time_namelookup
    The time, in seconds, it took from the start until the name resolving was completed.
time_pretransfer
    The  time,  in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.
time_redirect
    The time, in seconds, it took for all redirection steps including name lookup, connect, pretransfer and transfer before the final transaction was  started. time_redirect shows the complete execution time for multiple redirections.
time_starttransfer
    The  time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.
time_total
    The total time, in seconds, that the full operation 

相关内容