forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_utils_test.go
167 lines (137 loc) · 4.79 KB
/
stats_utils_test.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
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel_test
// This file contains test setup logic, and is named with a _test.go suffix to
// ensure it's only compiled with tests.
import (
"fmt"
"reflect"
"sort"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type statsValue struct {
// count is the counter value if this metric is a counter.
count int64
// timers is the list of timer values if this metrics is a timer.
timers []time.Duration
}
type recordingStatsReporter struct {
sync.Mutex
// Values is a map from the metricName -> map[tagMapAsString]*statsValue
Values map[string]map[string]*statsValue
// Expected stores expected counter values.
Expected *recordingStatsReporter
}
func newRecordingStatsReporter() *recordingStatsReporter {
return &recordingStatsReporter{
Values: make(map[string]map[string]*statsValue),
Expected: &recordingStatsReporter{
Values: make(map[string]map[string]*statsValue),
},
}
}
// keysMap returns the keys of the given map as a sorted list of strings.
// If the map is not of the type map[string]* then the function will panic.
func keysMap(m interface{}) []string {
var keys []string
mapKeys := reflect.ValueOf(m).MapKeys()
for _, v := range mapKeys {
keys = append(keys, v.Interface().(string))
}
sort.Strings(keys)
return keys
}
// tagsToString converts a map of tags to a string that can be used as a map key.
func tagsToString(tags map[string]string) string {
var vals []string
for _, k := range keysMap(tags) {
vals = append(vals, fmt.Sprintf("%v = %v", k, tags[k]))
}
return strings.Join(vals, ", ")
}
func (r *recordingStatsReporter) getStat(name string, tags map[string]string) *statsValue {
r.Lock()
defer r.Unlock()
tagMap, ok := r.Values[name]
if !ok {
tagMap = make(map[string]*statsValue)
r.Values[name] = tagMap
}
tagStr := tagsToString(tags)
statVal, ok := tagMap[tagStr]
if !ok {
statVal = &statsValue{}
tagMap[tagStr] = statVal
}
return statVal
}
func (r *recordingStatsReporter) IncCounter(name string, tags map[string]string, value int64) {
statVal := r.getStat(name, tags)
statVal.count += value
}
func (r *recordingStatsReporter) RecordTimer(name string, tags map[string]string, d time.Duration) {
statVal := r.getStat(name, tags)
statVal.timers = append(statVal.timers, d)
}
func (r *recordingStatsReporter) Reset() {
newReporter := newRecordingStatsReporter()
r.Values = newReporter.Values
r.Expected = newReporter.Expected
}
func (r *recordingStatsReporter) Validate(t *testing.T) {
r.Lock()
defer r.Unlock()
assert.Equal(t, keysMap(r.Expected.Values), keysMap(r.Values),
"Metric keys are different")
r.validateExpectedLocked(t)
}
// ValidateExpected only validates metrics added to expected rather than all recorded metrics.
func (r *recordingStatsReporter) ValidateExpected(t testing.TB) {
r.Lock()
defer r.Unlock()
r.validateExpectedLocked(t)
}
func (r *recordingStatsReporter) EnsureNotPresent(t testing.TB, counter string) {
r.Lock()
defer r.Unlock()
assert.NotContains(t, r.Values, counter, "metric should not be present")
}
func (r *recordingStatsReporter) validateExpectedLocked(t testing.TB) {
for counterKey, expectedCounter := range r.Expected.Values {
counter, ok := r.Values[counterKey]
if !assert.True(t, ok, "expected %v not found", counterKey) {
continue
}
assert.Equal(t, keysMap(expectedCounter), keysMap(counter),
"Metric %v has different reported tags", counterKey)
for tags, stat := range counter {
expectedStat, ok := expectedCounter[tags]
if !ok {
continue
}
assert.Equal(t, expectedStat, stat,
"Metric %v with tags %v has mismatched value", counterKey, tags)
}
}
}
func (r *recordingStatsReporter) UpdateGauge(name string, tags map[string]string, value int64) {}