From 7b9a901489c24e0c15a8ca97554f7ee44033955a Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 25 Jun 2022 12:26:54 -0700 Subject: [PATCH] util/deephash: add packet filter benchmark (breaking up parts of another change) This adds a PacketFilter hashing benchmark with an input that both contains every possible field, but also is somewhat representative in the shape of what real packet filters contain. Signed-off-by: Brad Fitzpatrick --- util/deephash/deephash_test.go | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/util/deephash/deephash_test.go b/util/deephash/deephash_test.go index 42cf00f54..29c0b425e 100644 --- a/util/deephash/deephash_test.go +++ b/util/deephash/deephash_test.go @@ -324,6 +324,57 @@ func BenchmarkHash(b *testing.B) { } } +func ptrTo[T any](v T) *T { return &v } + +// filterRules is a packet filter that has both everything populated (in its +// first element) and also a few entries that are the typical shape for regular +// packet filters as sent to clients. +var filterRules = []tailcfg.FilterRule{ + { + SrcIPs: []string{"*", "10.1.3.4/32", "10.0.0.0/24"}, + SrcBits: []int{1, 2, 3}, + DstPorts: []tailcfg.NetPortRange{{ + IP: "1.2.3.4/32", + Bits: ptrTo(32), + Ports: tailcfg.PortRange{First: 1, Last: 2}, + }}, + IPProto: []int{1, 2, 3, 4}, + CapGrant: []tailcfg.CapGrant{{ + Dsts: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.2.3.4/32")}, + Caps: []string{"foo"}, + }}, + }, + { + SrcIPs: []string{"foooooooooo"}, + DstPorts: []tailcfg.NetPortRange{{ + IP: "baaaaaarrrrr", + Ports: tailcfg.PortRange{First: 1, Last: 2}, + }}, + }, + { + SrcIPs: []string{"foooooooooo"}, + DstPorts: []tailcfg.NetPortRange{{ + IP: "baaaaaarrrrr", + Ports: tailcfg.PortRange{First: 1, Last: 2}, + }}, + }, + { + SrcIPs: []string{"foooooooooo"}, + DstPorts: []tailcfg.NetPortRange{{ + IP: "baaaaaarrrrr", + Ports: tailcfg.PortRange{First: 1, Last: 2}, + }}, + }, +} + +func BenchmarkHashPacketFilter(b *testing.B) { + b.ReportAllocs() + + for i := 0; i < b.N; i++ { + sink = Hash(filterRules) + } +} + func TestHashMapAcyclic(t *testing.T) { m := map[int]string{} for i := 0; i < 100; i++ {