From 3bae47fc22680c07ff5a9764061b77333a66fc42 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Thu, 9 Mar 2017 22:49:05 +0100 Subject: [PATCH] hit/miss stats for matchcache --- mdata/init.go | 4 ++-- mdata/matchcache/matchcache.go | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/mdata/init.go b/mdata/init.go index 388fba630e..054ff32a9c 100644 --- a/mdata/init.go +++ b/mdata/init.go @@ -91,6 +91,6 @@ func ConfigProcess() { } func Cache(cleanInterval, expireAfter time.Duration) { - schemasCache = matchcache.New(cleanInterval, expireAfter) - aggsCache = matchcache.New(cleanInterval, expireAfter) + schemasCache = matchcache.New("schemas", cleanInterval, expireAfter) + aggsCache = matchcache.New("aggs", cleanInterval, expireAfter) } diff --git a/mdata/matchcache/matchcache.go b/mdata/matchcache/matchcache.go index 3805d32a23..62663db7a7 100644 --- a/mdata/matchcache/matchcache.go +++ b/mdata/matchcache/matchcache.go @@ -1,8 +1,11 @@ package matchcache import ( + "fmt" "sync" "time" + + "github.com/raintank/metrictank/stats" ) // Cache caches key to uint16 lookups (for schemas and aggregations) @@ -15,6 +18,8 @@ type Cache struct { cleanInterval time.Duration expireAfter time.Duration + hits *stats.Counter32 + miss *stats.Counter32 } type Item struct { @@ -22,11 +27,13 @@ type Item struct { seen int64 } -func New(cleanInterval, expireAfter time.Duration) *Cache { +func New(name string, cleanInterval, expireAfter time.Duration) *Cache { m := &Cache{ data: make(map[string]Item), cleanInterval: cleanInterval, expireAfter: expireAfter, + hits: stats.NewCounter32(fmt.Sprintf("idx.matchcache.%s.ops.hit", name)), + miss: stats.NewCounter32(fmt.Sprintf("idx.matchcache.%s.ops.miss", name)), } go m.maintain() return m @@ -38,7 +45,10 @@ type AddFunc func(key string) uint16 func (m *Cache) Get(key string, fn AddFunc) uint16 { m.Lock() item, ok := m.data[key] - if !ok { + if ok { + m.hits.Inc() + } else { + m.miss.Inc() item.val = fn(key) } item.seen = time.Now().Unix()