From 2c23f1aedc51c9f816349eb4a24439dbf24484ea Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 9 Nov 2024 14:51:21 -0800 Subject: [PATCH] derp: add a unique.Make-vs-local map benchmark goos: darwin goarch: arm64 pkg: tailscale.com/derp cpu: Apple M1 BenchmarkUnique-8 139699720 10.59 ns/op BenchmarkUnique-8 138409840 8.619 ns/op BenchmarkUnique-8 134697708 8.521 ns/op BenchmarkUnique-8 136568799 8.653 ns/op BenchmarkUnique-8 134478981 8.647 ns/op BenchmarkLocalMap-8 675015452 1.643 ns/op BenchmarkLocalMap-8 717245598 1.648 ns/op BenchmarkLocalMap-8 697626253 1.657 ns/op BenchmarkLocalMap-8 729024962 1.670 ns/op BenchmarkLocalMap-8 712870580 1.668 ns/op PASS ok tailscale.com/derp 19.038s Updates tailscale/corp#24485 Change-Id: Ie8008b07c8c4625cf2b83e38eff169e2248b2d05 Signed-off-by: Brad Fitzpatrick --- derp/derp_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/derp/derp_test.go b/derp/derp_test.go index 9185194dd..436dd6d2e 100644 --- a/derp/derp_test.go +++ b/derp/derp_test.go @@ -22,6 +22,7 @@ import ( "sync" "testing" "time" + "unique" "go4.org/mem" "golang.org/x/time/rate" @@ -1598,3 +1599,40 @@ func TestServerRepliesToPing(t *testing.T) { } } } + +func BenchmarkUnique(b *testing.B) { + var key [32]byte + for i := range key { + key[i] = byte(i) + } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + h := unique.Make(key) + if h.Value() != key { + b.Fatal("unexpected") + } + } + }) +} + +func BenchmarkLocalMap(b *testing.B) { + var key [32]byte + for i := range key { + key[i] = byte(i) + } + m := map[[32]byte]bool{ + key: true, + } + k2 := key + for i := range k2 { + k2[0] = byte(i + 1) + m[k2] = false + } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + if !m[key] { + b.Fatal("unexpected") + } + } + }) +}