Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update benchmark #14

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 89 additions & 26 deletions benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package benchmark

import (
"sync/atomic"
"testing"
"time"

"github.com/dgraph-io/ristretto"
"github.com/lxzan/memorycache"
"github.com/lxzan/memorycache/internal/utils"
"github.com/maypok86/otter"
)

const benchcount = 1000000
const (
benchcount = 1280000
capacity = benchcount / 10
)

var benchkeys = make([]string, 0, benchcount)

Expand All @@ -23,12 +26,13 @@ func init() {
func BenchmarkMemoryCache_Set(b *testing.B) {
var mc = memorycache.New(
memorycache.WithBucketNum(128),
memorycache.WithBucketSize(1000, 10000),
memorycache.WithBucketSize(capacity/1280, capacity/128),
)
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
i := 0
for pb.Next() {
index := i.Add(1) % benchcount
index := i % benchcount
i++
mc.Set(benchkeys[index], 1, time.Hour)
}
})
Expand All @@ -37,17 +41,18 @@ func BenchmarkMemoryCache_Set(b *testing.B) {
func BenchmarkMemoryCache_Get(b *testing.B) {
var mc = memorycache.New(
memorycache.WithBucketNum(128),
memorycache.WithBucketSize(1000, 10000),
memorycache.WithBucketSize(capacity/1280, capacity/128),
)
for i := 0; i < benchcount; i++ {
mc.Set(benchkeys[i%benchcount], 1, time.Hour)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
i := 0
for pb.Next() {
index := i.Add(1) % benchcount
index := i % benchcount
i++
mc.Get(benchkeys[index])
}
})
Expand All @@ -56,17 +61,18 @@ func BenchmarkMemoryCache_Get(b *testing.B) {
func BenchmarkMemoryCache_SetAndGet(b *testing.B) {
var mc = memorycache.New(
memorycache.WithBucketNum(128),
memorycache.WithBucketSize(1000, 10000),
memorycache.WithBucketSize(capacity/1280, capacity/128),
)
for i := 0; i < benchcount; i++ {
mc.Set(benchkeys[i%benchcount], 1, time.Hour)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
i := 0
for pb.Next() {
index := i.Add(1) % benchcount
index := i % benchcount
i++
if index&7 == 0 {
mc.Set(benchkeys[index], 1, time.Hour)
} else {
Expand All @@ -78,54 +84,57 @@ func BenchmarkMemoryCache_SetAndGet(b *testing.B) {

func BenchmarkRistretto_Set(b *testing.B) {
var mc, _ = ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
NumCounters: 10 * capacity, // number of keys to track frequency of (10M).
MaxCost: capacity, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
i := 0
for pb.Next() {
index := i.Add(1) % benchcount
index := i % benchcount
i++
mc.SetWithTTL(benchkeys[index], 1, 1, time.Hour)
}
})
}

func BenchmarkRistretto_Get(b *testing.B) {
var mc, _ = ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
NumCounters: 10 * capacity, // number of keys to track frequency of (10M).
MaxCost: capacity, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
for i := 0; i < benchcount; i++ {
mc.SetWithTTL(benchkeys[i%benchcount], 1, 1, time.Hour)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
i := 0
for pb.Next() {
index := i.Add(1) % benchcount
index := i % benchcount
i++
mc.Get(benchkeys[index])
}
})
}

func BenchmarkRistretto_SetAndGet(b *testing.B) {
var mc, _ = ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
NumCounters: 10 * capacity, // number of keys to track frequency of (10M).
MaxCost: capacity, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
for i := 0; i < benchcount; i++ {
mc.SetWithTTL(benchkeys[i%benchcount], 1, 1, time.Hour)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
i := 0
for pb.Next() {
index := i.Add(1) % benchcount
index := i % benchcount
i++
if index&7 == 0 {
mc.SetWithTTL(benchkeys[index], 1, 1, time.Hour)
} else {
Expand All @@ -134,3 +143,57 @@ func BenchmarkRistretto_SetAndGet(b *testing.B) {
}
})
}

func BenchmarkOtter_Set(b *testing.B) {
var mc, _ = otter.MustBuilder[string, int](capacity).Build()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
index := i % benchcount
i++
mc.SetWithTTL(benchkeys[index], 1, time.Hour)
}
})
}

func BenchmarkOtter_Get(b *testing.B) {
mc, _ := otter.MustBuilder[string, int](capacity).Build()
for i := 0; i < benchcount; i++ {
mc.SetWithTTL(benchkeys[i%benchcount], 1, time.Hour)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
index := i % benchcount
i++
if index&7 == 0 {
mc.SetWithTTL(benchkeys[index], 1, time.Hour)
} else {
mc.Get(benchkeys[index])
}
}
})
}

func BenchmarkOtter_SetAndGet(b *testing.B) {
mc, _ := otter.MustBuilder[string, int](capacity).Build()
for i := 0; i < benchcount; i++ {
mc.SetWithTTL(benchkeys[i%benchcount], 1, time.Hour)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
index := i % benchcount
i++
if index&7 == 0 {
mc.SetWithTTL(benchkeys[index], 1, time.Hour)
} else {
mc.Get(benchkeys[index])
}
}
})
}
6 changes: 6 additions & 0 deletions benchmark/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ require (

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
github.com/gammazero/deque v0.2.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/maypok86/otter v0.0.0-20231114210221-6df2759dce89 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect
)

Expand Down
12 changes: 12 additions & 0 deletions benchmark/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWa
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=
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/gammazero/deque v0.2.1 h1:qSdsbG6pgp6nL7A0+K/B7s12mcCY/5l5SIUpMOl+dC0=
github.com/gammazero/deque v0.2.1/go.mod h1:LFroj8x4cMYCukHJDbxFCkT+r9AndaJnFMuZDV34tuU=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/maypok86/otter v0.0.0-20231114210221-6df2759dce89 h1:UrkdP/BbtB8zS2wjjZpbP7j8KN+UR8VhUxkKmPqR7WQ=
github.com/maypok86/otter v0.0.0-20231114210221-6df2759dce89/go.mod h1:zSGbZqHFYdVss62wCVkvEf+TyTamBVR520Cl3pO/dJ0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -24,6 +34,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down