-
Notifications
You must be signed in to change notification settings - Fork 3
/
metric_test.go
187 lines (166 loc) · 4.53 KB
/
metric_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package schema
import (
"reflect"
"sort"
"testing"
"unsafe"
)
func BenchmarkSetId(b *testing.B) {
metric := MetricData{
OrgId: 1234,
Name: "key1=val1.key2=val2.my.test.metric.name",
Interval: 15,
Value: 0.1234,
Unit: "ms",
Time: 1234567890,
Mtype: "gauge",
Tags: []string{"key1:val1", "key2:val2"},
}
for i := 0; i < b.N; i++ {
metric.SetId()
}
}
func TestTagValidation(t *testing.T) {
type testCase struct {
tag []string
expecting bool
}
testCases := []testCase{
{[]string{"abc=cba"}, true},
{[]string{"a="}, false},
{[]string{"a!="}, false},
{[]string{"=abc"}, false},
{[]string{"@#$%!=(*&"}, false},
{[]string{"!@#$%=(*&"}, false},
{[]string{"@#;$%=(*&"}, false},
{[]string{"@#$%=(;*&"}, false},
{[]string{"@#$%=(*&"}, true},
{[]string{"@#$%=(*&", "abc=!fd", "a===="}, true},
{[]string{"@#$%=(*&", "abc=!fd", "a===;="}, false},
{[]string{"a=~a"}, false},
{[]string{"a=a~"}, true},
{[]string{"aaa"}, false},
}
for _, tc := range testCases {
if tc.expecting != ValidateTags(tc.tag) {
t.Fatalf("Expected %t, but testcase %s returned %t", tc.expecting, tc.tag, !tc.expecting)
}
}
}
func newMetricDefinition(name string, tags []string) *MetricDefinition {
sort.Strings(tags)
return &MetricDefinition{Name: name, Tags: tags}
}
func TestNameWithTags(t *testing.T) {
type testCase struct {
expectedName string
expectedNameWithTags string
expectedTags []string
md MetricDefinition
}
testCases := []testCase{
{
"a.b.c",
"a.b.c;tag1=value1",
[]string{"tag1=value1"},
*newMetricDefinition("a.b.c", []string{"tag1=value1", "name=ccc"}),
}, {
"a.b.c",
"a.b.c;a=a;b=b;c=c",
[]string{"a=a", "b=b", "c=c"},
*newMetricDefinition("a.b.c", []string{"name=a.b.c", "c=c", "b=b", "a=a"}),
}, {
"a.b.c",
"a.b.c",
[]string{},
*newMetricDefinition("a.b.c", []string{"name=a.b.c"}),
}, {
"a.b.c",
"a.b.c",
[]string{},
*newMetricDefinition("a.b.c", []string{}),
}, {
"c",
"c;a=a;b=b;c=c",
[]string{"a=a", "b=b", "c=c"},
*newMetricDefinition("c", []string{"c=c", "a=a", "b=b"}),
},
}
for _, tc := range testCases {
tc.md.SetId()
if tc.expectedName != tc.md.Name {
t.Fatalf("Expected name %s, but got %s", tc.expectedName, tc.md.Name)
}
if tc.expectedNameWithTags != tc.md.NameWithTags() {
t.Fatalf("Expected name with tags %s, but got %s", tc.expectedNameWithTags, tc.md.NameWithTags())
}
if len(tc.expectedTags) != len(tc.md.Tags) {
t.Fatalf("Expected tags %+v, but got %+v", tc.expectedTags, tc.md.Tags)
}
for i := range tc.expectedTags {
if len(tc.expectedTags[i]) != len(tc.md.Tags[i]) {
t.Fatalf("Expected tags %+v, but got %+v", tc.expectedTags, tc.md.Tags)
}
}
getAddress := func(s string) uint {
return uint((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)
}
nameWithTagsAddr := getAddress(tc.md.NameWithTags())
nameAddr := getAddress(tc.md.Name)
if nameAddr != nameWithTagsAddr {
t.Fatalf("Name slice does not appear to be slice of base string, %d != %d", nameAddr, nameWithTagsAddr)
}
for i := range tc.md.Tags {
tagAddr := getAddress(tc.md.Tags[i])
if tagAddr < nameWithTagsAddr || tagAddr >= nameWithTagsAddr+uint(len(tc.md.NameWithTags())) {
t.Fatalf("Tag slice does not appear to be slice of base string, %d != %d", tagAddr, nameWithTagsAddr)
}
}
}
}
func TestNameSanitizedAsTagValue(t *testing.T) {
type testCase struct {
originalName string
expectedName string
}
cases := []testCase{
{
originalName: "my~.test.abc",
expectedName: "my~.test.abc",
}, {
originalName: "~a.b.c",
expectedName: "a.b.c",
}, {
originalName: "~~a~~.~~~b~~~.~~~c~~~",
expectedName: "a~~.~~~b~~~.~~~c~~~",
}, {
originalName: "~a~",
expectedName: "a~",
}, {
originalName: "~",
expectedName: "",
}, {
originalName: "~~~",
expectedName: "",
},
}
for i := range cases {
md := MetricDefinition{Name: cases[i].originalName}
sanitized := md.NameSanitizedAsTagValue()
if sanitized != cases[i].expectedName {
t.Fatalf("TC %d: Expected sanitized version of %s to be %s, but it was %s", i, md.Name, cases[i].expectedName, sanitized)
}
}
}
func BenchmarkNameSanitizedAsTagValueWithValidValue(b *testing.B) {
inputValue := "some.id.of.a.metric.1"
for i := 0; i < b.N; i++ {
SanitizeNameAsTagValue(inputValue)
}
}
func BenchmarkNameSanitizedAsTagValueWithInvalidValue(b *testing.B) {
inputValue := "~some.id.of.a.metric.1"
for i := 0; i < b.N; i++ {
SanitizeNameAsTagValue(inputValue)
}
}