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
|
package solomon
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/atomic"
)
func TestHistogram_MarshalJSON(t *testing.T) {
h := &Histogram{
name: "myhistogram",
metricType: typeHistogram,
tags: map[string]string{"ololo": "trololo"},
bucketBounds: []float64{1, 2, 3},
bucketValues: []int64{1, 2, 1},
infValue: *atomic.NewInt64(2),
}
b, err := json.Marshal(h)
assert.NoError(t, err)
expected := []byte(`{"type":"HIST","labels":{"ololo":"trololo","sensor":"myhistogram"},"hist":{"bounds":[1,2,3],"buckets":[1,2,1],"inf":2}}`)
assert.Equal(t, expected, b)
}
func TestRatedHistogram_MarshalJSON(t *testing.T) {
h := &Histogram{
name: "myhistogram",
metricType: typeRatedHistogram,
tags: map[string]string{"ololo": "trololo"},
bucketBounds: []float64{1, 2, 3},
bucketValues: []int64{1, 2, 1},
infValue: *atomic.NewInt64(2),
}
b, err := json.Marshal(h)
assert.NoError(t, err)
expected := []byte(`{"type":"HIST_RATE","labels":{"ololo":"trololo","sensor":"myhistogram"},"hist":{"bounds":[1,2,3],"buckets":[1,2,1],"inf":2}}`)
assert.Equal(t, expected, b)
}
func TestNameTagHistogram_MarshalJSON(t *testing.T) {
h := &Histogram{
name: "myhistogram",
metricType: typeRatedHistogram,
tags: map[string]string{"ololo": "trololo"},
bucketBounds: []float64{1, 2, 3},
bucketValues: []int64{1, 2, 1},
infValue: *atomic.NewInt64(2),
useNameTag: true,
}
b, err := json.Marshal(h)
assert.NoError(t, err)
expected := []byte(`{"type":"HIST_RATE","labels":{"name":"myhistogram","ololo":"trololo"},"hist":{"bounds":[1,2,3],"buckets":[1,2,1],"inf":2}}`)
assert.Equal(t, expected, b)
}
func TestHistogram_RecordDuration(t *testing.T) {
h := &Histogram{
name: "myhistogram",
metricType: typeHistogram,
tags: map[string]string{"ololo": "trololo"},
bucketBounds: []float64{1, 2, 3},
bucketValues: make([]int64, 3),
}
h.RecordDuration(500 * time.Millisecond)
h.RecordDuration(1 * time.Second)
h.RecordDuration(1800 * time.Millisecond)
h.RecordDuration(3 * time.Second)
h.RecordDuration(1 * time.Hour)
expectedValues := []int64{2, 1, 1}
assert.Equal(t, expectedValues, h.bucketValues)
var expectedInfValue int64 = 1
assert.Equal(t, expectedInfValue, h.infValue.Load())
}
func TestHistogram_RecordValue(t *testing.T) {
h := &Histogram{
name: "myhistogram",
metricType: typeHistogram,
tags: map[string]string{"ololo": "trololo"},
bucketBounds: []float64{1, 2, 3},
bucketValues: make([]int64, 3),
}
h.RecordValue(0.5)
h.RecordValue(1)
h.RecordValue(1.8)
h.RecordValue(3)
h.RecordValue(60)
expectedValues := []int64{2, 1, 1}
assert.Equal(t, expectedValues, h.bucketValues)
var expectedInfValue int64 = 1
assert.Equal(t, expectedInfValue, h.infValue.Load())
}
func TestHistogram_Reset(t *testing.T) {
h := &Histogram{
name: "myhistogram",
metricType: typeHistogram,
tags: map[string]string{"ololo": "trololo"},
bucketBounds: []float64{1, 2, 3},
bucketValues: make([]int64, 3),
}
h.RecordValue(0.5)
h.RecordValue(1)
h.RecordValue(1.8)
h.RecordValue(3)
h.RecordValue(60)
assert.Equal(t, []int64{2, 1, 1}, h.bucketValues)
assert.Equal(t, int64(1), h.infValue.Load())
h.Reset()
assert.Equal(t, []int64{0, 0, 0}, h.bucketValues)
assert.Equal(t, int64(0), h.infValue.Load())
}
func TestHistogram_InitBucketValues(t *testing.T) {
h := &Histogram{
name: "myhistogram",
metricType: typeHistogram,
tags: map[string]string{"ololo": "trololo"},
bucketBounds: []float64{1, 2, 3},
bucketValues: make([]int64, 3),
}
valsToInit := []int64{1, 2, 3, 4}
h.InitBucketValues(valsToInit[:2])
assert.Equal(t, append(valsToInit[:2], 0), h.bucketValues)
assert.Equal(t, *atomic.NewInt64(0), h.infValue)
h.InitBucketValues(valsToInit[:3])
assert.Equal(t, valsToInit[:3], h.bucketValues)
assert.Equal(t, *atomic.NewInt64(0), h.infValue)
h.InitBucketValues(valsToInit)
assert.Equal(t, valsToInit[:3], h.bucketValues)
assert.Equal(t, *atomic.NewInt64(valsToInit[3]), h.infValue)
}
|