测试 VM 网络延迟Test VM network latency

若要获得最准确的结果,请使用为该任务设计的工具来测量 Azure 虚拟机 (VM) 网络延迟。To achieve the most accurate results, measure your Azure virtual machine (VM) network latency with a tool that's designed for the task. 市售的工具包括 SockPerf(适用于 Linux)和 latte.exe(适用于 Windows),它们可以隔离和测量网络延迟,同时排除其他类型的延迟,例如应用程序延迟。Publicly available tools such as SockPerf (for Linux) and latte.exe (for Windows) can isolate and measure network latency while excluding other types of latency, such as application latency. 这些工具重点测量影响应用程序性能的网络流量类型(即传输控制协议 [TCP] 和用户数据报协议 [UDP] 流量)。These tools focus on the kind of network traffic that affects application performance (namely, Transmission Control Protocol [TCP] and User Datagram Protocol [UDP] traffic).

其他常见的连接工具(例如 Ping)可以测量延迟,但其结果可能并不代表实际工作负荷中使用的网络流量。Other common connectivity tools, such as Ping, might measure latency, but their results might not represent the network traffic that's used in real workloads. 这是因为,其中的大多数工具使用 Internet 控制消息协议 (ICMP),而应用程序流量处理该协议的方式不同,因此其结果可能不适用于使用 TCP 和 UDP 的工作负荷。That's because most of these tools employ the Internet Control Message Protocol (ICMP), which can be treated differently from application traffic and whose results might not apply to workloads that use TCP and UDP.

在准确测试大多数应用程序所用协议的网络延迟方面,SockPerf(适用于 Linux)和 latte.exe(适用于 Windows)生成的结果最为相关。For accurate network latency testing of the protocols used by most applications, SockPerf (for Linux) and latte.exe (for Windows) produce the most relevant results. 本文将介绍这两个工具。This article covers both of these tools.


通过使用两个 VM(一个作为发送方,另一个作为接收方),可以创建一个双向信道。By using two VMs, one as sender and one as receiver, you create a two-way communications channel. 使用此方法,你可以双向发送和接收数据包,并测量往返时间 (RTT)。With this approach, you can send and receive packets in both directions and measure the round-trip time (RTT).

可以使用此方法测量两个 VM 甚至两台物理计算机之间的网络延迟。You can use this approach to measure network latency between two VMs or even between two physical computers. 延迟测量可用于以下方案:Latency measurements can be useful for the following scenarios:

  • 为部署的 VM 之间的网络延迟建立基准。Establish a benchmark for network latency between the deployed VMs.
  • 在进行下述相关更改后,比较网络延迟变化产生的效果:Compare the effects of changes in network latency after related changes are made to:
    • 操作系统 (OS) 或网络堆栈软件,包括配置更改。Operating system (OS) or network stack software, including configuration changes.

    • VM 部署方法,例如部署到邻近放置组 (PPG)。A VM deployment method, such as deploying to proximity placement group (PPG).

    • VM 属性,例如加速网络或大小更改。VM properties, such as Accelerated Networking or size changes.

    • 虚拟网络,例如路由或筛选更改。A virtual network, such as routing or filtering changes.

测试工具Tools for testing

要测量延迟,有两个不同的工具可供你选择:To measure latency, you have two different tool options:

使用这些工具有助于确保仅测量 TCP 或 UDP 有效负载传送时间,而不测量应用程序未使用的,且不影响应用程序性能的 ICMP (Ping) 或其他数据包类型。By using these tools, you help ensure that only TCP or UDP payload delivery times are measured and not ICMP (Ping) or other packet types that aren't used by applications and don't affect their performance.

有关创建最佳 VM 配置的技巧Tips for creating an optimal VM configuration

创建 VM 配置时,请记住以下建议:When you create your VM configuration, keep in mind the following recommendations:

  • 使用最新版本的 Windows 或 Linux。Use the latest version of Windows or Linux.
  • 启用加速网络以获得最佳结果。Enable Accelerated Networking for best results.
  • 使用 Azure 邻近放置组部署 VM。Deploy VMs with an Azure proximity placement group.
  • 大型 VM 的性能往往优于小型 VM。Larger VMs generally perform better than smaller VMs.

有关分析的提示Tips for analysis

在分析测试结果时,请记住以下建议:As you're analyzing test results, keep in mind the following recommendations:

  • 完成部署、配置和优化后尽早建立基线。Establish a baseline early, as soon as deployment, configuration, and optimizations are complete.
  • 始终将新结果与基线进行比较,或者在进行受控的更改后将每次测试结果进行比较。Always compare new results to a baseline or, otherwise, from one test to another with controlled changes.
  • 每当观测到或规划更改后重复测试。Repeat tests whenever changes are observed or planned.

测试运行 Windows 的 VMTest VMs that are running Windows

将 latte.exe 加载到 VMGet latte.exe onto the VMs

下载最新版本的 latte.exeDownload the latest version of latte.exe.

考虑将 latte.exe 放在单独的文件夹中,例如 c:\toolsConsider putting latte.exe in separate folder, such as c:\tools.

允许 latte.exe 通过 Windows Defender 防火墙Allow latte.exe through Windows Defender Firewall

在“接收方”的 Windows Defender 防火墙中创建“允许”规则,以允许 latte.exe 流量抵达。On the receiver, create an Allow rule on Windows Defender Firewall to allow the latte.exe traffic to arrive. 最简单的方法是按名称允许整个 latte.exe 程序,而不是允许特定的 TCP 端口入站。It's easiest to allow the entire latte.exe program by name rather than to allow specific TCP ports inbound.

通过运行以下命令,允许 latte.exe 通过 Windows Defender 防火墙:Allow latte.exe through Windows Defender Firewall by running the following command:

netsh advfirewall firewall add rule program=<path>\latte.exe name="Latte" protocol=any dir=in action=allow enable=yes profile=ANY

例如,如果已将 latte.exe 复制到 c:\tools 文件夹中,则此命令为:For example, if you copied latte.exe to the c:\tools folder, this would be the command:

netsh advfirewall firewall add rule program=c:\tools\latte.exe name="Latte" protocol=any dir=in action=allow enable=yes profile=ANY

运行延迟测试Run latency tests

  • 在“接收方”,启动 latte.exe(从 CMD 窗口而不是从 PowerShell 运行它):On the receiver, start latte.exe (run it from the CMD window, not from PowerShell):

    latte -a <Receiver IP address>:<port> -i <iterations>

    大约 65,000 次迭代就足以返回具有代表性的结果。Around 65,000 iterations is long enough to return representative results.

    可输入任意可用端口号。Any available port number is fine.

    如果 VM 的 IP 地址为,则命令如下:If the VM has an IP address of, the command would look like this:

    latte -a -i 65100

  • 在“发送方”,启动 latte.exe(从 CMD 窗口而不是从 PowerShell 运行它):On the sender, start latte.exe (run it from the CMD window, not from PowerShell):

    latte -c -a <Receiver IP address>:<port> -i <iterations>

    生成的命令与在接收方相同,只是添加了  -c 来指示这是“客户端”或“发送方”:The resulting command is the same as on the receiver, except with the addition of -c to indicate that this is the client, or sender:

    latte -c -a -i 65100

等待结果。Wait for the results. 测试可能需要几分钟时间才能完成,具体取决于 VM 之间的距离。Depending on how far apart the VMs are, the test could take a few minutes to finish. 考虑先运行较少的迭代次数以使测试成功,然后再运行较长的测试。Consider starting with fewer iterations to test for success before running longer tests.

测试运行 Linux 的 VMTest VMs that are running Linux

若要测试运行 Linux 的 VM,请使用 SockPerfTo test VMs that are running Linux, use SockPerf.

在 VM 上安装 SockPerfInstall SockPerf on the VMs

在 Linux VM 上(包括“发送方”和“接收方”),运行以下命令以在 VM 上准备 SockPerf。On the Linux VMs, both sender and receiver, run the following commands to prepare SockPerf on the VMs. 提供的命令适用于主要分发版。Commands are provided for the major distros.

对于 CentOSFor CentOS

运行以下命令:Run the following commands:

#CentOS - Install Git and other helpful tools
sudo yum install gcc -y -q
sudo yum install git -y -q
sudo yum install gcc-c++ -y
sudo yum install ncurses-devel -y
sudo yum install -y automake
sudo yum install -y autoconf

对于 UbuntuFor Ubuntu

运行以下命令:Run the following commands:

#Ubuntu - Install Git and other helpful tools
sudo apt-get install build-essential -y
sudo apt-get install git -y -q
sudo apt-get install -y autotools-dev
sudo apt-get install -y automake
sudo apt-get install -y autoconf

对于所有分发版For all distros

请按以下步骤复制、编译并安装 SockPerf:Copy, compile, and install SockPerf according to the following steps:

#Bash - all distros

#From bash command line (assumes Git is installed)
git clone
cd sockperf/
./configure --prefix=

#make is slower, may take several minutes

#make install is fast
sudo make install

在 VM 上运行 SockPerfRun SockPerf on the VMs

SockPerf 安装完成后,便可以在 VM 上运行延迟测试了。After the SockPerf installation is complete, the VMs are ready to run the latency tests.

首先,在“接收方”启动 SockPerf。First, start SockPerf on the receiver.

可输入任意可用端口号。Any available port number is fine. 在此示例中,我们使用端口 12345:In this example, we use port 12345:

#Server/Receiver - assumes server's IP is
sudo sockperf sr --tcp -i -p 12345

现在,服务器正在侦听,客户端可以通过服务器侦听的端口(在本例中为 12345)开始将数据包发送到服务器。Now that the server is listening, the client can begin sending packets to the server on the port on which it is listening (in this case, 12345).

等待大约 100 秒就足以返回有代表性的结果,如以下示例中所示:About 100 seconds is long enough to return representative results, as shown in the following example:

#Client/Sender - assumes server's IP is
sockperf ping-pong -i --tcp -m 350 -t 101 -p 12345  --full-rtt

等待结果。Wait for the results. 迭代次数根据 VM 的距离而异。Depending on how far apart the VMs are, the number of iterations will vary. 考虑先运行大约 5 秒的短时间测试以使测试成功,然后再运行较长时间的测试。To test for success before you run longer tests, consider starting with shorter tests of about 5 seconds.

此 SockPerf 示例使用 350 字节消息大小,这是平均数据包的典型大小。This SockPerf example uses a 350-byte message size, which is typical for an average packet. 你可以调大或调小消息大小,使结果能够更准确地代表 VM 上运行的工作负荷。You can adjust the size higher or lower to achieve results that more accurately represent the workload that's running on your VMs.

后续步骤Next steps