Skip to content

Commit

Permalink
Include max mru and mfu size in stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ianferguson committed Mar 1, 2021
1 parent 7939c66 commit 6afb4a4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
21 changes: 13 additions & 8 deletions bicache.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@ type cacheData struct {
// Stats holds Bicache
// statistics data.
type Stats struct {
MFUSize uint // Number of acive MFU keys.
MRUSize uint // Number of active MRU keys.
MFUUsedP uint // MFU used in percent.
MRUUsedP uint // MRU used in percent.
Hits uint64 // Cache hits.
Misses uint64 // Cache misses.
Evictions uint64 // Cache evictions.
Overflows uint64 // Failed sets on full caches.
MFUSize uint // Number of active MFU keys.
MRUSize uint // Number of active MRU keys.
MFUUsedP uint // MFU used in percent.
MRUUsedP uint // MRU used in percent.
MFUMaxSize uint // Maximum number of MFU keys.
MRUMaxSize uint // Maximum number of MRU keys.
Hits uint64 // Cache hits.
Misses uint64 // Cache misses.
Evictions uint64 // Cache evictions.
Overflows uint64 // Failed sets on full caches.
}

// New takes a *Config and returns
Expand Down Expand Up @@ -271,6 +273,9 @@ func (b *Bicache) Stats() *Stats {
stats.Overflows += atomic.LoadUint64(&s.counters.overflows)
}

stats.MFUMaxSize = uint(mfuCap)
stats.MRUMaxSize = uint(mruCap)

stats.MRUUsedP = uint(float64(stats.MRUSize) / mruCap * 100)
// Prevent incorrect stats in MRU-only mode.
if mfuCap > 0 {
Expand Down
52 changes: 51 additions & 1 deletion bicache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,48 @@ func TestStats(t *testing.T) {
AutoEvict: 3000,
})

stats := c.Stats()

if stats.MFUSize != 0 {
t.Errorf("Expected MFU size 0, got %d", stats.MFUSize)
}

if stats.MRUSize != 0 {
t.Errorf("Expected MRU size 0, got %d", stats.MRUSize)
}

if stats.MFUMaxSize != 10 {
t.Errorf("Expected MFUMaxSize 10, got %d", stats.MFUSize)
}

if stats.MRUMaxSize != 30 {
t.Errorf("Expected MRUMaxSize 30, got %d", stats.MRUSize)
}

if stats.MFUUsedP != 0 {
t.Errorf("Expected MFU usedp 0, got %d", stats.MFUUsedP)
}

if stats.MRUUsedP != 0 {
t.Errorf("Expected MRU usedp 0, got %d", stats.MRUUsedP)
}

if stats.Hits != 0 {
t.Errorf("Expected 0 hits, got %d", stats.Hits)
}

if stats.Misses != 0 {
t.Errorf("Expected 0 misses, got %d", stats.Misses)
}

if stats.Evictions != 0 {
t.Errorf("Expected 0 evictions, got %d", stats.Evictions)
}

if stats.Overflows != 0 {
t.Errorf("Expected 0 overflows, got %d", stats.Overflows)
}

for i := 0; i < 50; i++ {
c.Set(strconv.Itoa(i), "value")
c.Get(strconv.Itoa(i))
Expand All @@ -51,7 +93,7 @@ func TestStats(t *testing.T) {
log.Printf("Sleeping for 4 seconds to allow evictions")
time.Sleep(4 * time.Second)

stats := c.Stats()
stats = c.Stats()

if stats.MFUSize != 10 {
t.Errorf("Expected MFU size 10, got %d", stats.MFUSize)
Expand All @@ -61,6 +103,14 @@ func TestStats(t *testing.T) {
t.Errorf("Expected MRU size 30, got %d", stats.MRUSize)
}

if stats.MFUMaxSize != 10 {
t.Errorf("Expected MFUMaxSize 10, got %d", stats.MFUSize)
}

if stats.MRUMaxSize != 30 {
t.Errorf("Expected MRUMaxSize 30, got %d", stats.MRUSize)
}

if stats.MFUUsedP != 100 {
t.Errorf("Expected MFU usedp 100, got %d", stats.MFUUsedP)
}
Expand Down

0 comments on commit 6afb4a4

Please sign in to comment.