Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 7387d22

Browse files
committed
Add tests and benchmarks for SetTags
1 parent 9e5eff6 commit 7387d22

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

api/models/series_test.go

+102
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package models
22

33
import (
44
"encoding/json"
5+
"math/rand"
6+
"reflect"
57
"testing"
68

79
"github.com/raintank/schema"
@@ -90,3 +92,103 @@ func TestJsonMarshal(t *testing.T) {
9092
}
9193
}
9294
}
95+
96+
func TestSetTags(t *testing.T) {
97+
cases := []struct {
98+
in Series
99+
out map[string]string
100+
}{
101+
{
102+
in: Series{},
103+
out: map[string]string{
104+
"name": "",
105+
},
106+
},
107+
{
108+
in: Series{
109+
Target: "a",
110+
},
111+
out: map[string]string{
112+
"name": "a",
113+
},
114+
},
115+
{
116+
in: Series{
117+
Target: `a\b`,
118+
},
119+
out: map[string]string{
120+
"name": `a\b`,
121+
},
122+
},
123+
{
124+
in: Series{
125+
Target: "a;b=c;c=d",
126+
},
127+
out: map[string]string{
128+
"name": "a",
129+
"b": "c",
130+
"c": "d",
131+
},
132+
},
133+
{
134+
in: Series{
135+
Target: "a;biglongtagkeyhere=andithasabiglongtagvaluetoo;c=d",
136+
},
137+
out: map[string]string{
138+
"name": "a",
139+
"biglongtagkeyhere": "andithasabiglongtagvaluetoo",
140+
"c": "d",
141+
},
142+
},
143+
}
144+
145+
for _, c := range cases {
146+
c.in.SetTags()
147+
if !reflect.DeepEqual(c.out, c.in.Tags) {
148+
t.Fatalf("SetTags incorrect\nexpected:%v\ngot: %v\n", c.out, c.in.Tags)
149+
}
150+
}
151+
}
152+
153+
func BenchmarkSetTags_00tags_00chars(b *testing.B) {
154+
benchmarkSetTags(b, 0, 0, 0)
155+
}
156+
157+
func BenchmarkSetTags_20tags_32chars(b *testing.B) {
158+
benchmarkSetTags(b, 20, 32, 32)
159+
}
160+
161+
func benchmarkSetTags(b *testing.B, numTags, tagKeyLength, tagValueLength int) {
162+
in := Series{
163+
Target: "my.metric.name",
164+
}
165+
166+
for i := 0; i < numTags; i++ {
167+
in.Target = in.Target + ";" + randString(tagKeyLength)
168+
in.Target = in.Target + "="
169+
in.Target = in.Target + randString(tagValueLength)
170+
}
171+
172+
b.ReportAllocs()
173+
b.ResetTimer()
174+
175+
for i := 0; i < b.N; i++ {
176+
in.SetTags()
177+
if len(in.Tags) != numTags+1 {
178+
b.Fatalf("Expected %d tags, got %d, target = %s, tags = %v", numTags+1, len(in.Tags), in.Target, in.Tags)
179+
}
180+
// Reset so as to not game the allocations
181+
in.Tags = nil
182+
}
183+
b.SetBytes(int64(len(in.Target)))
184+
}
185+
186+
func randString(n int) string {
187+
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
188+
189+
b := make([]byte, n)
190+
for i := range b {
191+
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
192+
}
193+
return string(b)
194+
}

0 commit comments

Comments
 (0)