diff options
author | Josh Bleecher Snyder <josh@tailscale.com> | 2021-01-05 18:14:59 -0800 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-01-07 14:49:44 +0100 |
commit | 85b495057977e12df378778366a03b508c43f00f (patch) | |
tree | 2dc1d13576017ed7ff12b6ae6711851f1b7772d1 | |
parent | 8a304155554a16285b5334ae4a02e09adfb0bde1 (diff) |
device: add latency and throughput benchmarks
These obviously don't perfectly capture real world performance,
in which syscalls and network links have a significant impact.
Nevertheless, they capture some of the internal performance factors,
and they're easy and convenient to work with.
Hat tip to Avery Pennarun for help designing the throughput benchmark.
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
-rw-r--r-- | device/device_test.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/device/device_test.go b/device/device_test.go index 09b5152..62dee56 100644 --- a/device/device_test.go +++ b/device/device_test.go @@ -12,6 +12,7 @@ import ( "io" "net" "sync" + "sync/atomic" "testing" "time" @@ -281,3 +282,61 @@ func randDevice(t *testing.T) *Device { device.SetPrivateKey(sk) return device } + +func BenchmarkLatency(b *testing.B) { + pair := genTestPair(b) + + // Establish a connection. + pair.Send(b, Ping, nil) + pair.Send(b, Pong, nil) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + pair.Send(b, Ping, nil) + pair.Send(b, Pong, nil) + } +} + +func BenchmarkThroughput(b *testing.B) { + pair := genTestPair(b) + + // Establish a connection. + pair.Send(b, Ping, nil) + pair.Send(b, Pong, nil) + + // Measure how long it takes to receive b.N packets, + // starting when we receive the first packet. + var recv uint64 + var elapsed time.Duration + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + var start time.Time + for { + <-pair[0].tun.Inbound + new := atomic.AddUint64(&recv, 1) + if new == 1 { + start = time.Now() + } + // Careful! Don't change this to else if; b.N can be equal to 1. + if new == uint64(b.N) { + elapsed = time.Since(start) + return + } + } + }() + + // Send packets as fast as we can until we've received enough. + ping := tuntest.Ping(pair[0].ip, pair[1].ip) + pingc := pair[1].tun.Outbound + var sent uint64 + for atomic.LoadUint64(&recv) != uint64(b.N) { + sent++ + pingc <- ping + } + wg.Wait() + + b.ReportMetric(float64(elapsed)/float64(b.N), "ns/op") + b.ReportMetric(1-float64(b.N)/float64(sent), "packet-loss") +} |