Skip to content

Commit 7b9a901

Browse files
committed
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 <bradfitz@tailscale.com>
1 parent c88bd53 commit 7b9a901

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

util/deephash/deephash_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,57 @@ func BenchmarkHash(b *testing.B) {
324324
}
325325
}
326326

327+
func ptrTo[T any](v T) *T { return &v }
328+
329+
// filterRules is a packet filter that has both everything populated (in its
330+
// first element) and also a few entries that are the typical shape for regular
331+
// packet filters as sent to clients.
332+
var filterRules = []tailcfg.FilterRule{
333+
{
334+
SrcIPs: []string{"*", "10.1.3.4/32", "10.0.0.0/24"},
335+
SrcBits: []int{1, 2, 3},
336+
DstPorts: []tailcfg.NetPortRange{{
337+
IP: "1.2.3.4/32",
338+
Bits: ptrTo(32),
339+
Ports: tailcfg.PortRange{First: 1, Last: 2},
340+
}},
341+
IPProto: []int{1, 2, 3, 4},
342+
CapGrant: []tailcfg.CapGrant{{
343+
Dsts: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.2.3.4/32")},
344+
Caps: []string{"foo"},
345+
}},
346+
},
347+
{
348+
SrcIPs: []string{"foooooooooo"},
349+
DstPorts: []tailcfg.NetPortRange{{
350+
IP: "baaaaaarrrrr",
351+
Ports: tailcfg.PortRange{First: 1, Last: 2},
352+
}},
353+
},
354+
{
355+
SrcIPs: []string{"foooooooooo"},
356+
DstPorts: []tailcfg.NetPortRange{{
357+
IP: "baaaaaarrrrr",
358+
Ports: tailcfg.PortRange{First: 1, Last: 2},
359+
}},
360+
},
361+
{
362+
SrcIPs: []string{"foooooooooo"},
363+
DstPorts: []tailcfg.NetPortRange{{
364+
IP: "baaaaaarrrrr",
365+
Ports: tailcfg.PortRange{First: 1, Last: 2},
366+
}},
367+
},
368+
}
369+
370+
func BenchmarkHashPacketFilter(b *testing.B) {
371+
b.ReportAllocs()
372+
373+
for i := 0; i < b.N; i++ {
374+
sink = Hash(filterRules)
375+
}
376+
}
377+
327378
func TestHashMapAcyclic(t *testing.T) {
328379
m := map[int]string{}
329380
for i := 0; i < 100; i++ {

0 commit comments

Comments
 (0)