From 266893f314308178ef1afeafba7363af4d68b731 Mon Sep 17 00:00:00 2001 From: phuslu Date: Mon, 16 Dec 2024 00:13:56 +0800 Subject: [PATCH] simpify benchmark code --- README.md | 59 +++++++++++-------------------------------------------- 1 file changed, 12 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 43d01ca..101cf45 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ A Performance result as below. Check github [benchmark][benchmark] action for mo go1.24 benchmark on keysize=16, itemsize=1000000, cachesize=50%, concurrency=8 ```go -// env writeratio=0.1 zipfian=false go test -v -cpu=8 -run=none -bench=. -benchtime=5s -benchmem bench_test.go +// env writeratio=0.05 go test -v -cpu=8 -run=none -bench=. -benchtime=5s -benchmem bench_test.go package bench import ( @@ -88,7 +88,6 @@ import ( otter "github.com/maypok86/otter" ecache "github.com/orca-zhang/ecache" phuslu "github.com/phuslu/lru" - "github.com/aclements/go-perfevent/perfbench" ) const ( @@ -97,7 +96,6 @@ const ( ) var writeratio, _ = strconv.ParseFloat(os.Getenv("writeratio"), 64) -var zipfian, _ = strconv.ParseBool(os.Getenv("zipfian")) type CheapRand struct { Seed uint64 @@ -135,7 +133,6 @@ var keys = func() (x []string) { }() func BenchmarkHashicorpSetGet(b *testing.B) { - c := perfbench.Open(b) cache := hashicorp.NewLRU[string, int](cachesize, nil, time.Hour) for i := range cachesize/2 { cache.Add(keys[i], i) @@ -151,17 +148,14 @@ func BenchmarkHashicorpSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.Add(keys[i], i) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) } func BenchmarkCloudflareSetGet(b *testing.B) { - c := perfbench.Open(b) cache := cloudflare.NewMultiLRUCache(uint(shardcount), uint(cachesize/shardcount)) for i := range cachesize/2 { cache.Set(keys[i], i, time.Now().Add(time.Hour)) @@ -178,17 +172,14 @@ func BenchmarkCloudflareSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.Set(keys[i], i, expires) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) } func BenchmarkEcacheSetGet(b *testing.B) { - c := perfbench.Open(b) cache := ecache.NewLRUCache(uint16(shardcount), uint16(cachesize/shardcount), time.Hour) for i := range cachesize/2 { cache.Put(keys[i], i) @@ -204,17 +195,14 @@ func BenchmarkEcacheSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.Put(keys[i], i) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) } func BenchmarkLxzanSetGet(b *testing.B) { - c := perfbench.Open(b) cache := lxzan.New[string, int]( lxzan.WithBucketNum(shardcount), lxzan.WithBucketSize(cachesize/shardcount, cachesize/shardcount), @@ -234,10 +222,8 @@ func BenchmarkLxzanSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.Set(keys[i], i, time.Hour) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) @@ -248,7 +234,6 @@ func hashStringXXHASH(s string) uint32 { } func BenchmarkFreelruSetGet(b *testing.B) { - c := perfbench.Open(b) cache, _ := freelru.NewSharded[string, int](cachesize, hashStringXXHASH) for i := range cachesize/2 { cache.AddWithLifetime(keys[i], i, time.Hour) @@ -264,17 +249,14 @@ func BenchmarkFreelruSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.AddWithLifetime(keys[i], i, time.Hour) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) } func BenchmarkPhusluSetGet(b *testing.B) { - c := perfbench.Open(b) cache := phuslu.NewTTLCache[string, int](cachesize, phuslu.WithShards[string, int](uint32(shardcount))) for i := range cachesize/2 { cache.Set(keys[i], i, time.Hour) @@ -290,17 +272,14 @@ func BenchmarkPhusluSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.Set(keys[i], i, time.Hour) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) } func BenchmarkNoTTLSetGet(b *testing.B) { - c := perfbench.Open(b) cache := phuslu.NewLRUCache[string, int](cachesize, phuslu.WithShards[string, int](uint32(shardcount))) for i := range cachesize/2 { cache.Set(keys[i], i) @@ -316,17 +295,14 @@ func BenchmarkNoTTLSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.Set(keys[i], i) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) } func BenchmarkCcacheSetGet(b *testing.B) { - c := perfbench.Open(b) cache := ccache.New(ccache.Configure[int]().MaxSize(cachesize).ItemsToPrune(100)) for i := range cachesize/2 { cache.Set(keys[i], i, time.Hour) @@ -342,17 +318,14 @@ func BenchmarkCcacheSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.Set(keys[i], i, time.Hour) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) } func BenchmarkRistrettoSetGet(b *testing.B) { - c := perfbench.Open(b) cache, _ := ristretto.NewCache(&ristretto.Config[string, int]{ NumCounters: 10 * cachesize, // number of keys to track frequency of (10M). MaxCost: cachesize, // maximum cost of cache (1M). @@ -372,17 +345,14 @@ func BenchmarkRistrettoSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.SetWithTTL(keys[i], i, 1, time.Hour) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) } func BenchmarkTheineSetGet(b *testing.B) { - c := perfbench.Open(b) cache, _ := theine.NewBuilder[string, int](cachesize).Build() for i := range cachesize/2 { cache.SetWithTTL(keys[i], i, 1, time.Hour) @@ -398,17 +368,14 @@ func BenchmarkTheineSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.SetWithTTL(keys[i], i, 1, time.Hour) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } }) } func BenchmarkOtterSetGet(b *testing.B) { - c := perfbench.Open(b) cache, _ := otter.MustBuilder[string, int](cachesize).WithVariableTTL().Build() for i := range cachesize/2 { cache.Set(keys[i], i, time.Hour) @@ -424,10 +391,8 @@ func BenchmarkOtterSetGet(b *testing.B) { if threshold > 0 && cheaprand.Uint32() <= threshold { i := int(cheaprand.Uint32n(cachesize)) cache.Set(keys[i], i, time.Hour) - } else if zipfian { - cache.Get(keys[zipf.Uint64()]) } else { - cache.Get(keys[cheaprand.Uint32n(cachesize)]) + cache.Get(keys[zipf.Uint64()]) } } })