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

feat: Add a cache to the cache #1296

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Changes from 1 commit
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
64 changes: 64 additions & 0 deletions generics/setttl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ import (
"testing"
"time"

"github.com/dgryski/go-wyhash"
"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/assert"
)

var seed = 3565269841805
var rng = wyhash.Rng(seed)

const charset = "abcdef0123456789"

func genID(numChars int) string {

id := make([]byte, numChars)
for i := 0; i < numChars; i++ {
id[i] = charset[int(rng.Next()%uint64(len(charset)))]
}
return string(id)
}

func TestSetTTLBasics(t *testing.T) {
s := NewSetWithTTL(100*time.Millisecond, "a", "b", "b")
fakeclock := clockwork.NewFakeClock()
Expand All @@ -24,3 +39,52 @@ func TestSetTTLBasics(t *testing.T) {
assert.Equal(t, 0, s.Length())
assert.Equal(t, s.Members(), []string{})
}

func BenchmarkSetWithTTLContains(b *testing.B) {
s := NewSetWithTTL[string](10 * time.Second)
fc := clockwork.NewFakeClock()
s.Clock = fc

n := 10000
traceIDs := make([]string, n)
for i := 0; i < n; i++ {
traceIDs[i] = genID(32)
if i%2 == 0 {
s.Add(traceIDs[i])
}
fc.Advance(1 * time.Microsecond)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Contains(traceIDs[i%n])
}
}

func BenchmarkSetWithTTLExpire(b *testing.B) {
s := NewSetWithTTL[string](1 * time.Second)
fc := clockwork.NewFakeClock()
s.Clock = fc

// 1K ids created at 1ms intervals
// we'll check them over the course of 1 second as well, so they should all expire by the end
n := 1000
traceIDs := make([]string, n)
for i := 0; i < n; i++ {
traceIDs[i] = genID(32)
s.Add(traceIDs[i])
fc.Advance(1 * time.Millisecond)
}
// make sure we have 1000 ids now
assert.Equal(b, n, s.Length())
b.ResetTimer()
advanceTime := 100 * time.Second / time.Duration(b.N)
for i := 0; i < b.N; i++ {
s.Contains(traceIDs[i%n])
if i%100 == 0 {
fc.Advance(advanceTime)
}
}
b.StopTimer()
// make sure all ids have expired by now (there might be 1 or 2 that haven't)
assert.GreaterOrEqual(b, 2, s.Length())
kentquirk marked this conversation as resolved.
Show resolved Hide resolved
}