Commit 570fa1c9 authored by Mikio Hara's avatar Mikio Hara

ipv6: simplify tests and benchmarks

Change-Id: I248e5e5215960bceaba357d3d4a6e72191f955b1
Reviewed-on: https://go-review.googlesource.com/47372
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarMatt Layher <mdlayher@gmail.com>
parent e1ae248e
......@@ -17,87 +17,50 @@ import (
"golang.org/x/net/ipv6"
)
func benchmarkUDPListener() (net.PacketConn, net.Addr, error) {
c, err := net.ListenPacket("udp6", "[::1]:0")
func BenchmarkReadWriteUnicast(b *testing.B) {
c, err := nettest.NewLocalPacketListener("udp6")
if err != nil {
return nil, nil, err
}
dst, err := net.ResolveUDPAddr("udp6", c.LocalAddr().String())
if err != nil {
c.Close()
return nil, nil, err
}
return c, dst, nil
}
func BenchmarkReadWriteNetUDP(b *testing.B) {
if !supportsIPv6 {
b.Skip("ipv6 is not supported")
}
c, dst, err := benchmarkUDPListener()
if err != nil {
b.Fatal(err)
b.Skipf("not supported on %s/%s: %v", runtime.GOOS, runtime.GOARCH, err)
}
defer c.Close()
dst := c.LocalAddr()
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 {
b.Fatal(err)
}
if _, _, err := c.ReadFrom(rb); err != nil {
b.Fatal(err)
}
}
func BenchmarkReadWriteIPv6UDP(b *testing.B) {
if !supportsIPv6 {
b.Skip("ipv6 is not supported")
}
c, dst, err := benchmarkUDPListener()
if err != nil {
b.Fatal(err)
}
defer c.Close()
})
b.Run("IPv6UDP", func(b *testing.B) {
p := ipv6.NewPacketConn(c)
cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
if err := p.SetControlMessage(cf, true); err != nil {
b.Fatal(err)
}
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128)
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkReadWriteIPv6UDP(b, p, wb, rb, dst, ifi)
}
}
func benchmarkReadWriteIPv6UDP(b *testing.B, p *ipv6.PacketConn, wb, rb []byte, dst net.Addr, ifi *net.Interface) {
cm := ipv6.ControlMessage{
TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
HopLimit: 1,
}
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
if ifi != nil {
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)
} else if n != len(wb) {
b.Fatalf("got %v; want %v", n, len(wb))
}
if _, _, _, err := p.ReadFrom(rb); err != nil {
b.Fatal(err)
}
}
})
}
func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
......@@ -109,7 +72,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
t.Skip("ipv6 is not supported")
}
c, err := net.ListenPacket("udp6", "[::1]:0")
c, err := nettest.NewLocalPacketListener("udp6")
if err != nil {
t.Fatal(err)
}
......@@ -117,11 +80,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
p := ipv6.NewPacketConn(c)
defer p.Close()
dst, err := net.ResolveUDPAddr("udp6", c.LocalAddr().String())
if err != nil {
t.Fatal(err)
}
dst := c.LocalAddr()
ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
wb := []byte("HELLO-R-U-THERE")
......@@ -167,7 +126,7 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
t.Error(err)
return
} else if n != len(wb) {
t.Errorf("got %v; want %v", n, len(wb))
t.Errorf("got %d; want %d", n, len(wb))
return
}
}
......
......@@ -27,7 +27,7 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
t.Skip("ipv6 is not supported")
}
c, err := net.ListenPacket("udp6", "[::1]:0")
c, err := nettest.NewLocalPacketListener("udp6")
if err != nil {
t.Fatal(err)
}
......@@ -35,11 +35,7 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
p := ipv6.NewPacketConn(c)
defer p.Close()
dst, err := net.ResolveUDPAddr("udp6", c.LocalAddr().String())
if err != nil {
t.Fatal(err)
}
dst := c.LocalAddr()
cm := ipv6.ControlMessage{
TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
Src: net.IPv6loopback,
......@@ -54,7 +50,8 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
for i, toggle := range []bool{true, false, true} {
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
t.Skipf("not supported on %s", runtime.GOOS)
t.Logf("not supported on %s", runtime.GOOS)
continue
}
t.Fatal(err)
}
......@@ -151,7 +148,8 @@ func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
}
if err := p.SetControlMessage(cf, toggle); err != nil {
if nettest.ProtocolNotSupported(err) {
t.Skipf("not supported on %s", runtime.GOOS)
t.Logf("not supported on %s", runtime.GOOS)
continue
}
t.Fatal(err)
}
......
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