From 7939c664f07e21705eb244158f16f4aa08f0d37d Mon Sep 17 00:00:00 2001 From: Ian Ferguson Date: Mon, 1 Mar 2021 12:55:04 -0500 Subject: [PATCH 1/2] Set up go modules --- go.mod | 8 +++ go.sum | 4 ++ vendor/github.com/jamiealquiza/fnv/LICENSE | 21 ------- vendor/github.com/jamiealquiza/fnv/README.md | 13 ----- vendor/github.com/jamiealquiza/fnv/fnv.go | 58 ------------------- .../github.com/jamiealquiza/fnv/fnv_test.go | 57 ------------------ 6 files changed, 12 insertions(+), 149 deletions(-) create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 vendor/github.com/jamiealquiza/fnv/LICENSE delete mode 100644 vendor/github.com/jamiealquiza/fnv/README.md delete mode 100644 vendor/github.com/jamiealquiza/fnv/fnv.go delete mode 100644 vendor/github.com/jamiealquiza/fnv/fnv_test.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8e5e216 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6d4cdb5 --- /dev/null +++ b/go.sum @@ -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= diff --git a/vendor/github.com/jamiealquiza/fnv/LICENSE b/vendor/github.com/jamiealquiza/fnv/LICENSE deleted file mode 100644 index 40845c1..0000000 --- a/vendor/github.com/jamiealquiza/fnv/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2017 Jamie Alquiza - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/vendor/github.com/jamiealquiza/fnv/README.md b/vendor/github.com/jamiealquiza/fnv/README.md deleted file mode 100644 index 45fc6de..0000000 --- a/vendor/github.com/jamiealquiza/fnv/README.md +++ /dev/null @@ -1,13 +0,0 @@ -[![GoDoc](https://godoc.org/github.com/jamiealquiza/fnv?status.svg)](https://godoc.org/github.com/jamiealquiza/fnv) - -# fnv - -Package fnv implements allocation-free 32 and 64 bit FNV-1 hash variants. - -``` -% go test -bench=. -benchmem -BenchmarkHash32a-4 300000000 4.85 ns/op 0 B/op 0 allocs/op -BenchmarkHash32-4 300000000 4.78 ns/op 0 B/op 0 allocs/op -BenchmarkHash64a-4 300000000 6.01 ns/op 0 B/op 0 allocs/op -BenchmarkHash64-4 300000000 5.18 ns/op 0 B/op 0 allocs/op -``` diff --git a/vendor/github.com/jamiealquiza/fnv/fnv.go b/vendor/github.com/jamiealquiza/fnv/fnv.go deleted file mode 100644 index 08158ca..0000000 --- a/vendor/github.com/jamiealquiza/fnv/fnv.go +++ /dev/null @@ -1,58 +0,0 @@ -// Package fnv implements allocation-free -// 32 and 64 bit FNV-1 hash variants. -package fnv - -const ( - offset32 = 0x811c9dc5 - prime32 = 0x1000193 - offset64 = 0xcbf29ce484222325 - prime64 = 0x100000001b3 -) - -// Hash32a takes a string and -// returns a 32 bit FNV-1a. -func Hash32a(s string) uint32 { - var h uint32 = offset32 - for _, c := range []byte(s) { - h ^= uint32(c) - h *= prime32 - } - - return h -} - -// Hash32 takes a string and -// returns a 32 bit FNV-1. -func Hash32(s string) uint32 { - var h uint32 = offset32 - for _, c := range []byte(s) { - h *= prime32 - h ^= uint32(c) - } - - return h -} - -// Hash64a takes a string and -// returns a 64 bit FNV-1a. -func Hash64a(s string) uint64 { - var h uint64 = offset64 - for _, c := range []byte(s) { - h ^= uint64(c) - h *= prime64 - } - - return h -} - -// Hash64 takes a string and -// returns a 64 bit FNV-1. -func Hash64(s string) uint64 { - var h uint64 = offset64 - for _, c := range []byte(s) { - h *= prime64 - h ^= uint64(c) - } - - return h -} diff --git a/vendor/github.com/jamiealquiza/fnv/fnv_test.go b/vendor/github.com/jamiealquiza/fnv/fnv_test.go deleted file mode 100644 index 658fd20..0000000 --- a/vendor/github.com/jamiealquiza/fnv/fnv_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package fnv - -import ( - "strconv" - "testing" -) - -// This needs to be replaced with -// better data. - -func BenchmarkHash32a(b *testing.B) { - d := make([]string, 1024) - for i := range d { - d[i] = strconv.Itoa(i) - } - - b.ResetTimer() - for i := 0; i < b.N; i++ { - Hash32a(d[i%1024]) - } -} - -func BenchmarkHash32(b *testing.B) { - d := make([]string, 1024) - for i := range d { - d[i] = strconv.Itoa(i) - } - - b.ResetTimer() - for i := 0; i < b.N; i++ { - Hash32(d[i%1024]) - } -} - -func BenchmarkHash64a(b *testing.B) { - d := make([]string, 1024) - for i := range d { - d[i] = strconv.Itoa(i) - } - - b.ResetTimer() - for i := 0; i < b.N; i++ { - Hash64a(d[i%1024]) - } -} - -func BenchmarkHash64(b *testing.B) { - d := make([]string, 1024) - for i := range d { - d[i] = strconv.Itoa(i) - } - - b.ResetTimer() - for i := 0; i < b.N; i++ { - Hash64(d[i%1024]) - } -} From 6afb4a42d8450d2d4c75bba783c17655e57b5db0 Mon Sep 17 00:00:00 2001 From: Ian Ferguson Date: Mon, 1 Mar 2021 12:55:34 -0500 Subject: [PATCH 2/2] Include max mru and mfu size in stats --- bicache.go | 21 ++++++++++++-------- bicache_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/bicache.go b/bicache.go index 2ff0c89..7f9a6bf 100644 --- a/bicache.go +++ b/bicache.go @@ -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 @@ -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 { diff --git a/bicache_test.go b/bicache_test.go index c58b851..8ceb094 100644 --- a/bicache_test.go +++ b/bicache_test.go @@ -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)) @@ -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) @@ -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) }