Skip to content

Commit

Permalink
metrics: Add cached count tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
moorereason committed Mar 30, 2021
1 parent 2dc222c commit 42db9df
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
55 changes: 38 additions & 17 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ import (
"sync"
"time"

"github.com/gohugoio/hugo/helpers"

"github.com/gohugoio/hugo/common/types"

"github.com/gohugoio/hugo/compare"
"github.com/gohugoio/hugo/helpers"
)

// The Provider interface defines an interface for measuring metrics.
Expand All @@ -44,6 +42,10 @@ type Provider interface {
// TrackValue tracks the value for diff calculations etc.
TrackValue(key string, value interface{})

// IncrementCachedExec increments the cached execute count for a given
// template.
IncrementCachedExec(key string)

// Reset clears the metric store.
Reset()
}
Expand All @@ -54,8 +56,6 @@ type diff struct {
simSum int
}

var counter = 0

func (d *diff) add(v interface{}) *diff {
if types.IsNil(d.baseline) {
d.baseline = v
Expand All @@ -77,6 +77,8 @@ type Store struct {
mu sync.Mutex
diffs map[string]*diff
diffmu sync.Mutex
cached map[string]int
cachedmu sync.Mutex
}

// NewProvider returns a new instance of a metric store.
Expand All @@ -85,6 +87,7 @@ func NewProvider(calculateHints bool) Provider {
calculateHints: calculateHints,
metrics: make(map[string][]time.Duration),
diffs: make(map[string]*diff),
cached: make(map[string]int),
}
}

Expand All @@ -93,9 +96,31 @@ func (s *Store) Reset() {
s.mu.Lock()
s.metrics = make(map[string][]time.Duration)
s.mu.Unlock()

s.diffmu.Lock()
s.diffs = make(map[string]*diff)
s.diffmu.Unlock()

s.cachedmu.Lock()
s.cached = make(map[string]int)
s.cachedmu.Unlock()
}

// IncrementCachedExec increments the cached execute count for a given template.
func (s *Store) IncrementCachedExec(key string) {
if !s.calculateHints {
return
}

s.cachedmu.Lock()
_, found := s.cached[key]

if !found {
s.cached[key] = 0
}

s.cached[key] += 1
s.cachedmu.Unlock()
}

// TrackValue tracks the value for diff calculations etc.
Expand All @@ -105,20 +130,14 @@ func (s *Store) TrackValue(key string, value interface{}) {
}

s.diffmu.Lock()
var (
d *diff
found bool
)

d, found = s.diffs[key]
d, found := s.diffs[key]

if !found {
d = &diff{}
s.diffs[key] = d
}

d.add(value)

s.diffmu.Unlock()
}

Expand Down Expand Up @@ -155,17 +174,18 @@ func (s *Store) WriteMetrics(w io.Writer) {
}

avg := time.Duration(int(sum) / len(v))
cacheCount := s.cached[k]

results[i] = result{key: k, count: len(v), max: max, sum: sum, avg: avg, cacheFactor: cacheFactor}
results[i] = result{key: k, count: len(v), max: max, sum: sum, avg: avg, cacheCount: cacheCount, cacheFactor: cacheFactor}
i++
}

s.mu.Unlock()

if s.calculateHints {
fmt.Fprintf(w, " %9s %13s %12s %12s %5s %s\n", "cache", "cumulative", "average", "maximum", "", "")
fmt.Fprintf(w, " %9s %13s %12s %12s %5s %s\n", "potential", "duration", "duration", "duration", "count", "template")
fmt.Fprintf(w, " %9s %13s %12s %12s %5s %s\n", "-----", "----------", "--------", "--------", "-----", "--------")
fmt.Fprintf(w, " %9s %13s %12s %12s %5s %6s %s\n", "cache", "cumulative", "average", "maximum", "total", "cached", "")
fmt.Fprintf(w, " %9s %13s %12s %12s %5s %6s %s\n", "potential", "duration", "duration", "duration", "count", "count", "template")
fmt.Fprintf(w, " %9s %13s %12s %12s %5s %6s %s\n", "-----", "----------", "--------", "--------", "-----", "------", "--------")
} else {
fmt.Fprintf(w, " %13s %12s %12s %5s %s\n", "cumulative", "average", "maximum", "", "")
fmt.Fprintf(w, " %13s %12s %12s %5s %s\n", "duration", "duration", "duration", "count", "template")
Expand All @@ -176,7 +196,7 @@ func (s *Store) WriteMetrics(w io.Writer) {
sort.Sort(bySum(results))
for _, v := range results {
if s.calculateHints {
fmt.Fprintf(w, " %9d %13s %12s %12s %5d %s\n", v.cacheFactor, v.sum, v.avg, v.max, v.count, v.key)
fmt.Fprintf(w, " %9d %13s %12s %12s %5d %6d %s\n", v.cacheFactor, v.sum, v.avg, v.max, v.count, v.cacheCount, v.key)
} else {
fmt.Fprintf(w, " %13s %12s %12s %5d %s\n", v.sum, v.avg, v.max, v.count, v.key)
}
Expand All @@ -187,6 +207,7 @@ func (s *Store) WriteMetrics(w io.Writer) {
type result struct {
key string
count int
cacheCount int
cacheFactor int
sum time.Duration
max time.Duration
Expand Down
4 changes: 4 additions & 0 deletions tpl/partials/partials.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ func (ns *Namespace) getOrCreate(key partialCacheKey, context interface{}) (resu
return nil, err
}

if ns.deps.Metrics != nil {
ns.deps.Metrics.IncrementCachedExec("partials/" + key.name)
}

ns.cachedPartials.Lock()
defer ns.cachedPartials.Unlock()
// Double-check.
Expand Down

0 comments on commit 42db9df

Please sign in to comment.