1
0
mirror of https://github.com/golang/go synced 2024-11-24 08:40:14 -07:00

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 <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2021-11-01 14:07:30 -07:00
parent 79c5240799
commit 0b37bd9b3f

View File

@ -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)
}
}
}