TCP 与 UDP 套接字延迟基准测试 [英] TCP vs. UDP socket latency benchmark

查看:42
本文介绍了TCP 与 UDP 套接字延迟基准测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中通过 TCP 和 UDP 实现了一个小型的套接字通信基准测试.令人惊讶的是,TCP 的速度几乎是 UDP 的两倍.

I have implemented a small benchmark for socket communication via TCP and UDP in Python. Surprisingly, TCP is almost exactly double as fast as UDP.

为了避免路由效应,服务器和客户端运行在同一台 Unix 机器上,但在不同的线程上.

To avoid routing effects, server and client are running on the same Unix machine, but on different threads.

也许代码有用.这是服务器代码:

Maybe the code is useful. Here is the server code:

import socket
import sys

host = 'localhost'  
port = 8888
buffersize = 8
server_address = (host, port) 

def start_UDP_server():
    socket_UDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    socket_UDP.bind(server_address)

    print("UDP server is running...")

    while True:
        data, from_address = socket_UDP.recvfrom(buffersize)
        if not data: break
        socket_UDP.sendto(data, from_address)
    socket_UDP.close()


def start_TCP_server():
    socket_TCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_TCP.bind(server_address)
    socket_TCP.listen(1)

    print("TCP server is running...")

    while True:    
        client, client_address = socket_TCP.accept()

        while True:
            data = client.recv(buffersize)
            if not data: break
            client.sendall(data)

        client.close()

因此您可以运行 start_TCP_server()start_UDP_server().

So you can run either start_TCP_server() or start_UDP_server().

在客户端的代码是:

import socket
import sys
import time

host = 'localhost'  
port = 8888
buffersize = 8
server_address = (host, port) 
client_address = (host, port+1)
N = 1000000


def benchmark_UDP():
    socket_UDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    socket_UDP.bind(client_address)

    print("Benchmark UDP...")

    duration = 0.0
    for i in range(0, N):
        b = bytes("a"*buffersize, "utf-8")
        start = time.time()
        socket_UDP.sendto(b, server_address)
        data, from_address = socket_UDP.recvfrom(buffersize)
        duration += time.time() - start

        if data != b:
            print("Error: Sent and received data are bot the same")

    print(duration*pow(10, 6)/N, "µs for UDP") 


def benchmark_TCP():
    socket_TCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_TCP.connect(server_address)

    print("Benchmark TCP...")

    duration = 0.0
    for i in range(0, N):
        b = bytes("a"*buffersize, "utf-8")
        start = time.time()
        socket_TCP.sendall(b)
        data = socket_TCP.recv(buffersize)
        duration += time.time() - start

        if data != b:
            print("Error: Sent and received data are bot the same")

    print(duration*pow(10, 6)/N, "µs for TCP")
    socket_TCP.close()

与服务器一样,您可以通过 benchmark_TCP()benchmark_UDP() 启动基准测试.

Like for the server you can start the benchmark by benchmark_TCP() or benchmark_UDP().

在 Unix 上,结果大约 TCP 为 25 µs,UDP 为大约 54 µs,对于 Windows 甚至更糟(TCP 大约为 30 µs,超过 200 µsUDP).为什么?我希望 UDP 的优势很小.

The results are about 25 µs for TCP, and about 54 µs for UDP on Unix and even worse for Windows (about 30 µs for TCP and more than 200 µs for UDP). Why? I would expect a minimal advantage for UDP.

推荐答案

您的 TCP 套接字已连接,但您的 UDP 套接字未连接.这意味着对 UDP 套接字上的每个发送/接收进行额外的处理.在上调用connectUDP 套接字的每一端,就像您在 TCP 套接字上调用 connect/accept 一样.

Your TCP socket is connected but your UDP socket is not. This means extra processing for every send/receive on the UDP socket. Call connect on each side for the UDP socket, just like you call connect/accept on the TCP socket.

iperf 这样的程序这样做是为了准确测量.

Programs like iperf do this to measure accurately.

这篇关于TCP 与 UDP 套接字延迟基准测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆