Skip to content

Commit

Permalink
Add benchmark comparing EmbeddedCache with new BlocksCache
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
  • Loading branch information
chaudum committed Mar 20, 2024
1 parent d42b09e commit ef0ef23
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions pkg/storage/stores/shipper/bloomshipper/blockscache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package bloomshipper

import (
"context"
"fmt"
"testing"
"time"

"github.com/go-kit/log"
"github.com/grafana/dskit/flagext"
"github.com/grafana/loki/pkg/storage/chunk/cache"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)

var (
Expand Down Expand Up @@ -330,3 +333,80 @@ func TestBlocksCache_RefCounter(t *testing.T) {
_ = cache.Release(ctx, "a")
require.Equal(t, int32(0), cache.entries["a"].Value.(*Entry).refCount.Load())
}

func prepareBenchmark(b *testing.B) map[string]BlockDirectory {
b.Helper()

entries := make(map[string]BlockDirectory)
for i := 0; i < 1000; i++ {
key := fmt.Sprintf("block-%04x", i)
entries[key] = BlockDirectory{
BlockRef: BlockRef{},
Path: fmt.Sprintf("blooms/%s", key),
removeDirectoryTimeout: time.Minute,
refCount: atomic.NewInt32(0),
logger: logger,
activeQueriersCheckInterval: time.Minute,
size: 4 << 10,
}
}
return entries
}

func Benchmark_BlocksCacheOld(b *testing.B) {
prepareBenchmark(b)
b.StopTimer()
cfg := cache.EmbeddedCacheConfig{
Enabled: true,
MaxSizeMB: 100,
MaxSizeItems: 10000,
TTL: time.Hour,
PurgeInterval: time.Hour,
}
cache := NewBlocksCache(cfg, nil, logger)
entries := prepareBenchmark(b)
ctx := context.Background()
b.ReportAllocs()
b.StartTimer()

// write
for k, v := range entries {
err := cache.Store(ctx, []string{k}, []BlockDirectory{v})
require.NoError(b, err)
}
for i := 0; i < b.N; i++ {
// read
for k, _ := range entries {
_, _ = cache.Get(ctx, k)
}
}

}

func Benchmark_BlocksCacheNew(b *testing.B) {
prepareBenchmark(b)
b.StopTimer()
cfg := BlocksCacheConfig{
Enabled: true,
SoftLimit: 100 << 20,
HardLimit: 120 << 20,
TTL: time.Hour,
PurgeInterval: time.Hour,
}
cache := NewFsBlocksCache(cfg, nil, logger)
entries := prepareBenchmark(b)
ctx := context.Background()
b.ReportAllocs()
b.StartTimer()

// write
for k, v := range entries {
_ = cache.PutMany(ctx, []string{k}, []BlockDirectory{v})
}
// read
for i := 0; i < b.N; i++ {
for k, _ := range entries {
_, _ = cache.Get(ctx, k)
}
}
}

0 comments on commit ef0ef23

Please sign in to comment.