Commit 72fc9c96 authored by Aaron Jacobs's avatar Aaron Jacobs

Add a benchmark for growing and shrinking.

This should better stress memclr.
parent b87ffb52
......@@ -85,3 +85,34 @@ func BenchmarkOutMessageReset(b *testing.B) {
b.SetBytes(int64(oms[0].offset))
})
}
func BenchmarkOutMessageGrowShrink(b *testing.B) {
// A single buffer, which should fit in some level of CPU cache.
b.Run("Single buffer", func(b *testing.B) {
var om OutMessage
for i := 0; i < b.N; i++ {
om.Grow(MaxReadSize)
om.ShrinkTo(OutMessageInitialSize)
}
b.SetBytes(int64(MaxReadSize))
})
// Many megabytes worth of buffers, which should defeat the CPU cache.
b.Run("Many buffers", func(b *testing.B) {
// The number of messages; intentionally a power of two.
const numMessages = 128
var oms [numMessages]OutMessage
if s := unsafe.Sizeof(oms); s < 128<<20 {
panic(fmt.Sprintf("Array is too small; total size: %d", s))
}
for i := 0; i < b.N; i++ {
oms[i%numMessages].Grow(MaxReadSize)
oms[i%numMessages].ShrinkTo(OutMessageInitialSize)
}
b.SetBytes(int64(MaxReadSize))
})
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment