diff --git a/lru/lru_test.go b/lru/lru_test.go index 6403db6..97e702e 100644 --- a/lru/lru_test.go +++ b/lru/lru_test.go @@ -1,6 +1,7 @@ package lru import ( + "fmt" "github.com/stretchr/testify/assert" "testing" "time" @@ -39,7 +40,7 @@ func TestEvictExpired(t *testing.T) { assert.False(t, cache.Set("first", 1, 1000*time.Millisecond)) assert.False(t, cache.Set("second", 2, 100*time.Millisecond)) - time.Sleep(200*time.Millisecond) + time.Sleep(200 * time.Millisecond) assert.True(t, cache.Set("third", 3, 1000*time.Millisecond)) @@ -58,7 +59,7 @@ func TestSetExisted(t *testing.T) { assert.False(t, cache.Set("first", 1, 200*time.Millisecond)) assert.False(t, cache.Set("second", 2, 500*time.Millisecond)) - time.Sleep(100*time.Millisecond) + time.Sleep(100 * time.Millisecond) assert.False(t, cache.Set("first", 11, 1000*time.Millisecond)) assert.True(t, cache.Set("third", 3, 1000*time.Millisecond)) @@ -72,7 +73,28 @@ func TestSetExisted(t *testing.T) { assert.False(t, ok) } -func TestDummy(t *testing.T) { // for coverage 100% coverage +func TestDummy(t *testing.T) { // for coverage 100% coverage cache := New[int](0) cache.evict() } + +// We will be storing many short strings as the key and value +func Benchmark(b *testing.B) { + cache := New[string](10) + ttl := 1000 * time.Millisecond + + b.Run("Set", func(b *testing.B) { + for i := 0; i < b.N; i++ { + cache.Set(fmt.Sprintf("item:%d", i), fmt.Sprintf("item:%d", i), ttl) + } + }) + + b.Run("Get", func(b *testing.B) { + for i := 0; i < b.N; i++ { + value, ok := cache.Get(fmt.Sprintf("item:%d", i)) + if ok { + _ = value + } + } + }) +}