forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_test.go
130 lines (116 loc) · 3.11 KB
/
matrix_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
package logql
import (
"testing"
"time"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql"
"github.com/stretchr/testify/require"
)
func TestMatrixStepper(t *testing.T) {
var (
start = time.Unix(0, 0)
end = time.Unix(6, 0)
step = time.Second
)
m := promql.Matrix{
promql.Series{
Metric: labels.FromStrings("foo", "bar"),
Floats: []promql.FPoint{
{T: start.UnixNano(), F: 0},
{T: start.Add(step).UnixNano() / int64(time.Millisecond), F: 1},
{T: start.Add(2*step).UnixNano() / int64(time.Millisecond), F: 2},
{T: start.Add(3*step).UnixNano() / int64(time.Millisecond), F: 3},
{T: start.Add(4*step).UnixNano() / int64(time.Millisecond), F: 4},
{T: start.Add(5*step).UnixNano() / int64(time.Millisecond), F: 5},
},
},
promql.Series{
Metric: labels.FromStrings("bazz", "buzz"),
Floats: []promql.FPoint{
{T: start.Add(2*step).UnixNano() / int64(time.Millisecond), F: 2},
{T: start.Add(4*step).UnixNano() / int64(time.Millisecond), F: 4},
},
},
}
s := NewMatrixStepEvaluator(start, end, step, m)
expected := []promql.Vector{
{
promql.Sample{
T: start.UnixNano(), F: 0,
Metric: labels.FromStrings("foo", "bar"),
},
},
{
promql.Sample{
T: start.Add(step).UnixNano() / int64(time.Millisecond), F: 1,
Metric: labels.FromStrings("foo", "bar"),
},
},
{
promql.Sample{
T: start.Add(2*step).UnixNano() / int64(time.Millisecond), F: 2,
Metric: labels.FromStrings("foo", "bar"),
},
promql.Sample{
T: start.Add(2*step).UnixNano() / int64(time.Millisecond), F: 2,
Metric: labels.FromStrings("bazz", "buzz"),
},
},
{
promql.Sample{
T: start.Add(3*step).UnixNano() / int64(time.Millisecond), F: 3,
Metric: labels.FromStrings("foo", "bar"),
},
},
{
promql.Sample{
T: start.Add(4*step).UnixNano() / int64(time.Millisecond), F: 4,
Metric: labels.FromStrings("foo", "bar"),
},
promql.Sample{
T: start.Add(4*step).UnixNano() / int64(time.Millisecond), F: 4,
Metric: labels.FromStrings("bazz", "buzz"),
},
},
{
promql.Sample{
T: start.Add(5*step).UnixNano() / int64(time.Millisecond), F: 5,
Metric: labels.FromStrings("foo", "bar"),
},
},
{},
}
for i := 0; i <= int(end.Sub(start)/step); i++ {
ok, ts, vec := s.Next()
require.Equal(t, ok, true)
require.Equal(t, start.Add(step*time.Duration(i)).UnixNano()/int64(time.Millisecond), ts)
require.Equal(t, expected[i], vec.SampleVector())
}
ok, _, _ := s.Next()
require.Equal(t, ok, false)
}
func Test_SingleStepMatrix(t *testing.T) {
var (
start = time.Unix(0, 0)
end = time.Unix(0, 0)
step = time.Second
)
m := promql.Matrix{
promql.Series{
Metric: labels.EmptyLabels(),
Floats: []promql.FPoint{
{T: start.UnixNano(), F: 10},
},
},
}
s := NewMatrixStepEvaluator(start, end, step, m)
ok, ts, vec := s.Next()
require.True(t, ok)
require.Equal(t, start.UnixNano(), ts)
require.Equal(t, promql.Vector{promql.Sample{
T: start.UnixNano(), F: 10,
Metric: labels.EmptyLabels(),
}}, vec.SampleVector())
ok, _, _ = s.Next()
require.False(t, ok)
}