From 5f2b5bd173b1e96a5f12664e9feb73d0f6ec49a2 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 27 Jul 2022 21:21:41 +0300 Subject: [PATCH] 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 --- lib/mergeset/encoding_test.go | 21 +++++++++++++++++++ lib/mergeset/encoding_timing_test.go | 30 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/mergeset/encoding_test.go b/lib/mergeset/encoding_test.go index 646901a73..48398ae2f 100644 --- a/lib/mergeset/encoding_test.go +++ b/lib/mergeset/encoding_test.go @@ -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 diff --git a/lib/mergeset/encoding_timing_test.go b/lib/mergeset/encoding_timing_test.go index 76369e3e3..982659256 100644 --- a/lib/mergeset/encoding_timing_test.go +++ b/lib/mergeset/encoding_timing_test.go @@ -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