Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
PIP-1490: Remove SyncLRUCache. No longer needed.
Browse files Browse the repository at this point in the history
Use `LRUCache` for everything.
  • Loading branch information
Baliedge committed Jan 19, 2022
1 parent 5034cb7 commit 86d4a0a
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 577 deletions.
167 changes: 167 additions & 0 deletions benchmark_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package gubernator_test

import (
"strconv"
"sync"
"testing"
"time"

"github.com/mailgun/gubernator/v2"
"github.com/mailgun/holster/v4/clock"
)

func BenchmarkCache(b *testing.B) {
testCases := []struct{
Name string
NewTestCache func() gubernator.Cache
LockRequired bool
}{
{
Name: "LRUCache",
NewTestCache: func() gubernator.Cache{
return gubernator.NewLRUCache(0)
},
LockRequired: true,
},
// {
// Name: "SyncLRUCache",
// NewTestCache: func() gubernator.Cache{
// return gubernator.NewSyncLRUCache(0)
// },
// LockRequired: false,
// },
}

for _, testCase := range testCases {
b.Run(testCase.Name, func(b *testing.B) {
b.Run("Sequential reads", func(b *testing.B) {
cache := testCase.NewTestCache()
expire := clock.Now().Add(time.Hour).UnixMilli()

for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
item := &gubernator.CacheItem{
Key: key,
Value: i,
ExpireAt: expire,
}
cache.Add(item)
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
_, _ = cache.GetItem(key)
}
})

b.Run("Sequential writes", func(b *testing.B) {
cache := testCase.NewTestCache()
expire := clock.Now().Add(time.Hour).UnixMilli()

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
item := &gubernator.CacheItem{
Key: strconv.Itoa(i),
Value: i,
ExpireAt: expire,
}
cache.Add(item)
}
})

b.Run("Concurrent reads", func(b *testing.B) {
cache := testCase.NewTestCache()
expire := clock.Now().Add(time.Hour).UnixMilli()

for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
item := &gubernator.CacheItem{
Key: key,
Value: i,
ExpireAt: expire,
}
cache.Add(item)
}

var wg sync.WaitGroup
var mutex sync.Mutex
var task func(i int)

if testCase.LockRequired {
task = func(i int) {
mutex.Lock()
defer mutex.Unlock()
key := strconv.Itoa(i)
_, _ = cache.GetItem(key)
wg.Done()
}
} else {
task = func(i int) {
key := strconv.Itoa(i)
_, _ = cache.GetItem(key)
wg.Done()
}
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
wg.Add(1)
go task(i)
}

wg.Wait()
})

b.Run("Concurrent writes", func(b *testing.B) {
cache := testCase.NewTestCache()
expire := clock.Now().Add(time.Hour).UnixMilli()

var wg sync.WaitGroup
var mutex sync.Mutex
var task func(i int)

if testCase.LockRequired {
task = func(i int) {
mutex.Lock()
defer mutex.Unlock()
item := &gubernator.CacheItem{
Key: strconv.Itoa(i),
Value: i,
ExpireAt: expire,
}
cache.Add(item)
wg.Done()
}
} else {
task = func(i int) {
item := &gubernator.CacheItem{
Key: strconv.Itoa(i),
Value: i,
ExpireAt: expire,
}
cache.Add(item)
wg.Done()
}
}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
wg.Add(1)
go task(i)
}

wg.Wait()
})

})
}
}
2 changes: 1 addition & 1 deletion daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s *Daemon) Start(ctx context.Context) error {
s.promRegister.Register(cacheCollector)

cacheFactory := func(maxSize int) Cache {
cache := NewSyncLRUCache(maxSize)
cache := NewLRUCache(maxSize)
cacheCollector.AddCache(cache)
return cache
}
Expand Down
171 changes: 0 additions & 171 deletions sync_lrucache.go

This file was deleted.

Loading

0 comments on commit 86d4a0a

Please sign in to comment.