wgengine/magicsock: add start of magicsock benchmarks (Conn.ReceiveIPv4 for now)

And only single-threaded for now. Will get fancier later.

Updates #414
josh/no-goroutine-per-udp-read-2
Brad Fitzpatrick 4 years ago
parent 713cbe84c1
commit 7b92f8e718

@ -56,7 +56,7 @@ func init() {
// conditions in tests. In particular, you can't expect two test magicsocks // conditions in tests. In particular, you can't expect two test magicsocks
// to be able to connect to each other through a test DERP unless they are // to be able to connect to each other through a test DERP unless they are
// both fully initialized before you try. // both fully initialized before you try.
func (c *Conn) WaitReady(t *testing.T) { func (c *Conn) WaitReady(t testing.TB) {
t.Helper() t.Helper()
timer := time.NewTimer(10 * time.Second) timer := time.NewTimer(10 * time.Second)
defer timer.Stop() defer timer.Stop()
@ -130,7 +130,7 @@ type magicStack struct {
// newMagicStack builds and initializes an idle magicsock and // newMagicStack builds and initializes an idle magicsock and
// friends. You need to call conn.SetNetworkMap and dev.Reconfig // friends. You need to call conn.SetNetworkMap and dev.Reconfig
// before anything interesting happens. // before anything interesting happens.
func newMagicStack(t *testing.T, logf logger.Logf, l nettype.PacketListener, derpMap *tailcfg.DERPMap) *magicStack { func newMagicStack(t testing.TB, logf logger.Logf, l nettype.PacketListener, derpMap *tailcfg.DERPMap) *magicStack {
t.Helper() t.Helper()
privateKey, err := wgcfg.NewPrivateKey() privateKey, err := wgcfg.NewPrivateKey()
@ -377,7 +377,7 @@ collectEndpoints:
} }
} }
func pickPort(t *testing.T) uint16 { func pickPort(t testing.TB) uint16 {
t.Helper() t.Helper()
conn, err := net.ListenPacket("udp4", "127.0.0.1:0") conn, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil { if err != nil {
@ -1344,3 +1344,75 @@ func TestDiscoEndpointAlignment(t *testing.T) {
t.Error("expected false on second call") t.Error("expected false on second call")
} }
} }
func BenchmarkReceiveFrom(b *testing.B) {
port := pickPort(b)
conn, err := NewConn(Options{
Logf: b.Logf,
Port: port,
EndpointsFunc: func(eps []string) {
b.Logf("endpoints: %q", eps)
},
})
if err != nil {
b.Fatal(err)
}
defer conn.Close()
sendConn, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil {
b.Fatal(err)
}
defer sendConn.Close()
var dstAddr net.Addr = conn.pconn4.LocalAddr()
sendBuf := make([]byte, 1<<10)
for i := range sendBuf {
sendBuf[i] = 'x'
}
buf := make([]byte, 2<<10)
for i := 0; i < b.N; i++ {
if _, err := sendConn.WriteTo(sendBuf, dstAddr); err != nil {
b.Fatalf("WriteTo: %v", err)
}
n, ep, addr, err := conn.ReceiveIPv4(buf)
if err != nil {
b.Fatal(err)
}
_ = n
_ = ep
_ = addr
}
}
func BenchmarkReceiveFrom_Native(b *testing.B) {
recvConn, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil {
b.Fatal(err)
}
defer recvConn.Close()
recvConnUDP := recvConn.(*net.UDPConn)
sendConn, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil {
b.Fatal(err)
}
defer sendConn.Close()
var dstAddr net.Addr = recvConn.LocalAddr()
sendBuf := make([]byte, 1<<10)
for i := range sendBuf {
sendBuf[i] = 'x'
}
buf := make([]byte, 2<<10)
for i := 0; i < b.N; i++ {
if _, err := sendConn.WriteTo(sendBuf, dstAddr); err != nil {
b.Fatalf("WriteTo: %v", err)
}
if _, _, err := recvConnUDP.ReadFromUDP(buf); err != nil {
b.Fatalf("ReadFromUDP: %v", err)
}
}
}

Loading…
Cancel
Save