Skip to content

Commit

Permalink
Merge pull request #86 from ianferguson/ianferguson/maxsizestat
Browse files Browse the repository at this point in the history
Max Size MRU/MFU stats/Add go modules
  • Loading branch information
jamiealquiza authored Mar 4, 2021
2 parents db3f720 + 6afb4a4 commit 7ec9c35
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 158 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
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/jamiealquiza/bicache

go 1.15

require (
github.com/jamiealquiza/fnv v1.0.0
github.com/jamiealquiza/tachymeter v2.0.0+incompatible
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/jamiealquiza/fnv v1.0.0 h1:4NwlkaoZiLhqk008EY5+MTGVPRQZgRG/6B7+jN7ueT8=
github.com/jamiealquiza/fnv v1.0.0/go.mod h1:iJRnFlvFvZpWKZd+KljYXcyQLasMIKAVuQhx63P4DUk=
github.com/jamiealquiza/tachymeter v2.0.0+incompatible h1:mGiF1DGo8l6vnGT8FXNNcIXht/YmjzfraiUprXYwJ6g=
github.com/jamiealquiza/tachymeter v2.0.0+incompatible/go.mod h1:Ayf6zPZKEnLsc3winWEXJRkTBhdHo58HODAu1oFJkYU=
21 changes: 0 additions & 21 deletions vendor/github.com/jamiealquiza/fnv/LICENSE

This file was deleted.

13 changes: 0 additions & 13 deletions vendor/github.com/jamiealquiza/fnv/README.md

This file was deleted.

58 changes: 0 additions & 58 deletions vendor/github.com/jamiealquiza/fnv/fnv.go

This file was deleted.

57 changes: 0 additions & 57 deletions vendor/github.com/jamiealquiza/fnv/fnv_test.go

This file was deleted.

0 comments on commit 7ec9c35

Please sign in to comment.