net/packet: add BenchmarkString

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
pull/1978/head
Josh Bleecher Snyder 4 years ago committed by Josh Bleecher Snyder
parent df350e2069
commit d6d1951897

@ -378,11 +378,9 @@ func TestParsedString(t *testing.T) {
}) })
} }
var sink string
allocs := testing.AllocsPerRun(1000, func() { allocs := testing.AllocsPerRun(1000, func() {
sink = tests[0].qdecode.String() sinkString = tests[0].qdecode.String()
}) })
_ = sink
if allocs != 1 { if allocs != 1 {
t.Errorf("allocs = %v; want 1", allocs) 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()
}
})
}
}

Loading…
Cancel
Save