lib/mergeset: add tests and benchmarks for commonPrefixLen function

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2254

This is needed for https://github.com/VictoriaMetrics/VictoriaMetrics/pull/2913
This commit is contained in:
Aliaksandr Valialkin 2022-07-27 21:21:41 +03:00
parent ab645b5fab
commit 5f2b5bd173
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 51 additions and 0 deletions

View file

@ -11,6 +11,27 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
)
func TestCommonPrefixLen(t *testing.T) {
f := func(a, b string, expectedPrefixLen int) {
t.Helper()
prefixLen := commonPrefixLen([]byte(a), []byte(b))
if prefixLen != expectedPrefixLen {
t.Fatalf("unexpected prefix len; got %d; want %d", prefixLen, expectedPrefixLen)
}
}
f("", "", 0)
f("a", "", 0)
f("", "a", 0)
f("a", "a", 1)
f("abc", "xy", 0)
f("abc", "abd", 2)
f("01234567", "01234567", 8)
f("01234567", "012345678", 8)
f("012345679", "012345678", 8)
f("01234569", "012345678", 7)
f("01234569", "01234568", 7)
}
func TestInmemoryBlockAdd(t *testing.T) {
var ib inmemoryBlock

View file

@ -8,6 +8,36 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
)
func BenchmarkCommonPrefixLen(b *testing.B) {
for _, prefix := range []string{
"", "x", "xy", "xyz", "xyz1", "xyz12",
"xyz123", "xyz1234", "01234567", "xyz123456", "xyz123456789012345678901234567890",
"aljkljfdpjopoewpoirerop934093094poipdfidpfdsfkjljdfpjoejkdjfljpfdkl",
"aljkljfdpjopoewpoirerop934093094poipdfidpfdsfkjljdfpjoejkdjfljpfdkllkj321oiiou321oijlkfdfjj lfdsjdslkfjdslfj ldskafjldsflkfdsjlkj",
} {
b.Run(fmt.Sprintf("prefix-len-%d", len(prefix)), func(b *testing.B) {
benchmarkCommonPrefixLen(b, prefix)
})
}
}
func benchmarkCommonPrefixLen(b *testing.B, prefix string) {
b.ReportAllocs()
b.SetBytes(int64(len(prefix)))
b.RunParallel(func(pb *testing.PB) {
a := append([]byte{}, prefix...)
a = append(a, 'a')
b := append([]byte{}, prefix...)
b = append(b, 'b')
for pb.Next() {
n := commonPrefixLen(a, b)
if n != len(prefix) {
panic(fmt.Errorf("unexpected prefix len; got %d; want %d", n, len(prefix)))
}
}
})
}
func BenchmarkInmemoryBlockMarshal(b *testing.B) {
const itemsCount = 1000
var ibSrc inmemoryBlock