|
| 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 | + "sort" |
| 21 | + "sync" |
| 22 | + "time" |
| 23 | +) |
| 24 | + |
| 25 | +// The Provider interface defines an interface for measuring metrics. |
| 26 | +type Provider interface { |
| 27 | + // MeasureSince adds a measurement for key to the metric store. |
| 28 | + // Used with defer and time.Now(). |
| 29 | + MeasureSince(key string, start time.Time) |
| 30 | + |
| 31 | + // WriteMetrics will write a summary of the metrics to w. |
| 32 | + WriteMetrics(w io.Writer) |
| 33 | + |
| 34 | + // Reset clears the metric store. |
| 35 | + Reset() |
| 36 | +} |
| 37 | + |
| 38 | +// Store provides storage for a set of metrics. |
| 39 | +type Store struct { |
| 40 | + metrics map[string][]time.Duration |
| 41 | + mu *sync.Mutex |
| 42 | +} |
| 43 | + |
| 44 | +// NewProvider returns a new instance of a metric store. |
| 45 | +func NewProvider() Provider { |
| 46 | + return &Store{ |
| 47 | + metrics: make(map[string][]time.Duration), |
| 48 | + mu: &sync.Mutex{}, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Reset clears the metrics store. |
| 53 | +func (s *Store) Reset() { |
| 54 | + s.mu.Lock() |
| 55 | + s.metrics = make(map[string][]time.Duration) |
| 56 | + s.mu.Unlock() |
| 57 | +} |
| 58 | + |
| 59 | +// MeasureSince adds a measurement for key to the metric store. |
| 60 | +func (s *Store) MeasureSince(key string, start time.Time) { |
| 61 | + s.mu.Lock() |
| 62 | + s.metrics[key] = append(s.metrics[key], time.Since(start)) |
| 63 | + s.mu.Unlock() |
| 64 | +} |
| 65 | + |
| 66 | +// WriteMetrics writes a summary of the metrics to w. |
| 67 | +func (s *Store) WriteMetrics(w io.Writer) { |
| 68 | + s.mu.Lock() |
| 69 | + |
| 70 | + results := make([]result, len(s.metrics)) |
| 71 | + |
| 72 | + var i int |
| 73 | + for k, v := range s.metrics { |
| 74 | + var sum time.Duration |
| 75 | + var max time.Duration |
| 76 | + |
| 77 | + for _, d := range v { |
| 78 | + sum += d |
| 79 | + if d > max { |
| 80 | + max = d |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + avg := time.Duration(int(sum) / len(v)) |
| 85 | + |
| 86 | + results[i] = result{key: k, count: len(v), max: max, sum: sum, avg: avg} |
| 87 | + i++ |
| 88 | + } |
| 89 | + |
| 90 | + s.mu.Unlock() |
| 91 | + |
| 92 | + // sort and print results |
| 93 | + fmt.Fprintf(w, " %13s %12s %12s %5s %s\n", "cumulative", "average", "maximum", "", "") |
| 94 | + fmt.Fprintf(w, " %13s %12s %12s %5s %s\n", "duration", "duration", "duration", "count", "template") |
| 95 | + fmt.Fprintf(w, " %13s %12s %12s %5s %s\n", "----------", "--------", "--------", "-----", "--------") |
| 96 | + |
| 97 | + sort.Sort(bySum(results)) |
| 98 | + for _, v := range results { |
| 99 | + fmt.Fprintf(w, " %13s %12s %12s %5d %s\n", v.sum, v.avg, v.max, v.count, v.key) |
| 100 | + } |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +// A result represents the calculated results for a given metric. |
| 105 | +type result struct { |
| 106 | + key string |
| 107 | + count int |
| 108 | + sum time.Duration |
| 109 | + max time.Duration |
| 110 | + avg time.Duration |
| 111 | +} |
| 112 | + |
| 113 | +type bySum []result |
| 114 | + |
| 115 | +func (b bySum) Len() int { return len(b) } |
| 116 | +func (b bySum) Swap(i, j int) { b[i], b[j] = b[j], b[i] } |
| 117 | +func (b bySum) Less(i, j int) bool { return b[i].sum < b[j].sum } |
0 commit comments