Skip to content

Commit 8b8ee48

Browse files
committed
cache: improve BenchmarkCacheGet
We improve BenchmarkCacheGet to use the default number of shards and to avoid the random generator in the hot path.
1 parent 4f3e287 commit 8b8ee48

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

internal/cache/cache_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"strconv"
1515
"sync"
1616
"testing"
17-
"time"
1817

1918
"github.com/cockroachdb/pebble/internal/base"
2019
"github.com/stretchr/testify/require"
@@ -289,10 +288,12 @@ func TestCacheStressSetExisting(t *testing.T) {
289288
}
290289

291290
func BenchmarkCacheGet(b *testing.B) {
292-
const size = 100000
291+
const size = 1_000_000
293292

294-
n := runtime.GOMAXPROCS(0)
295-
cache := NewWithShards(size*int64(n), n)
293+
// We double the size to allow for shard imbalances. With many objects and
294+
// relatively few shards, the probability that any bucket is more than double
295+
// the expected size is vanishingly small.
296+
cache := New(2 * size)
296297
defer cache.Unref()
297298
h := cache.NewHandle()
298299
defer h.Close()
@@ -303,12 +304,12 @@ func BenchmarkCacheGet(b *testing.B) {
303304

304305
b.ResetTimer()
305306
b.RunParallel(func(pb *testing.PB) {
306-
rng := rand.New(rand.NewPCG(0, uint64(time.Now().UnixNano())))
307-
307+
pcg := rand.NewPCG(rand.Uint64(), rand.Uint64())
308308
for pb.Next() {
309-
v := h.Get(base.DiskFileNum(0), uint64(rng.IntN(size)))
309+
offset := pcg.Uint64() % size
310+
v := h.Get(base.DiskFileNum(0), offset)
310311
if v == nil {
311-
b.Fatal("failed to lookup value")
312+
b.Fatal("failed to look up value")
312313
}
313314
v.Release()
314315
}

0 commit comments

Comments
 (0)