Commit e1ae248e authored by Mikio Hara's avatar Mikio Hara

ipv4: simplify tests and benchmarks

Change-Id: I588bc65b0052efb12b3b7bc2267deedcdad029ce
Reviewed-on: https://go-review.googlesource.com/47371
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarMatt Layher <mdlayher@gmail.com>
parent f5bfe5c2
...@@ -16,77 +16,47 @@ import ( ...@@ -16,77 +16,47 @@ import (
"golang.org/x/net/ipv4" "golang.org/x/net/ipv4"
) )
func benchmarkUDPListener() (net.PacketConn, net.Addr, error) { func BenchmarkReadWriteUnicast(b *testing.B) {
c, err := net.ListenPacket("udp4", "127.0.0.1:0") c, err := nettest.NewLocalPacketListener("udp4")
if err != nil { if err != nil {
return nil, nil, err b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, 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.Fatal(err)
} }
defer c.Close() defer c.Close()
dst := c.LocalAddr()
wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128) 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) { b.Run("NetUDP", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := c.WriteTo(wb, dst); err != nil { if _, err := c.WriteTo(wb, dst); err != nil {
b.Fatal(err) b.Fatal(err)
} }
if _, _, err := c.ReadFrom(rb); err != nil { if _, _, err := c.ReadFrom(rb); err != nil {
b.Fatal(err) b.Fatal(err)
} }
}
func BenchmarkReadWriteIPv4UDP(b *testing.B) {
c, dst, err := benchmarkUDPListener()
if err != nil {
b.Fatal(err)
} }
defer c.Close() })
b.Run("IPv4UDP", func(b *testing.B) {
p := ipv4.NewPacketConn(c) p := ipv4.NewPacketConn(c)
defer p.Close()
cf := ipv4.FlagTTL | ipv4.FlagInterface cf := ipv4.FlagTTL | ipv4.FlagInterface
if err := p.SetControlMessage(cf, true); err != nil { if err := p.SetControlMessage(cf, true); err != nil {
b.Fatal(err) b.Fatal(err)
} }
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
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} cm := ipv4.ControlMessage{TTL: 1}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi != nil { if ifi != nil {
cm.IfIndex = ifi.Index cm.IfIndex = ifi.Index
} }
if n, err := p.WriteTo(wb, &cm, dst); err != nil {
for i := 0; i < b.N; i++ {
if _, err := p.WriteTo(wb, &cm, dst); err != nil {
b.Fatal(err) b.Fatal(err)
} else if n != len(wb) {
b.Fatalf("got %v; want %v", n, len(wb))
} }
if _, _, _, err := p.ReadFrom(rb); err != nil { if _, _, _, err := p.ReadFrom(rb); err != nil {
b.Fatal(err) b.Fatal(err)
} }
}
})
} }
func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) { func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
...@@ -95,7 +65,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) { ...@@ -95,7 +65,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS) t.Skipf("not supported on %s", runtime.GOOS)
} }
c, err := net.ListenPacket("udp4", "127.0.0.1:0") c, err := nettest.NewLocalPacketListener("udp4")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -103,11 +73,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) { ...@@ -103,11 +73,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
p := ipv4.NewPacketConn(c) p := ipv4.NewPacketConn(c)
defer p.Close() defer p.Close()
dst, err := net.ResolveUDPAddr("udp4", c.LocalAddr().String()) dst := c.LocalAddr()
if err != nil {
t.Fatal(err)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback) ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
cf := ipv4.FlagTTL | ipv4.FlagSrc | ipv4.FlagDst | ipv4.FlagInterface cf := ipv4.FlagTTL | ipv4.FlagSrc | ipv4.FlagDst | ipv4.FlagInterface
wb := []byte("HELLO-R-U-THERE") wb := []byte("HELLO-R-U-THERE")
...@@ -152,7 +118,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) { ...@@ -152,7 +118,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
t.Error(err) t.Error(err)
return return
} else if n != len(wb) { } else if n != len(wb) {
t.Errorf("short write: %v", n) t.Errorf("got %d; want %d", n, len(wb))
return return
} }
} }
......
...@@ -28,18 +28,15 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) { ...@@ -28,18 +28,15 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
t.Skipf("not available on %s", runtime.GOOS) t.Skipf("not available on %s", runtime.GOOS)
} }
c, err := net.ListenPacket("udp4", "127.0.0.1:0") c, err := nettest.NewLocalPacketListener("udp4")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer c.Close() defer c.Close()
dst, err := net.ResolveUDPAddr("udp4", c.LocalAddr().String())
if err != nil {
t.Fatal(err)
}
p := ipv4.NewPacketConn(c) p := ipv4.NewPacketConn(c)
defer p.Close() defer p.Close()
dst := c.LocalAddr()
cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
wb := []byte("HELLO-R-U-THERE") wb := []byte("HELLO-R-U-THERE")
......
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