Commit b073adc0 authored by Mikio Hara's avatar Mikio Hara

go.net/ipv4: add datagram benchmark

Current result on darwin/amd64:
BenchmarkReadWriteNetUDP-2	  100000	     16112 ns/op	     330 B/op	       6 allocs/op
BenchmarkReadWriteIPv4UDP-2	  200000	     14337 ns/op	     654 B/op	      14 allocs/op

Update golang/go#3684

R=dave
CC=golang-dev
https://golang.org/cl/10125044
parent 2a7369bc
...@@ -13,6 +13,76 @@ import ( ...@@ -13,6 +13,76 @@ import (
"testing" "testing"
) )
func benchmarkUDPListener() (net.PacketConn, net.Addr, error) {
c, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil {
return nil, nil, err
}
dst, err := net.ResolveUDPAddr("udp4", c.LocalAddr().String())
if err != nil {
c.Close()
return nil, nil, err
}
return c, dst, nil
}
func BenchmarkReadWriteNetUDP(b *testing.B) {
c, dst, err := benchmarkUDPListener()
if err != nil {
b.Fatalf("benchmarkUDPListener failed: %v", err)
}
defer c.Close()
wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128)
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkReadWriteNetUDP(b, c, wb, rb, dst)
}
}
func benchmarkReadWriteNetUDP(b *testing.B, c net.PacketConn, wb, rb []byte, dst net.Addr) {
if _, err := c.WriteTo(wb, dst); err != nil {
b.Fatalf("net.PacketConn.WriteTo failed: %v", err)
}
if _, _, err := c.ReadFrom(rb); err != nil {
b.Fatalf("net.PacketConn.ReadFrom failed: %v", err)
}
}
func BenchmarkReadWriteIPv4UDP(b *testing.B) {
c, dst, err := benchmarkUDPListener()
if err != nil {
b.Fatalf("benchmarkUDPListener failed: %v", err)
}
defer c.Close()
p := ipv4.NewPacketConn(c)
cf := ipv4.FlagTTL | ipv4.FlagInterface
if err := p.SetControlMessage(cf, true); err != nil {
b.Fatalf("ipv4.PacketConn.SetControlMessage failed: %v", err)
}
ifi := loopbackInterface()
wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128)
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkReadWriteIPv4UDP(b, p, wb, rb, dst, ifi)
}
}
func benchmarkReadWriteIPv4UDP(b *testing.B, p *ipv4.PacketConn, wb, rb []byte, dst net.Addr, ifi *net.Interface) {
cm := ipv4.ControlMessage{TTL: 1}
if ifi != nil {
cm.IfIndex = ifi.Index
}
if _, err := p.WriteTo(wb, &cm, dst); err != nil {
b.Fatalf("ipv4.PacketConn.WriteTo failed: %v", err)
}
if _, _, _, err := p.ReadFrom(rb); err != nil {
b.Fatalf("ipv4.PacketConn.ReadFrom failed: %v", err)
}
}
func TestReadWriteUnicastIPPayloadUDP(t *testing.T) { func TestReadWriteUnicastIPPayloadUDP(t *testing.T) {
c, err := net.ListenPacket("udp4", "127.0.0.1:0") c, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil { if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment