-
Notifications
You must be signed in to change notification settings - Fork 116
/
stats_test.go
172 lines (143 loc) · 4.69 KB
/
stats_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
168
169
170
171
172
// Copyright (c) 2021 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 tally
import (
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type statsTestReporter struct {
last interface{}
valueSamples map[float64]int
durationSamples map[time.Duration]int
buckets Buckets
}
func newStatsTestReporter() *statsTestReporter {
return &statsTestReporter{
valueSamples: make(map[float64]int),
durationSamples: make(map[time.Duration]int),
}
}
func (r *statsTestReporter) ReportCounter(name string, tags map[string]string, value int64) {
r.last = value
}
func (r *statsTestReporter) ReportGauge(name string, tags map[string]string, value float64) {
r.last = value
}
func (r *statsTestReporter) ReportTimer(name string, tags map[string]string, interval time.Duration) {
r.last = interval
}
func (r *statsTestReporter) ReportHistogramValueSamples(
name string,
tags map[string]string,
buckets Buckets,
bucketLowerBound,
bucketUpperBound float64,
samples int64,
) {
r.valueSamples[bucketUpperBound] = int(samples)
r.buckets = buckets
}
func (r *statsTestReporter) ReportHistogramDurationSamples(
name string,
tags map[string]string,
buckets Buckets,
bucketLowerBound,
bucketUpperBound time.Duration,
samples int64,
) {
r.durationSamples[bucketUpperBound] = int(samples)
r.buckets = buckets
}
func (r *statsTestReporter) Capabilities() Capabilities {
return capabilitiesReportingNoTagging
}
func (r *statsTestReporter) Flush() {}
func TestCounter(t *testing.T) {
counter := newCounter(nil)
r := newStatsTestReporter()
counter.Inc(1)
counter.report("", nil, r)
assert.Equal(t, int64(1), r.last)
counter.Inc(1)
counter.report("", nil, r)
assert.Equal(t, int64(1), r.last)
counter.Inc(1)
counter.report("", nil, r)
assert.Equal(t, int64(1), r.last)
}
func TestGauge(t *testing.T) {
gauge := newGauge(nil)
r := newStatsTestReporter()
gauge.Update(42)
gauge.report("", nil, r)
assert.Equal(t, float64(42), r.last)
gauge.Update(1234)
gauge.Update(5678)
gauge.report("", nil, r)
assert.Equal(t, float64(5678), r.last)
}
func TestTimer(t *testing.T) {
r := newStatsTestReporter()
timer := newTimer("t1", nil, r, nil)
timer.Record(42 * time.Millisecond)
assert.Equal(t, 42*time.Millisecond, r.last)
timer.Record(128 * time.Millisecond)
assert.Equal(t, 128*time.Millisecond, r.last)
}
func TestHistogramValueSamples(t *testing.T) {
r := newStatsTestReporter()
buckets := MustMakeLinearValueBuckets(0, 10, 10)
storage := newBucketStorage(valueHistogramType, buckets)
h := newHistogram(valueHistogramType, "h1", nil, r, storage, nil)
var offset float64
for i := 0; i < 3; i++ {
h.RecordValue(offset + rand.Float64()*10)
}
offset = 50
for i := 0; i < 5; i++ {
h.RecordValue(offset + rand.Float64()*10)
}
h.report(h.name, h.tags, r)
assert.Equal(t, 3, r.valueSamples[10.0])
assert.Equal(t, 5, r.valueSamples[60.0])
assert.Equal(t, buckets, r.buckets)
}
func TestHistogramDurationSamples(t *testing.T) {
r := newStatsTestReporter()
buckets := MustMakeLinearDurationBuckets(0, 10*time.Millisecond, 10)
storage := newBucketStorage(durationHistogramType, buckets)
h := newHistogram(durationHistogramType, "h1", nil, r, storage, nil)
var offset time.Duration
for i := 0; i < 3; i++ {
h.RecordDuration(offset +
time.Duration(rand.Float64()*float64(10*time.Millisecond)))
}
offset = 50 * time.Millisecond
for i := 0; i < 5; i++ {
h.RecordDuration(offset +
time.Duration(rand.Float64()*float64(10*time.Millisecond)))
}
h.report(h.name, h.tags, r)
assert.Equal(t, 3, r.durationSamples[10*time.Millisecond])
assert.Equal(t, 5, r.durationSamples[60*time.Millisecond])
assert.Equal(t, buckets, r.buckets)
}