2017-09-26 14:03:04 -04:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package metrics provides simple metrics tracking features.
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-10-04 16:12:51 -04:00
|
|
|
"math"
|
2020-12-02 07:23:25 -05:00
|
|
|
"reflect"
|
2017-09-26 14:03:04 -04:00
|
|
|
"sort"
|
2017-10-04 16:12:51 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-09-26 14:03:04 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-04-02 04:30:24 -04:00
|
|
|
|
2020-12-02 07:23:25 -05:00
|
|
|
"github.com/gohugoio/hugo/common/types"
|
2019-04-02 04:30:24 -04:00
|
|
|
"github.com/gohugoio/hugo/compare"
|
2021-03-30 11:47:34 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-09-26 14:03:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// The Provider interface defines an interface for measuring metrics.
|
|
|
|
type Provider interface {
|
|
|
|
// MeasureSince adds a measurement for key to the metric store.
|
|
|
|
// Used with defer and time.Now().
|
|
|
|
MeasureSince(key string, start time.Time)
|
|
|
|
|
|
|
|
// WriteMetrics will write a summary of the metrics to w.
|
|
|
|
WriteMetrics(w io.Writer)
|
|
|
|
|
2017-10-04 16:12:51 -04:00
|
|
|
// TrackValue tracks the value for diff calculations etc.
|
2021-03-30 11:47:34 -04:00
|
|
|
TrackValue(key string, value interface{}, cached bool)
|
2017-10-04 16:12:51 -04:00
|
|
|
|
2017-09-26 14:03:04 -04:00
|
|
|
// Reset clears the metric store.
|
|
|
|
Reset()
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:12:51 -04:00
|
|
|
type diff struct {
|
2019-04-02 04:30:24 -04:00
|
|
|
baseline interface{}
|
2017-10-04 16:12:51 -04:00
|
|
|
count int
|
|
|
|
simSum int
|
|
|
|
}
|
|
|
|
|
2019-04-02 04:30:24 -04:00
|
|
|
func (d *diff) add(v interface{}) *diff {
|
2020-03-12 12:09:49 -04:00
|
|
|
if types.IsNil(d.baseline) {
|
2017-10-04 16:12:51 -04:00
|
|
|
d.baseline = v
|
|
|
|
d.count = 1
|
|
|
|
d.simSum = 100 // If we get only one it is very cache friendly.
|
|
|
|
return d
|
|
|
|
}
|
2020-03-12 12:09:49 -04:00
|
|
|
adder := howSimilar(v, d.baseline)
|
|
|
|
d.simSum += adder
|
2017-10-04 16:12:51 -04:00
|
|
|
d.count++
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:03:04 -04:00
|
|
|
// Store provides storage for a set of metrics.
|
|
|
|
type Store struct {
|
2017-10-04 16:12:51 -04:00
|
|
|
calculateHints bool
|
|
|
|
metrics map[string][]time.Duration
|
|
|
|
mu sync.Mutex
|
|
|
|
diffs map[string]*diff
|
|
|
|
diffmu sync.Mutex
|
2021-03-30 11:47:34 -04:00
|
|
|
cached map[string]int
|
|
|
|
cachedmu sync.Mutex
|
2017-09-26 14:03:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewProvider returns a new instance of a metric store.
|
2017-10-04 16:12:51 -04:00
|
|
|
func NewProvider(calculateHints bool) Provider {
|
2017-09-26 14:03:04 -04:00
|
|
|
return &Store{
|
2017-10-04 16:12:51 -04:00
|
|
|
calculateHints: calculateHints,
|
|
|
|
metrics: make(map[string][]time.Duration),
|
|
|
|
diffs: make(map[string]*diff),
|
2021-03-30 11:47:34 -04:00
|
|
|
cached: make(map[string]int),
|
2017-09-26 14:03:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset clears the metrics store.
|
|
|
|
func (s *Store) Reset() {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.metrics = make(map[string][]time.Duration)
|
|
|
|
s.mu.Unlock()
|
2021-03-30 11:47:34 -04:00
|
|
|
|
2017-10-04 16:12:51 -04:00
|
|
|
s.diffmu.Lock()
|
|
|
|
s.diffs = make(map[string]*diff)
|
|
|
|
s.diffmu.Unlock()
|
2021-03-30 11:47:34 -04:00
|
|
|
|
|
|
|
s.cachedmu.Lock()
|
|
|
|
s.cached = make(map[string]int)
|
|
|
|
s.cachedmu.Unlock()
|
2017-10-04 16:12:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TrackValue tracks the value for diff calculations etc.
|
2021-03-30 11:47:34 -04:00
|
|
|
func (s *Store) TrackValue(key string, value interface{}, cached bool) {
|
2017-10-04 16:12:51 -04:00
|
|
|
if !s.calculateHints {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.diffmu.Lock()
|
2021-03-30 11:47:34 -04:00
|
|
|
d, found := s.diffs[key]
|
2017-10-04 16:12:51 -04:00
|
|
|
|
|
|
|
if !found {
|
|
|
|
d = &diff{}
|
|
|
|
s.diffs[key] = d
|
|
|
|
}
|
|
|
|
|
|
|
|
d.add(value)
|
|
|
|
s.diffmu.Unlock()
|
2021-03-30 11:47:34 -04:00
|
|
|
|
|
|
|
if cached {
|
|
|
|
s.cachedmu.Lock()
|
|
|
|
s.cached[key] = s.cached[key] + 1
|
|
|
|
s.cachedmu.Unlock()
|
|
|
|
}
|
2017-09-26 14:03:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MeasureSince adds a measurement for key to the metric store.
|
|
|
|
func (s *Store) MeasureSince(key string, start time.Time) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.metrics[key] = append(s.metrics[key], time.Since(start))
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMetrics writes a summary of the metrics to w.
|
|
|
|
func (s *Store) WriteMetrics(w io.Writer) {
|
|
|
|
s.mu.Lock()
|
|
|
|
|
|
|
|
results := make([]result, len(s.metrics))
|
|
|
|
|
|
|
|
var i int
|
|
|
|
for k, v := range s.metrics {
|
|
|
|
var sum time.Duration
|
|
|
|
var max time.Duration
|
|
|
|
|
2017-10-04 16:12:51 -04:00
|
|
|
diff, found := s.diffs[k]
|
2020-03-12 12:09:49 -04:00
|
|
|
|
2017-10-04 16:12:51 -04:00
|
|
|
cacheFactor := 0
|
|
|
|
if found {
|
|
|
|
cacheFactor = int(math.Floor(float64(diff.simSum) / float64(diff.count)))
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:03:04 -04:00
|
|
|
for _, d := range v {
|
|
|
|
sum += d
|
|
|
|
if d > max {
|
|
|
|
max = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
avg := time.Duration(int(sum) / len(v))
|
2021-03-30 11:47:34 -04:00
|
|
|
cacheCount := s.cached[k]
|
2017-09-26 14:03:04 -04:00
|
|
|
|
2021-03-30 11:47:34 -04:00
|
|
|
results[i] = result{key: k, count: len(v), max: max, sum: sum, avg: avg, cacheCount: cacheCount, cacheFactor: cacheFactor}
|
2017-09-26 14:03:04 -04:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
2017-10-04 16:12:51 -04:00
|
|
|
if s.calculateHints {
|
2021-03-30 11:47:34 -04:00
|
|
|
fmt.Fprintf(w, " %13s %12s %12s %9s %7s %6s %5s %s\n", "cumulative", "average", "maximum", "cache", "percent", "cached", "total", "")
|
|
|
|
fmt.Fprintf(w, " %13s %12s %12s %9s %7s %6s %5s %s\n", "duration", "duration", "duration", "potential", "cached", "count", "count", "template")
|
|
|
|
fmt.Fprintf(w, " %13s %12s %12s %9s %7s %6s %5s %s\n", "----------", "--------", "--------", "---------", "-------", "------", "-----", "--------")
|
2017-10-04 16:12:51 -04:00
|
|
|
} 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")
|
|
|
|
fmt.Fprintf(w, " %13s %12s %12s %5s %s\n", "----------", "--------", "--------", "-----", "--------")
|
|
|
|
|
|
|
|
}
|
2017-09-26 14:03:04 -04:00
|
|
|
|
|
|
|
sort.Sort(bySum(results))
|
|
|
|
for _, v := range results {
|
2017-10-04 16:12:51 -04:00
|
|
|
if s.calculateHints {
|
2021-03-30 11:47:34 -04:00
|
|
|
fmt.Fprintf(w, " %13s %12s %12s %9d %7.f %6d %5d %s\n", v.sum, v.avg, v.max, v.cacheFactor, float64(v.cacheCount)/float64(v.count)*100, v.cacheCount, v.count, v.key)
|
2017-10-04 16:12:51 -04:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, " %13s %12s %12s %5d %s\n", v.sum, v.avg, v.max, v.count, v.key)
|
|
|
|
}
|
2017-09-26 14:03:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A result represents the calculated results for a given metric.
|
|
|
|
type result struct {
|
2017-10-04 16:12:51 -04:00
|
|
|
key string
|
|
|
|
count int
|
2021-03-30 11:47:34 -04:00
|
|
|
cacheCount int
|
2017-10-04 16:12:51 -04:00
|
|
|
cacheFactor int
|
|
|
|
sum time.Duration
|
|
|
|
max time.Duration
|
|
|
|
avg time.Duration
|
2017-09-26 14:03:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type bySum []result
|
|
|
|
|
|
|
|
func (b bySum) Len() int { return len(b) }
|
|
|
|
func (b bySum) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
2017-09-26 14:31:40 -04:00
|
|
|
func (b bySum) Less(i, j int) bool { return b[i].sum > b[j].sum }
|
2017-10-04 16:12:51 -04:00
|
|
|
|
|
|
|
// howSimilar is a naive diff implementation that returns
|
|
|
|
// a number between 0-100 indicating how similar a and b are.
|
2019-04-02 04:30:24 -04:00
|
|
|
func howSimilar(a, b interface{}) int {
|
2020-03-12 12:09:49 -04:00
|
|
|
t1, t2 := reflect.TypeOf(a), reflect.TypeOf(b)
|
|
|
|
if t1 != t2 {
|
|
|
|
return 0
|
|
|
|
}
|
2017-10-04 16:12:51 -04:00
|
|
|
|
2020-03-12 12:09:49 -04:00
|
|
|
if t1.Comparable() && t2.Comparable() {
|
|
|
|
if a == b {
|
|
|
|
return 100
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
as, ok1 := types.TypeToString(a)
|
|
|
|
bs, ok2 := types.TypeToString(b)
|
2019-04-02 04:30:24 -04:00
|
|
|
|
2019-04-02 14:43:58 -04:00
|
|
|
if ok1 && ok2 {
|
2019-04-02 04:30:24 -04:00
|
|
|
return howSimilarStrings(as, bs)
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:43:58 -04:00
|
|
|
if ok1 != ok2 {
|
2019-04-02 04:30:24 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
e1, ok1 := a.(compare.Eqer)
|
|
|
|
e2, ok2 := b.(compare.Eqer)
|
|
|
|
if ok1 && ok2 && e1.Eq(e2) {
|
|
|
|
return 100
|
|
|
|
}
|
|
|
|
|
|
|
|
pe1, pok1 := a.(compare.ProbablyEqer)
|
|
|
|
pe2, pok2 := b.(compare.ProbablyEqer)
|
|
|
|
if pok1 && pok2 && pe1.ProbablyEq(pe2) {
|
|
|
|
return 90
|
|
|
|
}
|
|
|
|
|
2020-03-12 12:09:49 -04:00
|
|
|
h1, h2 := helpers.HashString(a), helpers.HashString(b)
|
|
|
|
if h1 == h2 {
|
|
|
|
return 100
|
|
|
|
}
|
2019-04-02 04:30:24 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// howSimilar is a naive diff implementation that returns
|
|
|
|
// a number between 0-100 indicating how similar a and b are.
|
|
|
|
// 100 is when all words in a also exists in b.
|
|
|
|
func howSimilarStrings(a, b string) int {
|
2020-03-12 12:09:49 -04:00
|
|
|
if a == b {
|
|
|
|
return 100
|
|
|
|
}
|
2019-04-02 04:30:24 -04:00
|
|
|
|
2017-10-04 16:12:51 -04:00
|
|
|
// Give some weight to the word positions.
|
|
|
|
const partitionSize = 4
|
|
|
|
|
|
|
|
af, bf := strings.Fields(a), strings.Fields(b)
|
|
|
|
if len(bf) > len(af) {
|
|
|
|
af, bf = bf, af
|
|
|
|
}
|
|
|
|
|
|
|
|
m1 := make(map[string]bool)
|
|
|
|
for i, x := range bf {
|
|
|
|
partition := partition(i, partitionSize)
|
|
|
|
key := x + "/" + strconv.Itoa(partition)
|
|
|
|
m1[key] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
common := 0
|
|
|
|
for i, x := range af {
|
|
|
|
partition := partition(i, partitionSize)
|
|
|
|
key := x + "/" + strconv.Itoa(partition)
|
|
|
|
if m1[key] {
|
|
|
|
common++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(math.Floor((float64(common) / float64(len(af)) * 100)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func partition(d, scale int) int {
|
|
|
|
return int(math.Floor((float64(d) / float64(scale)))) * scale
|
|
|
|
}
|