From 547cb1edce38ef27c24f125a07f93c200b0ed4fe Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 27 Jul 2022 22:15:01 +0300 Subject: [PATCH] benchmark inmemoryBlock.{Marshal,Unmarshal} for different prefix length Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2254 This is needed for https://github.com/VictoriaMetrics/VictoriaMetrics/pull/2913 --- lib/mergeset/encoding_timing_test.go | 42 ++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/mergeset/encoding_timing_test.go b/lib/mergeset/encoding_timing_test.go index 982659256..2955cc3f5 100644 --- a/lib/mergeset/encoding_timing_test.go +++ b/lib/mergeset/encoding_timing_test.go @@ -8,13 +8,15 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" ) +var benchPrefixes = []string{ + "", "x", "xy", "xyz", "xyz1", "xyz12", + "xyz123", "xyz1234", "01234567", "xyz123456", "xyz123456789012345678901234567890", + "aljkljfdpjopoewpoirerop934093094poipdfidpfdsfkjljdfpjoejkdjfljpfdkl", + "aljkljfdpjopoewpoirerop934093094poipdfidpfdsfkjljdfpjoejkdjfljpfdkllkj321oiiou321oijlkfdfjjlfdsjdslkfjdslfjldskafjldsflkfdsjlkj", +} + func BenchmarkCommonPrefixLen(b *testing.B) { - for _, prefix := range []string{ - "", "x", "xy", "xyz", "xyz1", "xyz12", - "xyz123", "xyz1234", "01234567", "xyz123456", "xyz123456789012345678901234567890", - "aljkljfdpjopoewpoirerop934093094poipdfidpfdsfkjljdfpjoejkdjfljpfdkl", - "aljkljfdpjopoewpoirerop934093094poipdfidpfdsfkjljdfpjoejkdjfljpfdkllkj321oiiou321oijlkfdfjj lfdsjdslkfjdslfj ldskafjldsflkfdsjlkj", - } { + for _, prefix := range benchPrefixes { b.Run(fmt.Sprintf("prefix-len-%d", len(prefix)), func(b *testing.B) { benchmarkCommonPrefixLen(b, prefix) }) @@ -39,10 +41,18 @@ func benchmarkCommonPrefixLen(b *testing.B, prefix string) { } func BenchmarkInmemoryBlockMarshal(b *testing.B) { - const itemsCount = 1000 + for _, prefix := range benchPrefixes { + b.Run(fmt.Sprintf("prefix-len-%d", len(prefix)), func(b *testing.B) { + benchmarkInmemoryBlockMarshal(b, prefix) + }) + } +} + +func benchmarkInmemoryBlockMarshal(b *testing.B, prefix string) { + const itemsCount = 500 var ibSrc inmemoryBlock for i := 0; i < itemsCount; i++ { - item := []byte(fmt.Sprintf("key %d", i)) + item := []byte(fmt.Sprintf("%s%d", prefix, i)) if !ibSrc.Add(item) { b.Fatalf("cannot add more than %d items", i) } @@ -50,7 +60,7 @@ func BenchmarkInmemoryBlockMarshal(b *testing.B) { sort.Sort(&ibSrc) b.ResetTimer() - b.SetBytes(itemsCount) + b.SetBytes(int64(itemsCount * len(prefix))) b.ReportAllocs() b.RunParallel(func(pb *testing.PB) { var sb storageBlock @@ -66,9 +76,17 @@ func BenchmarkInmemoryBlockMarshal(b *testing.B) { } func BenchmarkInmemoryBlockUnmarshal(b *testing.B) { + for _, prefix := range benchPrefixes { + b.Run(fmt.Sprintf("prefix-len-%d", len(prefix)), func(b *testing.B) { + benchmarkInmemoryBlockUnmarshal(b, prefix) + }) + } +} + +func benchmarkInmemoryBlockUnmarshal(b *testing.B, prefix string) { var ibSrc inmemoryBlock - for i := 0; i < 1000; i++ { - item := []byte(fmt.Sprintf("key %d", i)) + for i := 0; i < 500; i++ { + item := []byte(fmt.Sprintf("%s%d", prefix, i)) if !ibSrc.Add(item) { b.Fatalf("cannot add more than %d items", i) } @@ -77,7 +95,7 @@ func BenchmarkInmemoryBlockUnmarshal(b *testing.B) { firstItem, commonPrefix, itemsCount, mt := ibSrc.MarshalUnsortedData(&sbSrc, nil, nil, 0) b.ResetTimer() - b.SetBytes(int64(itemsCount)) + b.SetBytes(int64(itemsCount) * int64(len(prefix))) b.ReportAllocs() b.RunParallel(func(pb *testing.PB) { var ib inmemoryBlock