From 84cec43bbe47885550252bae84db221148dec046 Mon Sep 17 00:00:00 2001 From: phuslu Date: Tue, 2 Jan 2024 12:28:53 +0800 Subject: [PATCH] run bench test with 75% reads 25% writes --- bench/bench_test.go | 60 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/bench/bench_test.go b/bench/bench_test.go index efb3a52..7edc3fa 100644 --- a/bench/bench_test.go +++ b/bench/bench_test.go @@ -39,56 +39,52 @@ func BenchmarkCloudflareGet(b *testing.B) { for i := 0; i < cachesize/2; i++ { cache.Set(keymap[i], i, time.Now().Add(time.Hour)) } - b.SetParallelism(parallelism) b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { + expires := time.Now().Add(time.Hour) for pb.Next() { i := int(fastrandn(cachesize)) - v, ok := cache.Get(keymap[i]) - if ok && v.(int) != i { - b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v) + if i >= cachesize/4 { + cache.Get(keymap[i]) + } else { + cache.Set(keymap[i], i, expires) } } }) } - func BenchmarkCcacheGet(b *testing.B) { cache := ccache.New(ccache.Configure[int]().MaxSize(cachesize)) for i := 0; i < cachesize/2; i++ { cache.Set(keymap[i], i, time.Hour) } - b.SetParallelism(parallelism) b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - v := cache.Get(keymap[i]) - if v != nil && v.Value() != i { - b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v) + if i >= cachesize/4 { + cache.Get(keymap[i]) + } else { + cache.Set(keymap[i], i, time.Hour) } } }) } - func BenchmarkEcacheGet(b *testing.B) { cache := ecache.NewLRUCache(1024, cachesize/1024, time.Hour) for i := 0; i < cachesize/2; i++ { cache.Put(keymap[i], i) } - b.SetParallelism(parallelism) b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - v, ok := cache.Get(keymap[i]) - if ok && v != i { - b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v) + if i >= cachesize/4 { + cache.Get(keymap[i]) + } else { + cache.Put(keymap[i], i) } } }) @@ -97,7 +93,7 @@ func BenchmarkEcacheGet(b *testing.B) { func BenchmarkRistrettoGet(b *testing.B) { cache, _ := ristretto.NewCache(&ristretto.Config{ NumCounters: cachesize, // number of keys to track frequency of (10M). - MaxCost: 1 << 30, // maximum cost of cache (1GB). + MaxCost: 2 << 30, // maximum cost of cache (2GB). BufferItems: 64, // number of keys per Get buffer. }) for i := 0; i < cachesize/2; i++ { @@ -110,9 +106,10 @@ func BenchmarkRistrettoGet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - v, ok := cache.Get(keymap[i]) - if ok && v != i { - b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v) + if i >= cachesize/4 { + cache.Get(keymap[i]) + } else { + cache.SetWithTTL(keymap[i], i, 1, time.Hour) } } }) @@ -130,9 +127,10 @@ func BenchmarkTheineGet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - v, ok := cache.Get(keymap[i]) - if ok && v != i { - b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v) + if i >= cachesize/4 { + _, _ = cache.Get(keymap[i]) + } else { + _ = cache.SetWithTTL(keymap[i], i, 1, time.Hour) } } }) @@ -150,9 +148,10 @@ func BenchmarkOtterGet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - v, ok := cache.Get(keymap[i]) - if ok && v != i { - b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v) + if i >= cachesize/4 { + _, _ = cache.Get(keymap[i]) + } else { + cache.SetWithTTL(keymap[i], i, time.Hour) } } }) @@ -170,9 +169,10 @@ func BenchmarkPhusluGet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - v, ok := cache.Get(keymap[i]) - if ok && v != i { - b.Fatalf("get %v from cache want %v, got %v", keymap[i], i, v) + if i >= cachesize/4 { + _, _ = cache.Get(keymap[i]) + } else { + cache.SetWithTTL(keymap[i], i, time.Hour) } } })