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
This commit is contained in:
Aliaksandr Valialkin 2022-07-27 22:15:01 +03:00
parent 5f2b5bd173
commit 547cb1edce
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -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