From 0b37bd9b3f7556d31061c6007afca59ffe2642d3 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Mon, 1 Nov 2021 14:07:30 -0700 Subject: [PATCH] net: add BenchmarkWriteToReadFromUDPAddrPort The main item of interest is the allocs. BenchmarkWriteToReadFromUDPAddrPort-8 237735 5152 ns/op 68 B/op 3 allocs/op Change-Id: I5cb3f2e1ebbb02acc51039c2266e99b90d388a99 Reviewed-on: https://go-review.googlesource.com/c/go/+/360598 Trust: Josh Bleecher Snyder Run-TryBot: Josh Bleecher Snyder TryBot-Result: Go Bot Reviewed-by: Brad Fitzpatrick --- src/net/udpsock_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/net/udpsock_test.go b/src/net/udpsock_test.go index 74e449c8881..371d9af5116 100644 --- a/src/net/udpsock_test.go +++ b/src/net/udpsock_test.go @@ -479,6 +479,7 @@ func BenchmarkReadWriteMsgUDPAddrPort(b *testing.B) { if err != nil { b.Fatal(err) } + defer conn.Close() addr := conn.LocalAddr().(*UDPAddr).AddrPort() buf := make([]byte, 8) b.ResetTimer() @@ -500,6 +501,7 @@ func BenchmarkWriteToReadFromUDP(b *testing.B) { if err != nil { b.Fatal(err) } + defer conn.Close() addr := conn.LocalAddr() buf := make([]byte, 8) b.ResetTimer() @@ -515,3 +517,25 @@ func BenchmarkWriteToReadFromUDP(b *testing.B) { } } } + +func BenchmarkWriteToReadFromUDPAddrPort(b *testing.B) { + conn, err := ListenUDP("udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)}) + if err != nil { + b.Fatal(err) + } + defer conn.Close() + addr := conn.LocalAddr().(*UDPAddr).AddrPort() + buf := make([]byte, 8) + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _, err := conn.WriteToUDPAddrPort(buf, addr) + if err != nil { + b.Fatal(err) + } + _, _, err = conn.ReadFromUDP(buf) // TODO: create and use ReadFromUDPAddrPort + if err != nil { + b.Fatal(err) + } + } +}