Skip to content

Commit

Permalink
add freelru and make 90% reads
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Jan 3, 2024
1 parent 41cc937 commit 8f19e5b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 33 additions & 6 deletions bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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++ {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion bench/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bench/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 8f19e5b

Please sign in to comment.