forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_alias_test.go
More file actions
138 lines (132 loc) · 2.82 KB
/
Copy pathfunc_alias_test.go
File metadata and controls
138 lines (132 loc) · 2.82 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
package expr
import (
"math"
"strconv"
"testing"
"github.com/raintank/metrictank/api/models"
"gopkg.in/raintank/schema.v1"
)
func TestAliasSingle(t *testing.T) {
testAlias(
"single",
[]models.Series{
{
Target: "foo",
Datapoints: []schema.Point{
{Val: 0, Ts: 10},
{Val: math.NaN(), Ts: 20},
},
},
},
[]models.Series{
{
Target: "bar",
Datapoints: []schema.Point{
{Val: 0, Ts: 10},
{Val: math.NaN(), Ts: 20},
},
},
},
t,
)
}
func TestAliasMultiple(t *testing.T) {
testAlias(
"multiple",
[]models.Series{
{
Target: "foo-1",
Datapoints: []schema.Point{
{Val: 0, Ts: 10},
{Val: math.NaN(), Ts: 20},
},
},
{
Target: "foo-2",
Datapoints: []schema.Point{
{Val: 20, Ts: 10},
{Val: 100, Ts: 20},
},
},
},
[]models.Series{
{
Target: "bar",
Datapoints: []schema.Point{
{Val: 0, Ts: 10},
{Val: math.NaN(), Ts: 20},
},
},
{
Target: "bar",
Datapoints: []schema.Point{
{Val: 20, Ts: 10},
{Val: 100, Ts: 20},
},
},
},
t,
)
}
func testAlias(name string, in []models.Series, out []models.Series, t *testing.T) {
f := NewAlias()
alias := f.(*FuncAlias)
alias.alias = "bar"
alias.in = NewMock(in)
got, err := f.Exec(make(map[Req][]models.Series))
if err != nil {
t.Fatalf("case %q: err should be nil. got %q", name, err)
}
if len(got) != len(in) {
t.Fatalf("case %q: alias output should be same amount of series as input: %d, not %d", name, len(in), len(got))
}
for i, o := range out {
g := got[i]
if o.Target != g.Target {
t.Fatalf("case %q: expected target %q, got %q", name, o.Target, g.Target)
}
if len(o.Datapoints) != len(g.Datapoints) {
t.Fatalf("case %q: len output expected %d, got %d", name, len(o.Datapoints), len(g.Datapoints))
}
for j, p := range o.Datapoints {
bothNaN := math.IsNaN(p.Val) && math.IsNaN(g.Datapoints[j].Val)
if (bothNaN || p.Val == g.Datapoints[j].Val) && p.Ts == g.Datapoints[j].Ts {
continue
}
t.Fatalf("case %q: output point %d - expected %v got %v", name, j, p, g.Datapoints[j])
}
}
}
func BenchmarkAlias_1(b *testing.B) {
benchmarkAlias(b, 1)
}
func BenchmarkAlias_10(b *testing.B) {
benchmarkAlias(b, 10)
}
func BenchmarkAlias_100(b *testing.B) {
benchmarkAlias(b, 100)
}
func BenchmarkAlias_1000(b *testing.B) {
benchmarkAlias(b, 1000)
}
func benchmarkAlias(b *testing.B, numSeries int) {
var input []models.Series
for i := 0; i < numSeries; i++ {
series := models.Series{
Target: strconv.Itoa(i),
}
input = append(input, series)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
f := NewAlias()
alias := f.(*FuncAlias)
alias.alias = "new-name"
alias.in = NewMock(input)
got, err := f.Exec(make(map[Req][]models.Series))
if err != nil {
b.Fatalf("%s", err)
}
results = got
}
}