Skip to content

Commit

Permalink
Add Len field to Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Mar 25, 2024
1 parent d7a51bd commit 6034503
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
14 changes: 12 additions & 2 deletions lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Metrics struct {
Misses uint64
Capacity uint32
Lifetime string
Len int
}

var _ Cache[int, int] = (*LRU[int, int])(nil)
Expand Down Expand Up @@ -148,7 +149,9 @@ func initLRU[K comparable, V any](lru *LRU[K, V], capacity, size uint32, hash Ha
lru.hash = hash
lru.buckets = buckets
lru.elements = elements
lru.lifetime = 0
lru.metrics.Capacity = capacity
lru.metrics.Lifetime = lru.lifetime.String()

// If the size is 2^N, we can avoid costly divisions.
if bits.OnesCount32(lru.size) == 1 {
Expand Down Expand Up @@ -506,18 +509,25 @@ func (lru *LRU[K, V]) Purge() {
}

lru.len = 0
lru.metrics = Metrics{}
lru.metrics = Metrics{
Capacity: lru.cap,
Lifetime: lru.lifetime.String(),
}
}

// Metrics returns the metrics of the cache.
func (lru *LRU[K, V]) Metrics() Metrics {
lru.metrics.Len = lru.Len()
return lru.metrics
}

// ResetMetrics resets the metrics of the cache and returns the previous state.
func (lru *LRU[K, V]) ResetMetrics() Metrics {
metrics := lru.metrics
lru.metrics = Metrics{}
lru.metrics = Metrics{
Capacity: lru.cap,
Lifetime: lru.lifetime.String(),
}
return metrics
}

Expand Down
24 changes: 23 additions & 1 deletion lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ func TestLRUMetrics(t *testing.T) {
m := lru.Metrics()
FatalIf(t, m.Capacity != 3, "Unexpected capacity: %d (!= %d)", m.Capacity, 3)
FatalIf(t, m.Lifetime != "3s", "Unexpected lifetime: %s (!= %s)", m.Lifetime, "3s")

lru.ResetMetrics()
m = lru.Metrics()
FatalIf(t, m.Capacity != 3, "Unexpected capacity: %d (!= %d)", m.Capacity, 3)
FatalIf(t, m.Lifetime != "3s", "Unexpected lifetime: %s (!= %s)", m.Lifetime, "3s")
}

func testMetrics(t *testing.T, cache Cache[uint64, uint64]) {
Expand All @@ -342,5 +347,22 @@ func testMetrics(t *testing.T, cache Cache[uint64, uint64]) {
FatalIf(t, m.Removals != 1, "Unexpected evictions: %d (!= %d)", m.Removals, 1)
FatalIf(t, m.Collisions != 0, "Unexpected collisions: %d (!= %d)", m.Collisions, 0)
FatalIf(t, m.Capacity != 1, "Unexpected capacity: %d (!= %d)", m.Capacity, 1)
FatalIf(t, m.Lifetime != "", "Unexpected lifetime: %s (!= %s)", m.Lifetime, "")
FatalIf(t, m.Lifetime != "0s", "Unexpected lifetime: %s (!= %s)", m.Lifetime, "0s")
FatalIf(t, m.Len != 0, "Unexpected len: %d (!= %d)", m.Len, 0)

cache.Add(4, 4)
m = cache.Metrics()
FatalIf(t, m.Len != 1, "Unexpected len: %d (!= %d)", m.Len, 1)

cache.Purge()
m = cache.Metrics()
FatalIf(t, m.Inserts != 0, "Unexpected inserts: %d (!= %d)", m.Inserts, 0)
FatalIf(t, m.Hits != 0, "Unexpected hits: %d (!= %d)", m.Hits, 0)
FatalIf(t, m.Misses != 0, "Unexpected misses: %d (!= %d)", m.Misses, 0)
FatalIf(t, m.Evictions != 0, "Unexpected evictions: %d (!= %d)", m.Evictions, 0)
FatalIf(t, m.Removals != 0, "Unexpected evictions: %d (!= %d)", m.Removals, 0)
FatalIf(t, m.Collisions != 0, "Unexpected collisions: %d (!= %d)", m.Collisions, 0)
FatalIf(t, m.Capacity != 1, "Unexpected capacity: %d (!= %d)", m.Capacity, 1)
FatalIf(t, m.Lifetime != "0s", "Unexpected lifetime: %s (!= %s)", m.Lifetime, "0s")
FatalIf(t, m.Len != 0, "Unexpected len: %d (!= %d)", m.Len, 0)
}
1 change: 1 addition & 0 deletions shardedlru.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func addMetrics(dst *Metrics, src Metrics) {
dst.Misses += src.Misses
dst.Capacity += src.Capacity
dst.Lifetime = src.Lifetime
dst.Len += src.Len
}

// just used for debugging
Expand Down
5 changes: 5 additions & 0 deletions shardedlru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func TestShardedLRUMetrics(t *testing.T) {
m := lru.Metrics()
FatalIf(t, m.Capacity != 7, "Unexpected capacity: %d (!= %d)", m.Capacity, 7)
FatalIf(t, m.Lifetime != "7s", "Unexpected lifetime: %s (!= %s)", m.Lifetime, "7s")

lru.ResetMetrics()
m = lru.Metrics()
FatalIf(t, m.Capacity != 7, "Unexpected capacity: %d (!= %d)", m.Capacity, 7)
FatalIf(t, m.Lifetime != "7s", "Unexpected lifetime: %s (!= %s)", m.Lifetime, "7s")
}

func TestStressWithLifetime(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions syncedlru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ func TestSyncedLRUMetrics(t *testing.T) {
m := lru.Metrics()
FatalIf(t, m.Capacity != 5, "Unexpected capacity: %d (!= %d)", m.Capacity, 5)
FatalIf(t, m.Lifetime != "5s", "Unexpected lifetime: %s (!= %s)", m.Lifetime, "5s")

lru.ResetMetrics()
m = lru.Metrics()
FatalIf(t, m.Capacity != 5, "Unexpected capacity: %d (!= %d)", m.Capacity, 5)
FatalIf(t, m.Lifetime != "5s", "Unexpected lifetime: %s (!= %s)", m.Lifetime, "5s")
}

0 comments on commit 6034503

Please sign in to comment.