From 8f19e5bdd7301122640b3900e5f6e5deb15c55e3 Mon Sep 17 00:00:00 2001 From: phuslu Date: Wed, 3 Jan 2024 12:18:51 +0800 Subject: [PATCH] add freelru and make 90% reads --- README.md | 2 +- bench/bench_test.go | 39 +++++++++++++++++++++++++++++++++------ bench/go.mod | 3 ++- bench/go.sum | 2 ++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 44f3cf0..1e1319f 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ func main() { ### Benchmarks -A Performance result on keysize=16, cachesize=1000000, parallelism=32 with Read(75%)/Write(25%). Check [actions][actions] for more results and details. +A Performance result on keysize=16, cachesize=1000000, parallelism=32 with Read(90%)/Write(10%). Check [actions][actions] for more results and details. ``` goos: linux goarch: amd64 diff --git a/bench/bench_test.go b/bench/bench_test.go index 21175d3..fe03899 100644 --- a/bench/bench_test.go +++ b/bench/bench_test.go @@ -8,8 +8,10 @@ import ( _ "unsafe" theine "github.com/Yiling-J/theine-go" + "github.com/cespare/xxhash/v2" cloudflare "github.com/cloudflare/golibs/lrucache" ristretto "github.com/dgraph-io/ristretto" + freelru "github.com/elastic/go-freelru" otter "github.com/maypok86/otter" ecache "github.com/orca-zhang/ecache" phuslu "github.com/phuslu/lru" @@ -44,7 +46,7 @@ func BenchmarkCloudflareGet(b *testing.B) { expires := time.Now().Add(time.Hour) for pb.Next() { i := int(fastrandn(cachesize)) - if i >= cachesize/4 { + if i >= cachesize/10 { cache.Get(keymap[i]) } else { cache.Set(keymap[i], i, expires) @@ -63,7 +65,7 @@ func BenchmarkEcacheGet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - if i >= cachesize/4 { + if i >= cachesize/10 { cache.Get(keymap[i]) } else { cache.Put(keymap[i], i) @@ -88,7 +90,7 @@ func BenchmarkRistrettoGet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - if i >= cachesize/4 { + if i >= cachesize/10 { cache.Get(keymap[i]) } else { cache.SetWithTTL(keymap[i], i, 1, time.Hour) @@ -109,7 +111,7 @@ func BenchmarkTheineGet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - if i >= cachesize/4 { + if i >= cachesize/10 { cache.Get(keymap[i]) } else { cache.SetWithTTL(keymap[i], i, 1, time.Hour) @@ -130,7 +132,7 @@ func BenchmarkOtterGet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - if i >= cachesize/4 { + if i >= cachesize/10 { cache.Get(keymap[i]) } else { cache.SetWithTTL(keymap[i], i, time.Hour) @@ -139,6 +141,31 @@ func BenchmarkOtterGet(b *testing.B) { }) } +func hashStringXXHASH(s string) uint32 { + return uint32(xxhash.Sum64String(s)) +} + +func BenchmarkFreelruGet(b *testing.B) { + cache, _ := freelru.NewSharded[string, int](cachesize, hashStringXXHASH) + for i := 0; i < cachesize/2; i++ { + cache.AddWithLifetime(keymap[i], i, time.Hour) + } + + b.SetParallelism(parallelism) + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + i := int(fastrandn(cachesize)) + if i >= cachesize/10 { + cache.Get(keymap[i]) + } else { + cache.AddWithLifetime(keymap[i], i, time.Hour) + } + } + }) +} + func BenchmarkPhusluGet(b *testing.B) { cache := phuslu.New[string, int](cachesize) for i := 0; i < cachesize/2; i++ { @@ -151,7 +178,7 @@ func BenchmarkPhusluGet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { i := int(fastrandn(cachesize)) - if i >= cachesize/4 { + if i >= cachesize/10 { cache.Get(keymap[i]) } else { cache.SetWithTTL(keymap[i], i, time.Hour) diff --git a/bench/go.mod b/bench/go.mod index ed61937..100d0d6 100644 --- a/bench/go.mod +++ b/bench/go.mod @@ -4,15 +4,16 @@ go 1.21.1 require ( github.com/Yiling-J/theine-go v0.3.1 + github.com/cespare/xxhash/v2 v2.1.2 github.com/cloudflare/golibs v0.0.0-20210909181612-21743d7dd02a github.com/dgraph-io/ristretto v0.1.1 + github.com/elastic/go-freelru v0.9.0 github.com/maypok86/otter v0.0.0-20231222143008-a9479c80c78a github.com/orca-zhang/ecache v1.1.1 github.com/phuslu/lru v0.0.0-00010101000000-000000000000 ) require ( - github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/dolthub/maphash v0.1.0 // indirect github.com/dolthub/swiss v0.2.1 // indirect github.com/dustin/go-humanize v1.0.0 // indirect diff --git a/bench/go.sum b/bench/go.sum index a7aec12..990fadc 100644 --- a/bench/go.sum +++ b/bench/go.sum @@ -19,6 +19,8 @@ github.com/dolthub/swiss v0.2.1 h1:gs2osYs5SJkAaH5/ggVJqXQxRXtWshF6uE0lgR/Y3Gw= github.com/dolthub/swiss v0.2.1/go.mod h1:8AhKZZ1HK7g18j7v7k6c5cYIGEZJcPn0ARsai8cUrh0= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/elastic/go-freelru v0.9.0 h1:s9K5Q4xBoQC96XogjymtKDYfcABkoyDUoSIG4vprywg= +github.com/elastic/go-freelru v0.9.0/go.mod h1:bSdWT4M0lW79K8QbX6XY2heQYSCqD7THoYf82pT/H3I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/gammazero/deque v0.2.1 h1:qSdsbG6pgp6nL7A0+K/B7s12mcCY/5l5SIUpMOl+dC0=