mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-19 05:19:35 +00:00
Review feedback
This commit is contained in:
@@ -20,7 +20,7 @@ type Walker interface {
|
||||
// CachingWalker is a walker than caches a copy of the output from another
|
||||
// Walker, and then allows other concurrent readers to Walk that copy.
|
||||
type CachingWalker struct {
|
||||
cache []Process
|
||||
cache map[int]Process
|
||||
previousByPID map[int]Process
|
||||
cacheLock sync.RWMutex
|
||||
source Walker
|
||||
@@ -47,9 +47,9 @@ func (c *CachingWalker) Walk(f func(Process, Process)) error {
|
||||
|
||||
// Tick updates cached copy of process list
|
||||
func (c *CachingWalker) Tick() error {
|
||||
newCache := []Process{}
|
||||
newCache := map[int]Process{}
|
||||
err := c.source.Walk(func(p, _ Process) {
|
||||
newCache = append(newCache, p)
|
||||
newCache[p.PID] = p
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -57,10 +57,7 @@ func (c *CachingWalker) Tick() error {
|
||||
|
||||
c.cacheLock.Lock()
|
||||
defer c.cacheLock.Unlock()
|
||||
c.previousByPID = map[int]Process{}
|
||||
for _, p := range c.cache {
|
||||
c.previousByPID[p.PID] = p
|
||||
}
|
||||
c.previousByPID = c.cache
|
||||
c.cache = newCache
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -34,15 +34,16 @@ func TestCache(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
want, err := all(walker)
|
||||
have, err := all(cachingWalker)
|
||||
if err != nil || !reflect.DeepEqual(processes, have) {
|
||||
t.Errorf("%v (%v)", test.Diff(processes, have), err)
|
||||
if err != nil || !reflect.DeepEqual(want, have) {
|
||||
t.Errorf("%v (%v)", test.Diff(want, have), err)
|
||||
}
|
||||
|
||||
walker.processes = []process.Process{}
|
||||
have, err = all(cachingWalker)
|
||||
if err != nil || !reflect.DeepEqual(processes, have) {
|
||||
t.Errorf("%v (%v)", test.Diff(processes, have), err)
|
||||
if err != nil || !reflect.DeepEqual(want, have) {
|
||||
t.Errorf("%v (%v)", test.Diff(want, have), err)
|
||||
}
|
||||
|
||||
err = cachingWalker.Tick()
|
||||
@@ -51,16 +52,16 @@ func TestCache(t *testing.T) {
|
||||
}
|
||||
|
||||
have, err = all(cachingWalker)
|
||||
want := []process.Process{}
|
||||
want = map[process.Process]struct{}{}
|
||||
if err != nil || !reflect.DeepEqual(want, have) {
|
||||
t.Errorf("%v (%v)", test.Diff(want, have), err)
|
||||
}
|
||||
}
|
||||
|
||||
func all(w process.Walker) ([]process.Process, error) {
|
||||
all := []process.Process{}
|
||||
func all(w process.Walker) (map[process.Process]struct{}, error) {
|
||||
all := map[process.Process]struct{}{}
|
||||
err := w.Walk(func(p, _ process.Process) {
|
||||
all = append(all, p)
|
||||
all[p] = struct{}{}
|
||||
})
|
||||
return all, err
|
||||
}
|
||||
|
||||
@@ -539,7 +539,7 @@ func hostOriginTable(nmd report.Node) (Table, bool) {
|
||||
fmt formatter
|
||||
}{
|
||||
{host.CPUUsage, "CPU Usage", formatPercent},
|
||||
{host.MemUsage, "Memory Usage", formatPercent},
|
||||
{host.MemUsage, "Memory Usage", formatMemory},
|
||||
} {
|
||||
if val, ok := nmd.Metrics[tuple.key]; ok {
|
||||
rows = append(rows, sparklineRow(tuple.human, val, tuple.fmt))
|
||||
|
||||
Reference in New Issue
Block a user