sort-cli: add benchmark

This commit is contained in:
2024-02-03 17:47:01 +03:00
parent a1e778afe3
commit d1cb8beba3

View File

@ -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))
@ -76,3 +77,23 @@ func TestDummy(t *testing.T) { // for coverage 100% coverage
cache := New[int](0)
cache.evict()
}
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
}
}
})
}