This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
caching support for collector plugins
This commit adds support for caching collections for a default amount of time (500ms).
- Loading branch information
Showing
6 changed files
with
254 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/intelsdi-x/pulse/core" | ||
) | ||
|
||
var ( | ||
cacheExpiration = time.Duration(500 * time.Millisecond) | ||
metricCache = cache{ | ||
table: make(map[string]*cachecell), | ||
} | ||
cacheLog = log.WithField("_module", "client-cache") | ||
) | ||
|
||
type cachecell struct { | ||
time time.Time | ||
metric core.Metric | ||
hits uint64 | ||
misses uint64 | ||
} | ||
|
||
type cache struct { | ||
table map[string]*cachecell | ||
} | ||
|
||
func (c *cache) get(key string) core.Metric { | ||
var ( | ||
cell *cachecell | ||
ok bool | ||
) | ||
if cell, ok = c.table[key]; ok && time.Since(cell.time) < cacheExpiration { | ||
cell.hits++ | ||
cacheLog.WithFields(log.Fields{ | ||
"namespace": key, | ||
"hits": cell.hits, | ||
"misses": cell.misses, | ||
}).Debug(fmt.Sprintf("cache hit [%s]", key)) | ||
return cell.metric | ||
} | ||
if ok { | ||
cell.misses++ | ||
cacheLog.WithFields(log.Fields{ | ||
"namespace": key, | ||
"hits": cell.hits, | ||
"misses": cell.misses, | ||
}).Debug(fmt.Sprintf("cache miss [%s]", key)) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *cache) put(key string, metric core.Metric) { | ||
if _, ok := c.table[key]; ok { | ||
c.table[key].time = time.Now() | ||
c.table[key].metric = metric | ||
} else { | ||
c.table[key] = &cachecell{ | ||
time: time.Now(), | ||
metric: metric, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package client | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
|
||
"github.com/intelsdi-x/pulse/control/plugin" | ||
) | ||
|
||
func TestCache(t *testing.T) { | ||
Convey("puts and gets a metric", t, func() { | ||
mc := &cache{ | ||
table: make(map[string]*cachecell), | ||
} | ||
foo := &plugin.PluginMetricType{ | ||
Namespace_: []string{"foo", "bar"}, | ||
} | ||
mc.put("/foo/bar", foo) | ||
ret := mc.get("/foo/bar") | ||
So(ret, ShouldNotBeNil) | ||
So(ret, ShouldEqual, foo) | ||
}) | ||
Convey("returns nil if the cache cell does not exist", t, func() { | ||
mc := &cache{ | ||
table: make(map[string]*cachecell), | ||
} | ||
ret := mc.get("/foo/bar") | ||
So(ret, ShouldBeNil) | ||
}) | ||
Convey("returns nil if the cache cell has expired", t, func() { | ||
mc := &cache{ | ||
table: make(map[string]*cachecell), | ||
} | ||
foo := &plugin.PluginMetricType{ | ||
Namespace_: []string{"foo", "bar"}, | ||
} | ||
mc.put("/foo/bar", foo) | ||
time.Sleep(501 * time.Millisecond) | ||
ret := mc.get("/foo/bar") | ||
So(ret, ShouldBeNil) | ||
}) | ||
Convey("hit and miss counts", t, func() { | ||
Convey("ticks hit count when a cache entry is hit", func() { | ||
mc := &cache{ | ||
table: make(map[string]*cachecell), | ||
} | ||
foo := &plugin.PluginMetricType{ | ||
Namespace_: []string{"foo", "bar"}, | ||
} | ||
mc.put("/foo/bar", foo) | ||
mc.get("/foo/bar") | ||
So(mc.table["/foo/bar"].hits, ShouldEqual, 1) | ||
}) | ||
Convey("ticks miss count when a cache entry is missed", func() { | ||
mc := &cache{ | ||
table: make(map[string]*cachecell), | ||
} | ||
foo := &plugin.PluginMetricType{ | ||
Namespace_: []string{"foo", "bar"}, | ||
} | ||
mc.put("/foo/bar", foo) | ||
time.Sleep(501 * time.Millisecond) | ||
mc.get("/foo/bar") | ||
So(mc.table["/foo/bar"].misses, ShouldEqual, 1) | ||
}) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.