From 60345033fb725882b1f54b49af16f1c396cc2abc Mon Sep 17 00:00:00 2001 From: Fufu Date: Mon, 25 Mar 2024 08:56:47 +0800 Subject: [PATCH] Add Len field to Metrics --- lru.go | 14 ++++++++++++-- lru_test.go | 24 +++++++++++++++++++++++- shardedlru.go | 1 + shardedlru_test.go | 5 +++++ syncedlru_test.go | 5 +++++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lru.go b/lru.go index 17b4402..451e115 100644 --- a/lru.go +++ b/lru.go @@ -89,6 +89,7 @@ type Metrics struct { Misses uint64 Capacity uint32 Lifetime string + Len int } var _ Cache[int, int] = (*LRU[int, int])(nil) @@ -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 { @@ -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 } diff --git a/lru_test.go b/lru_test.go index 6bdae31..a4e2c4d 100644 --- a/lru_test.go +++ b/lru_test.go @@ -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]) { @@ -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) } diff --git a/shardedlru.go b/shardedlru.go index 34a3f52..f415289 100644 --- a/shardedlru.go +++ b/shardedlru.go @@ -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 diff --git a/shardedlru_test.go b/shardedlru_test.go index fba578e..e0f2103 100644 --- a/shardedlru_test.go +++ b/shardedlru_test.go @@ -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) { diff --git a/syncedlru_test.go b/syncedlru_test.go index ee40306..8a66050 100644 --- a/syncedlru_test.go +++ b/syncedlru_test.go @@ -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") }