因此,我决定非常快速地创建一个简单的基准测试,在没有任何线程或糟糕的编程技术的情况下发送200000个UDP数据包。基准测试介于Go和Python之间。此外,我希望基准测试尽可能快地进行协调。我曾使用C语言进行协调,但由于我使用的是Windows,所以似乎最容易使用Rust

防锈代码:

use std::time::{SystemTime};
use std::net::UdpSocket;

const DATA: &[u8] = &[0; 1024];

fn main() {
    let time_start = SystemTime::now();

    let socket = UdpSocket::bind("192.168.1.155:1339").unwrap();
    for _ in 0..200000 {
        socket.send_to(DATA, "192.168.1.122:80").unwrap();
    }
    let elapsed = time_start.elapsed().unwrap();
    println!("Time {} ms to send 200 000 packets", elapsed.as_millis());
}
Time 1863 ms to send 200 000 packets

对于Go基准测试,这基本上就是代码:

var buff = []byte("")

func main() {
    buff = []byte(RandomString(1024))

    conn, err := net.ListenUDP("udp", &net.UDPAddr{})
    if err != nil {
        log.Fatal("Listen:", err)
    }

    loopSendHello(*conn)
    conn.Close()
}

func loopSendHello(conn net.UDPConn) {
    timeStart := time.Now()
    for i:=0; i < 200000; i++ {
        sendHelloEdited(&conn)
    }
    timeStop := time.Now()
    timeDelta := timeStop.Sub(timeStart)
    fmt.Println("Took", timeDelta.Milliseconds(), "ms to send 200 000 packets.")
}

// targetAddr is target address in form of net.UDPAddr
func sendHelloEdited(conn *net.UDPConn) {
    _, err := conn.WriteToUDP(buff, &targetAddr)
    if err != nil {
        log.Fatal("Error while writing to conn! :", err)
    }
}
Took 1959 MS to send 200 000 packets.

现在进行Python测试,代码如下:

def main():
    x = 0
    data = random._urandom(1024)
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    addr = (str(ip), int(port))
    startTime = datetime.datetime.now()
    while x < 200000:
        s.sendto(data, addr)
        x += 1
    s.close()
    endTime = datetime.datetime.now()
    deltaTime = endTime - startTime
    print("Took", deltaTime, "to send 200 000 packets")

main()
Took 0:00:01.831531 to send 200 000 packets

因此,围棋似乎确实是最慢的,但只是略微缓慢