Skip to content

Commit

Permalink
Merge pull request #15 from lxzan/dev
Browse files Browse the repository at this point in the history
merge dev into main
  • Loading branch information
lxzan authored Dec 4, 2023
2 parents 304fb51 + 5d22960 commit 1cb66a3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 51 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,17 @@ func main() {
- 1,000,000 elements

```
go test -benchmem -run=^$ -bench . github.com/lxzan/memorycache/benchmark
goos: linux
goarch: amd64
pkg: github.com/lxzan/memorycache/benchmark
cpu: AMD EPYC 7763 64-Core Processor
BenchmarkMemoryCache_Set-4 11106261 100.6 ns/op 18 B/op 0 allocs/op
BenchmarkMemoryCache_Get-4 635988 77.30 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-4 7933663 491.8 ns/op 170 B/op 2 allocs/op
BenchmarkRistretto_Get-4 11085688 98.92 ns/op 18 B/op 1 allocs/op
cpu: AMD Ryzen 5 PRO 4650G with Radeon Graphics
BenchmarkMemoryCache_Set-12 22848898 62.83 ns/op 8 B/op 0 allocs/op
BenchmarkMemoryCache_Get-12 47904933 30.94 ns/op 0 B/op 0 allocs/op
BenchmarkMemoryCache_SetAndGet-12 48951848 34.41 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-12 12992732 139.3 ns/op 118 B/op 2 allocs/op
BenchmarkRistretto_Get-12 27832851 45.11 ns/op 16 B/op 1 allocs/op
BenchmarkRistretto_SetAndGet-12 12232522 102.9 ns/op 32 B/op 1 allocs/op
PASS
ok github.com/lxzan/memorycache/benchmark 31.772s
```
14 changes: 9 additions & 5 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,17 @@ func main() {
- 1,000,000 元素

```
go test -benchmem -run=^$ -bench . github.com/lxzan/memorycache/benchmark
goos: linux
goarch: amd64
pkg: github.com/lxzan/memorycache/benchmark
cpu: AMD EPYC 7763 64-Core Processor
BenchmarkMemoryCache_Set-4 11106261 100.6 ns/op 18 B/op 0 allocs/op
BenchmarkMemoryCache_Get-4 635988 77.30 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-4 7933663 491.8 ns/op 170 B/op 2 allocs/op
BenchmarkRistretto_Get-4 11085688 98.92 ns/op 18 B/op 1 allocs/op
cpu: AMD Ryzen 5 PRO 4650G with Radeon Graphics
BenchmarkMemoryCache_Set-12 22848898 62.83 ns/op 8 B/op 0 allocs/op
BenchmarkMemoryCache_Get-12 47904933 30.94 ns/op 0 B/op 0 allocs/op
BenchmarkMemoryCache_SetAndGet-12 48951848 34.41 ns/op 0 B/op 0 allocs/op
BenchmarkRistretto_Set-12 12992732 139.3 ns/op 118 B/op 2 allocs/op
BenchmarkRistretto_Get-12 27832851 45.11 ns/op 16 B/op 1 allocs/op
BenchmarkRistretto_SetAndGet-12 12232522 102.9 ns/op 32 B/op 1 allocs/op
PASS
ok github.com/lxzan/memorycache/benchmark 31.772s
```
47 changes: 22 additions & 25 deletions benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import (
"github.com/maypok86/otter"
)

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

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

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

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

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
var i = 0
for pb.Next() {
index := i % benchcount
i++
Expand All @@ -84,12 +81,12 @@ func BenchmarkMemoryCache_SetAndGet(b *testing.B) {

func BenchmarkRistretto_Set(b *testing.B) {
var mc, _ = ristretto.NewCache(&ristretto.Config{
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.
NumCounters: 10000 * 128 * 10, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
b.RunParallel(func(pb *testing.PB) {
i := 0
var i = 0
for pb.Next() {
index := i % benchcount
i++
Expand All @@ -100,17 +97,17 @@ func BenchmarkRistretto_Set(b *testing.B) {

func BenchmarkRistretto_Get(b *testing.B) {
var mc, _ = ristretto.NewCache(&ristretto.Config{
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.
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.
})
for i := 0; i < benchcount; i++ {
mc.SetWithTTL(benchkeys[i%benchcount], 1, 1, time.Hour)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
var i = 0
for pb.Next() {
index := i % benchcount
i++
Expand All @@ -121,17 +118,17 @@ func BenchmarkRistretto_Get(b *testing.B) {

func BenchmarkRistretto_SetAndGet(b *testing.B) {
var mc, _ = ristretto.NewCache(&ristretto.Config{
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.
NumCounters: 10000 * 128 * 10, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // 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) {
i := 0
var i = 0
for pb.Next() {
index := i % benchcount
i++
Expand All @@ -145,7 +142,7 @@ func BenchmarkRistretto_SetAndGet(b *testing.B) {
}

func BenchmarkOtter_Set(b *testing.B) {
var mc, _ = otter.MustBuilder[string, int](capacity).Build()
var mc, _ = otter.MustBuilder[string, int](10000 * 128).Build()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
Expand All @@ -157,7 +154,7 @@ func BenchmarkOtter_Set(b *testing.B) {
}

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

func BenchmarkOtter_SetAndGet(b *testing.B) {
mc, _ := otter.MustBuilder[string, int](capacity).Build()
mc, _ := otter.MustBuilder[string, int](10000 * 128).Build()
for i := 0; i < benchcount; i++ {
mc.SetWithTTL(benchkeys[i%benchcount], 1, time.Hour)
}
Expand Down
7 changes: 3 additions & 4 deletions benchmark/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/dgraph-io/ristretto v0.1.1
github.com/lxzan/memorycache v1.0.0
github.com/maypok86/otter v0.0.0-20231202212625-970153160a38
)

require (
Expand All @@ -14,12 +15,10 @@ require (
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/klauspost/cpuid/v2 v2.2.6 // 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
golang.org/x/sys v0.15.0 // indirect
)

replace github.com/lxzan/memorycache v1.0.0 => ../
20 changes: 8 additions & 12 deletions benchmark/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,24 @@ 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/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/maypok86/otter v0.0.0-20231202212625-970153160a38 h1:acEgJlZFBOS29Ie5WRJKRo13SKi0jjcK5Ce+P1ZmcYY=
github.com/maypok86/otter v0.0.0-20231202212625-970153160a38/go.mod h1:v+Q8JyiI0/ShqyWInxumE3YjsT1ZacCnQ3x7FX4in20=
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=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
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=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 1cb66a3

Please sign in to comment.