| 1 |
// Copyright 2017 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package metrics provides simple metrics tracking features. |
| 15 |
package metrics |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"io" |
| 20 |
"math" |
| 21 |
"reflect" |
| 22 |
"sort" |
| 23 |
"strconv" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/gohugoio/hugo/common/hashing" |
| 29 |
"github.com/gohugoio/hugo/common/types" |
| 30 |
"github.com/gohugoio/hugo/compare" |
| 31 |
) |
| 32 |
|
| 33 |
// The Provider interface defines an interface for measuring metrics. |
| 34 |
type Provider interface { |
| 35 |
// MeasureSince adds a measurement for key to the metric store. |
| 36 |
// Used with defer and time.Now(). |
| 37 |
MeasureSince(key string, start time.Time) |
| 38 |
|
| 39 |
// WriteMetrics will write a summary of the metrics to w. |
| 40 |
WriteMetrics(w io.Writer) |
| 41 |
|
| 42 |
// TrackValue tracks the value for diff calculations etc. |
| 43 |
TrackValue(key string, value any, cached bool) |
| 44 |
|
| 45 |
// Reset clears the metric store. |
| 46 |
Reset() |
| 47 |
} |
| 48 |
|
| 49 |
type diff struct { |
| 50 |
baseline any |
| 51 |
count int |
| 52 |
simSum int |
| 53 |
} |
| 54 |
|
| 55 |
func (d *diff) add(v any) *diff { |
| 56 |
if types.IsNil(d.baseline) { |
| 57 |
d.baseline = v |
| 58 |
d.count = 1 |
| 59 |
d.simSum = 100 // If we get only one it is very cache friendly. |
| 60 |
return d |
| 61 |
} |
| 62 |
adder := howSimilar(v, d.baseline) |
| 63 |
d.simSum += adder |
| 64 |
d.count++ |
| 65 |
|
| 66 |
return d |
| 67 |
} |
| 68 |
|
| 69 |
// Store provides storage for a set of metrics. |
| 70 |
type Store struct { |
| 71 |
calculateHints bool |
| 72 |
metrics map[string][]time.Duration |
| 73 |
mu sync.Mutex |
| 74 |
diffs map[string]*diff |
| 75 |
diffmu sync.Mutex |
| 76 |
cached map[string]int |
| 77 |
cachedmu sync.Mutex |
| 78 |
} |
| 79 |
|
| 80 |
// NewProvider returns a new instance of a metric store. |
| 81 |
func NewProvider(calculateHints bool) Provider { |
| 82 |
return &Store{ |
| 83 |
calculateHints: calculateHints, |
| 84 |
metrics: make(map[string][]time.Duration), |
| 85 |
diffs: make(map[string]*diff), |
| 86 |
cached: make(map[string]int), |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// Reset clears the metrics store. |
| 91 |
func (s *Store) Reset() { |
| 92 |
s.mu.Lock() |
| 93 |
s.metrics = make(map[string][]time.Duration) |
| 94 |
s.mu.Unlock() |
| 95 |
|
| 96 |
s.diffmu.Lock() |
| 97 |
s.diffs = make(map[string]*diff) |
| 98 |
s.diffmu.Unlock() |
| 99 |
|
| 100 |
s.cachedmu.Lock() |
| 101 |
s.cached = make(map[string]int) |
| 102 |
s.cachedmu.Unlock() |
| 103 |
} |
| 104 |
|
| 105 |
// TrackValue tracks the value for diff calculations etc. |
| 106 |
func (s *Store) TrackValue(key string, value any, cached bool) { |
| 107 |
if !s.calculateHints { |
| 108 |
return |
| 109 |
} |
| 110 |
|
| 111 |
s.diffmu.Lock() |
| 112 |
d, found := s.diffs[key] |
| 113 |
|
| 114 |
if !found { |
| 115 |
d = &diff{} |
| 116 |
s.diffs[key] = d |
| 117 |
} |
| 118 |
|
| 119 |
d.add(value) |
| 120 |
s.diffmu.Unlock() |
| 121 |
|
| 122 |
if cached { |
| 123 |
s.cachedmu.Lock() |
| 124 |
s.cached[key] = s.cached[key] + 1 |
| 125 |
s.cachedmu.Unlock() |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// MeasureSince adds a measurement for key to the metric store. |
| 130 |
func (s *Store) MeasureSince(key string, start time.Time) { |
| 131 |
s.mu.Lock() |
| 132 |
s.metrics[key] = append(s.metrics[key], time.Since(start)) |
| 133 |
s.mu.Unlock() |
| 134 |
} |
| 135 |
|
| 136 |
// WriteMetrics writes a summary of the metrics to w. |
| 137 |
func (s *Store) WriteMetrics(w io.Writer) { |
| 138 |
s.mu.Lock() |
| 139 |
|
| 140 |
results := make([]result, len(s.metrics)) |
| 141 |
|
| 142 |
var i int |
| 143 |
for k, v := range s.metrics { |
| 144 |
var sum time.Duration |
| 145 |
var max time.Duration |
| 146 |
|
| 147 |
diff, found := s.diffs[k] |
| 148 |
|
| 149 |
cacheFactor := 0 |
| 150 |
if found { |
| 151 |
cacheFactor = int(math.Floor(float64(diff.simSum) / float64(diff.count))) |
| 152 |
} |
| 153 |
|
| 154 |
for _, d := range v { |
| 155 |
sum += d |
| 156 |
if d > max { |
| 157 |
max = d |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
avg := time.Duration(int(sum) / len(v)) |
| 162 |
cacheCount := s.cached[k] |
| 163 |
|
| 164 |
results[i] = result{key: k, count: len(v), max: max, sum: sum, avg: avg, cacheCount: cacheCount, cacheFactor: cacheFactor} |
| 165 |
i++ |
| 166 |
} |
| 167 |
|
| 168 |
s.mu.Unlock() |
| 169 |
|
| 170 |
if s.calculateHints { |
| 171 |
fmt.Fprintf(w, " %15s %12s %12s %9s %7s %6s %5s %s\n", "cumulative", "average", "maximum", "cache", "percent", "cached", "total", "") |
| 172 |
fmt.Fprintf(w, " %15s %12s %12s %9s %7s %6s %5s %s\n", "duration", "duration", "duration", "potential", "cached", "count", "count", "template") |
| 173 |
fmt.Fprintf(w, " %15s %12s %12s %9s %7s %6s %5s %s\n", "----------", "--------", "--------", "---------", "-------", "------", "-----", "--------") |
| 174 |
} else { |
| 175 |
fmt.Fprintf(w, " %15s %12s %12s %5s %s\n", "cumulative", "average", "maximum", "", "") |
| 176 |
fmt.Fprintf(w, " %15s %12s %12s %5s %s\n", "duration", "duration", "duration", "count", "template") |
| 177 |
fmt.Fprintf(w, " %15s %12s %12s %5s %s\n", "----------", "--------", "--------", "-----", "--------") |
| 178 |
} |
| 179 |
|
| 180 |
sort.Sort(bySum(results)) |
| 181 |
for _, v := range results { |
| 182 |
if s.calculateHints { |
| 183 |
fmt.Fprintf(w, " %15s %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) |
| 184 |
} else { |
| 185 |
fmt.Fprintf(w, " %15s %12s %12s %5d %s\n", v.sum, v.avg, v.max, v.count, v.key) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
// A result represents the calculated results for a given metric. |
| 191 |
type result struct { |
| 192 |
key string |
| 193 |
count int |
| 194 |
cacheCount int |
| 195 |
cacheFactor int |
| 196 |
sum time.Duration |
| 197 |
max time.Duration |
| 198 |
avg time.Duration |
| 199 |
} |
| 200 |
|
| 201 |
type bySum []result |
| 202 |
|
| 203 |
func (b bySum) Len() int { return len(b) } |
| 204 |
func (b bySum) Swap(i, j int) { b[i], b[j] = b[j], b[i] } |
| 205 |
func (b bySum) Less(i, j int) bool { return b[i].sum > b[j].sum } |
| 206 |
|
| 207 |
// howSimilar is a naive diff implementation that returns |
| 208 |
// a number between 0-100 indicating how similar a and b are. |
| 209 |
func howSimilar(a, b any) int { |
| 210 |
t1, t2 := reflect.TypeOf(a), reflect.TypeOf(b) |
| 211 |
if t1 != t2 { |
| 212 |
return 0 |
| 213 |
} |
| 214 |
|
| 215 |
if t1.Comparable() && t2.Comparable() { |
| 216 |
if a == b { |
| 217 |
return 100 |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
as, ok1 := types.TypeToString(a) |
| 222 |
bs, ok2 := types.TypeToString(b) |
| 223 |
|
| 224 |
if ok1 && ok2 { |
| 225 |
return howSimilarStrings(as, bs) |
| 226 |
} |
| 227 |
|
| 228 |
if ok1 != ok2 { |
| 229 |
return 0 |
| 230 |
} |
| 231 |
|
| 232 |
e1, ok1 := a.(compare.Eqer) |
| 233 |
e2, ok2 := b.(compare.Eqer) |
| 234 |
if ok1 && ok2 && e1.Eq(e2) { |
| 235 |
return 100 |
| 236 |
} |
| 237 |
|
| 238 |
pe1, pok1 := a.(compare.ProbablyEqer) |
| 239 |
pe2, pok2 := b.(compare.ProbablyEqer) |
| 240 |
if pok1 && pok2 && pe1.ProbablyEq(pe2) { |
| 241 |
return 90 |
| 242 |
} |
| 243 |
|
| 244 |
h1, h2 := hashing.HashString(a), hashing.HashString(b) |
| 245 |
if h1 == h2 { |
| 246 |
return 100 |
| 247 |
} |
| 248 |
return 0 |
| 249 |
} |
| 250 |
|
| 251 |
// howSimilar is a naive diff implementation that returns |
| 252 |
// a number between 0-100 indicating how similar a and b are. |
| 253 |
// 100 is when all words in a also exists in b. |
| 254 |
func howSimilarStrings(a, b string) int { |
| 255 |
if a == b { |
| 256 |
return 100 |
| 257 |
} |
| 258 |
|
| 259 |
// Give some weight to the word positions. |
| 260 |
const partitionSize = 4 |
| 261 |
|
| 262 |
af, bf := strings.Fields(a), strings.Fields(b) |
| 263 |
if len(bf) > len(af) { |
| 264 |
af, bf = bf, af |
| 265 |
} |
| 266 |
|
| 267 |
m1 := make(map[string]bool) |
| 268 |
for i, x := range bf { |
| 269 |
partition := partition(i, partitionSize) |
| 270 |
key := x + "/" + strconv.Itoa(partition) |
| 271 |
m1[key] = true |
| 272 |
} |
| 273 |
|
| 274 |
common := 0 |
| 275 |
for i, x := range af { |
| 276 |
partition := partition(i, partitionSize) |
| 277 |
key := x + "/" + strconv.Itoa(partition) |
| 278 |
if m1[key] { |
| 279 |
common++ |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if common == 0 && common == len(af) { |
| 284 |
return 100 |
| 285 |
} |
| 286 |
|
| 287 |
return int(math.Floor((float64(common) / float64(len(af)) * 100))) |
| 288 |
} |
| 289 |
|
| 290 |
func partition(d, scale int) int { |
| 291 |
return int(math.Floor((float64(d) / float64(scale)))) * scale |
| 292 |
} |