forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage_test.go
More file actions
198 lines (174 loc) · 6.14 KB
/
Copy pathusage_test.go
File metadata and controls
198 lines (174 loc) · 6.14 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package usage
import (
"sync"
"testing"
"time"
"github.com/benbjohnson/clock"
"github.com/raintank/metrictank/consolidation"
"github.com/raintank/metrictank/idx/memory"
"github.com/raintank/metrictank/mdata"
"github.com/raintank/metrictank/mdata/chunk"
"gopkg.in/raintank/schema.v1"
)
type FakeAggMetrics struct {
sync.Mutex
Metrics map[string]*FakeAggMetric
}
func NewFakeAggMetrics() *FakeAggMetrics {
return &FakeAggMetrics{
Metrics: make(map[string]*FakeAggMetric),
}
}
func (f *FakeAggMetrics) Get(key string) (mdata.Metric, bool) {
f.Lock()
m, ok := f.Metrics[key]
f.Unlock()
return m, ok
}
func (f *FakeAggMetrics) GetOrCreate(key string) mdata.Metric {
f.Lock()
m, ok := f.Metrics[key]
if !ok {
m = &FakeAggMetric{key, 0, 0}
f.Metrics[key] = m
}
f.Unlock()
return m
}
func (f *FakeAggMetrics) AggSettings() []mdata.AggSetting {
return []mdata.AggSetting{}
}
type FakeAggMetric struct {
key string
lastTs uint32
lastVal float64
}
func (f *FakeAggMetric) Add(ts uint32, val float64) {
f.lastTs = ts
f.lastVal = val
}
// we won't use this
func (f *FakeAggMetric) Get(from, to uint32) (uint32, []chunk.Iter) {
return 0, make([]chunk.Iter, 0)
}
func (f *FakeAggMetric) GetAggregated(consolidator consolidation.Consolidator, aggSpan, from, to uint32) (uint32, []chunk.Iter) {
return 0, make([]chunk.Iter, 0)
}
func idFor(org int, metric, unit, mtype string, tags []string, interval uint32) string {
md := schema.MetricData{
OrgId: org,
Metric: metric,
Unit: unit,
Mtype: mtype,
Tags: tags,
Interval: int(interval),
}
md.SetId()
return md.Id
}
func assertLen(epoch int, aggmetrics *FakeAggMetrics, l int, t *testing.T) {
aggmetrics.Lock()
if len(aggmetrics.Metrics) != l {
t.Fatalf("%d seconds in: there should be %d metrics at this point, not %d", epoch, l, len(aggmetrics.Metrics))
}
aggmetrics.Unlock()
}
func assert(interval uint32, epoch int, aggmetrics *FakeAggMetrics, org int, metric, unit, mtype string, ts uint32, val float64, t *testing.T) {
id := idFor(org, metric, unit, mtype, []string{}, interval)
m, ok := aggmetrics.Get(id)
if !ok {
t.Fatalf("%d seconds in: assert org %d metric %s ts %d val %f -> metric not found", epoch, org, metric, ts, val)
}
n := m.(*FakeAggMetric)
if n.lastVal != val || n.lastTs != ts {
t.Fatalf("%d seconds in: assert org %d metric %s ts %d val %f -> got ts %d val %f", epoch, org, metric, ts, val, n.lastTs, n.lastVal)
}
}
func DisabledTestUsageBasic(t *testing.T) {
mock := clock.NewMock()
aggmetrics := NewFakeAggMetrics()
metricIndex := memory.New()
metricIndex.Init()
interval := uint32(60)
u := New(interval, aggmetrics, metricIndex, mock)
assertLen(0, aggmetrics, 0, t)
u.Add(1, "foo")
u.Add(1, "bar")
u.Add(2, "foo")
mock.Add(30 * time.Second)
assertLen(30, aggmetrics, 0, t)
mock.Add(29 * time.Second)
assertLen(59, aggmetrics, 0, t)
u.Add(2, "foo")
mock.Add(time.Second)
assertLen(60, aggmetrics, 4, t)
assert(interval, 60, aggmetrics, 1, "metrictank.usage.numSeries", "serie", "gauge", 60, 2, t)
assert(interval, 60, aggmetrics, 1, "metrictank.usage.numPoints", "point", "counter", 60, 2, t)
assert(interval, 60, aggmetrics, 2, "metrictank.usage.numSeries", "serie", "gauge", 60, 1, t)
assert(interval, 60, aggmetrics, 2, "metrictank.usage.numPoints", "point", "counter", 60, 2, t)
u.Add(1, "foo")
u.Add(2, "foo")
mock.Add(60 * time.Second)
assert(interval, 120, aggmetrics, 1, "metrictank.usage.numSeries", "serie", "gauge", 120, 1, t)
assert(interval, 120, aggmetrics, 1, "metrictank.usage.numPoints", "point", "counter", 120, 3, t)
assert(interval, 120, aggmetrics, 2, "metrictank.usage.numSeries", "serie", "gauge", 120, 1, t)
assert(interval, 120, aggmetrics, 2, "metrictank.usage.numPoints", "point", "counter", 120, 3, t)
u.Stop()
}
func DisabledTestUsageMinusOne(t *testing.T) {
mock := clock.NewMock()
aggmetrics := NewFakeAggMetrics()
metricIndex := memory.New()
metricIndex.Init()
interval := uint32(60)
u := New(interval, aggmetrics, metricIndex, mock)
assertLen(0, aggmetrics, 0, t)
u.Add(-1, "globally-visible") // but usage only reported to org 1
u.Add(1, "foo")
u.Add(2, "bar")
mock.Add(30 * time.Second)
assertLen(30, aggmetrics, 0, t)
mock.Add(29 * time.Second)
assertLen(59, aggmetrics, 0, t)
mock.Add(time.Second)
// not very pretty.. but an easy way to assure that the Usage Reporter
// goroutine has some time to push the results into our fake aggmetrics
time.Sleep(20 * time.Millisecond)
assertLen(60, aggmetrics, 6, t)
assert(interval, 60, aggmetrics, 1, "metrictank.usage-minus1.numSeries", "serie", "gauge", 60, 1, t)
assert(interval, 60, aggmetrics, 1, "metrictank.usage-minus1.numPoints", "point", "counter", 60, 1, t)
assert(interval, 60, aggmetrics, 1, "metrictank.usage.numSeries", "serie", "gauge", 60, 1, t)
assert(interval, 60, aggmetrics, 1, "metrictank.usage.numPoints", "point", "counter", 60, 1, t)
assert(interval, 60, aggmetrics, 2, "metrictank.usage.numSeries", "serie", "gauge", 60, 1, t)
assert(interval, 60, aggmetrics, 2, "metrictank.usage.numPoints", "point", "counter", 60, 1, t)
u.Stop()
}
func DisabledTestUsageWrap32(t *testing.T) {
mock := clock.NewMock()
aggmetrics := NewFakeAggMetrics()
metricIndex := memory.New()
metricIndex.Init()
interval := uint32(60)
u := New(interval, aggmetrics, metricIndex, mock)
// max uint32 is 4294967295, let's verify the proper wrapping around that
// pretend an insert maxuint32 -900000
assertLen(0, aggmetrics, 0, t)
u.Add(2, "foo")
u.set(2, "foo", 4294067295)
mock.Add(30 * time.Second)
assertLen(30, aggmetrics, 0, t)
mock.Add(29 * time.Second)
assertLen(59, aggmetrics, 0, t)
mock.Add(time.Second)
assertLen(60, aggmetrics, 2, t)
assert(interval, 60, aggmetrics, 2, "metrictank.usage.numSeries", "serie", "gauge", 60, 1, t)
assert(interval, 60, aggmetrics, 2, "metrictank.usage.numPoints", "point", "counter", 60, 4294067295, t)
for i := 0; i < 1000001; i++ {
u.Add(2, "foo")
}
mock.Add(60 * time.Second)
assertLen(120, aggmetrics, 2, t)
assert(interval, 120, aggmetrics, 2, "metrictank.usage.numSeries", "serie", "gauge", 120, 1, t)
assert(interval, 120, aggmetrics, 2, "metrictank.usage.numPoints", "point", "counter", 120, 100000, t)
u.Stop()
}