From d6d19518978529a561ce5a3821f6499151cdb248 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 18 May 2021 12:46:10 -0700 Subject: [PATCH] net/packet: add BenchmarkString Signed-off-by: Josh Bleecher Snyder --- net/packet/packet_test.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/net/packet/packet_test.go b/net/packet/packet_test.go index ac4fa33f3..a88ab9675 100644 --- a/net/packet/packet_test.go +++ b/net/packet/packet_test.go @@ -378,11 +378,9 @@ func TestParsedString(t *testing.T) { }) } - var sink string allocs := testing.AllocsPerRun(1000, func() { - sink = tests[0].qdecode.String() + sinkString = tests[0].qdecode.String() }) - _ = sink if allocs != 1 { t.Errorf("allocs = %v; want 1", allocs) } @@ -532,3 +530,33 @@ func TestMarshalResponse(t *testing.T) { }) } } + +var sinkString string + +func BenchmarkString(b *testing.B) { + benches := []struct { + name string + buf []byte + }{ + {"tcp4", tcp4PacketBuffer}, + {"tcp6", tcp6RequestBuffer}, + {"udp4", udp4RequestBuffer}, + {"udp6", udp6RequestBuffer}, + {"icmp4", icmp4RequestBuffer}, + {"icmp6", icmp6PacketBuffer}, + {"igmp", igmpPacketBuffer}, + {"unknown", unknownPacketBuffer}, + } + + for _, bench := range benches { + b.Run(bench.name, func(b *testing.B) { + b.ReportAllocs() + var p Parsed + p.Decode(bench.buf) + b.ResetTimer() + for i := 0; i < b.N; i++ { + sinkString = p.String() + } + }) + } +}