From 472eb6f6f5326581a5d316f8f3fe4a507869316d Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 7 Sep 2023 09:31:33 -0700 Subject: [PATCH] util/lru: add a microbenchmark The benchmark simulates an LRU being queries with uniformly random inputs, in a set that's too large for the LRU, which should stress the eviction codepath. Signed-off-by: David Anderson --- util/lru/lru_test.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/util/lru/lru_test.go b/util/lru/lru_test.go index 315c86218..cdc169f57 100644 --- a/util/lru/lru_test.go +++ b/util/lru/lru_test.go @@ -3,7 +3,10 @@ package lru -import "testing" +import ( + "math/rand" + "testing" +) func TestLRU(t *testing.T) { var c Cache[int, string] @@ -40,3 +43,17 @@ func TestLRU(t *testing.T) { t.Errorf("contains 3; should not") } } + +func BenchmarkLRU(b *testing.B) { + const lruSize = 10 + const maxval = 15 // 33% more keys than the LRU can hold + + c := Cache[int, bool]{MaxEntries: lruSize} + b.ReportAllocs() + for i := 0; i < b.N; i++ { + k := rand.Intn(maxval) + if !c.Get(k) { + c.Set(k, true) + } + } +}