forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timings.go
172 lines (150 loc) · 4.35 KB
/
timings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stats
import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"
)
// Timings is meant to tracks timing data
// by named categories as well as histograms.
type Timings struct {
mu sync.Mutex
totalCount int64
totalTime int64
histograms map[string]*Histogram
}
// NewTimings creates a new Timings object, and publishes it if name is set.
// categories is an optional list of categories to initialize to 0.
// Categories that aren't initialized will be missing from the map until the
// first time they are updated.
func NewTimings(name string, categories ...string) *Timings {
t := &Timings{histograms: make(map[string]*Histogram)}
for _, cat := range categories {
t.histograms[cat] = NewGenericHistogram("", bucketCutoffs, bucketLabels, "Count", "Time")
}
if name != "" {
Publish(name, t)
}
return t
}
// Add will add a new value to the named histogram.
func (t *Timings) Add(name string, elapsed time.Duration) {
t.mu.Lock()
defer t.mu.Unlock()
hist, ok := t.histograms[name]
if !ok {
hist = NewGenericHistogram("", bucketCutoffs, bucketLabels, "Count", "Time")
t.histograms[name] = hist
}
elapsedNs := int64(elapsed)
hist.Add(elapsedNs)
t.totalCount++
t.totalTime += elapsedNs
}
// Record is a convenience function that records completion
// timing data based on the provided start time of an event.
func (t *Timings) Record(name string, startTime time.Time) {
t.Add(name, time.Now().Sub(startTime))
}
// String is for expvar.
func (t *Timings) String() string {
t.mu.Lock()
defer t.mu.Unlock()
tm := struct {
TotalCount int64
TotalTime int64
Histograms map[string]*Histogram
}{
t.totalCount,
t.totalTime,
t.histograms,
}
data, err := json.Marshal(tm)
if err != nil {
data, _ = json.Marshal(err.Error())
}
return string(data)
}
// Histograms returns a map pointing at the histograms.
func (t *Timings) Histograms() (h map[string]*Histogram) {
t.mu.Lock()
defer t.mu.Unlock()
h = make(map[string]*Histogram, len(t.histograms))
for k, v := range t.histograms {
h[k] = v
}
return
}
// Count returns the total count for all values.
func (t *Timings) Count() int64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.totalCount
}
// Time returns the total time elapsed for all values.
func (t *Timings) Time() int64 {
t.mu.Lock()
defer t.mu.Unlock()
return t.totalTime
}
// Counts returns the total count for each value.
func (t *Timings) Counts() map[string]int64 {
t.mu.Lock()
defer t.mu.Unlock()
counts := make(map[string]int64, len(t.histograms)+1)
for k, v := range t.histograms {
counts[k] = v.Count()
}
counts["All"] = t.totalCount
return counts
}
var bucketCutoffs = []int64{0.0005 * 1e9, 0.001 * 1e9, 0.005 * 1e9, 0.010 * 1e9, 0.050 * 1e9, 0.100 * 1e9, 0.500 * 1e9, 1.000 * 1e9, 5.000 * 1e9, 10.00 * 1e9}
var bucketLabels []string
func init() {
bucketLabels = make([]string, len(bucketCutoffs)+1)
for i, v := range bucketCutoffs {
bucketLabels[i] = fmt.Sprintf("%d", v)
}
bucketLabels[len(bucketLabels)-1] = "inf"
}
// MultiTimings is meant to tracks timing data by categories as well
// as histograms. The names of the categories are compound names made
// with joining multiple strings with '.'.
type MultiTimings struct {
Timings
labels []string
}
// NewMultiTimings creates a new MultiTimings object.
func NewMultiTimings(name string, labels []string) *MultiTimings {
t := &MultiTimings{
Timings: Timings{histograms: make(map[string]*Histogram)},
labels: labels,
}
if name != "" {
Publish(name, t)
}
return t
}
// Labels returns descriptions of the parts of each compound category name.
func (mt *MultiTimings) Labels() []string {
return mt.labels
}
// Add will add a new value to the named histogram.
func (mt *MultiTimings) Add(names []string, elapsed time.Duration) {
if len(names) != len(mt.labels) {
panic("MultiTimings: wrong number of values in Add")
}
mt.Timings.Add(strings.Join(names, "."), elapsed)
}
// Record is a convenience function that records completion
// timing data based on the provided start time of an event.
func (mt *MultiTimings) Record(names []string, startTime time.Time) {
if len(names) != len(mt.labels) {
panic("MultiTimings: wrong number of values in Record")
}
mt.Timings.Record(strings.Join(names, "."), startTime)
}