-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathset_batch_size_test.go
More file actions
159 lines (152 loc) · 4.6 KB
/
set_batch_size_test.go
File metadata and controls
159 lines (152 loc) · 4.6 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) The Thanos Community Authors.
// Licensed under the Apache License 2.0.
package logicalplan
import (
"testing"
"github.com/thanos-io/promql-engine/query"
"github.com/efficientgo/core/testutil"
"github.com/prometheus/prometheus/promql/parser"
)
func TestSetBatchSize(t *testing.T) {
cases := []struct {
name string
expr string
expected string
}{
{
name: "selector",
expr: `http_requests_total`,
expected: `http_requests_total`,
},
{
name: "rate",
expr: `rate(http_requests_total[5m])`,
expected: `rate(http_requests_total[5m0s])`,
},
{
name: "sum",
expr: `sum(http_requests_total)`,
expected: `sum(http_requests_total[batch=10])`,
},
{
name: "quantile",
expr: `quantile(0.9, http_requests_total)`,
expected: `quantile(0.9, http_requests_total)`,
},
{
name: "two-level aggregation",
expr: `max by (pod) (sum by (pod) (http_requests_total))`,
expected: `max by (pod) (sum by (pod) (http_requests_total[batch=10]))`,
},
{
name: "aggregation of binary expression",
expr: `max by (pod) (metric_a / metric_b)`,
expected: `max by (pod) (metric_a / metric_b)`,
},
{
name: "binary operation of aggregations",
expr: `max(metric_a) / max(metric_b)`,
expected: `max(metric_a[batch=10]) / max(metric_b[batch=10])`,
},
{
name: "binary operation with same metric aggregations",
expr: `max(metric_a) / max(metric_a{code="foo"})`,
expected: `max(metric_a[batch=10]) / max(filter([code="foo"], metric_a[batch=10]))`,
},
{
name: `histogram quantile`,
expr: `histogram_quantile(0.5, metric_bucket)`,
expected: `histogram_quantile(0.5, metric_bucket)`,
},
{
name: "binary expression with time",
expr: `time() - max by (foo) (bar)`,
expected: `time() - max by (foo) (bar[batch=10])`,
},
{
name: "binary expression with single aggregation",
expr: `metric_a - max by (foo) (bar)`,
expected: `metric_a - max by (foo) (bar[batch=10])`,
},
{
name: "number literal",
expr: `1`,
expected: `1`,
},
{
name: "absent",
expr: `absent(foo)`,
expected: `absent(foo)`,
},
{
name: "histogram quantile with aggregation",
expr: `histogram_quantile(scalar(max(quantile)), http_requests_total)`,
expected: `histogram_quantile(scalar(max(quantile[batch=10])), http_requests_total)`,
},
// Range vector functions should allow batching to propagate
{
name: "sum with rate",
expr: `sum(rate(http_requests_total[5m]))`,
expected: `sum(rate(http_requests_total[batch=10][5m0s]))`,
},
{
name: "avg with increase",
expr: `avg(increase(http_requests_total[5m]))`,
expected: `avg(increase(http_requests_total[batch=10][5m0s]))`,
},
// Label-preserving functions should allow batching to propagate
{
name: "sum with abs",
expr: `sum(abs(metric))`,
expected: `sum(abs(metric[batch=10]))`,
},
{
name: "avg with ceil",
expr: `avg(ceil(metric))`,
expected: `avg(ceil(metric[batch=10]))`,
},
{
name: "max with clamp_max",
expr: `max(clamp_max(metric, 100))`,
expected: `max(clamp_max(metric[batch=10], 100))`,
},
{
name: "nested math functions",
expr: `sum(abs(floor(metric)))`,
expected: `sum(abs(floor(metric[batch=10])))`,
},
{
name: "aggregation with timestamp",
expr: `sum(timestamp(metric))`,
// timestamp() is pushed down into VectorSelector with SelectTimestamp=true
expected: `sum(metric[batch=10][timestamp])`,
},
// Label-modifying functions should disable batching
{
name: "label_replace disables batching",
expr: `sum(label_replace(metric, "dst", "$1", "src", "(.*)"))`,
expected: `sum(label_replace(metric[batch=10], "dst", "$1", "src", "(.*)"))`,
},
{
name: "label_join disables batching",
expr: `sum(label_join(metric, "dst", ",", "src"))`,
expected: `sum(label_join(metric[batch=10], "dst", ",", "src"))`,
},
{
name: "histogram_fraction disables batching",
expr: `sum(histogram_fraction(0, 0.5, metric))`,
expected: `sum(histogram_fraction(0, 0.5, metric))`,
},
}
optimizers := append([]Optimizer{SelectorBatchSize{Size: 10}}, DefaultOptimizers...)
for _, tcase := range cases {
t.Run(tcase.expr, func(t *testing.T) {
t.Parallel()
expr, err := parser.ParseExpr(tcase.expr)
testutil.Ok(t, err)
plan, _ := NewFromAST(expr, &query.Options{}, PlanOptions{})
optimizedPlan, _ := plan.Optimize(optimizers)
testutil.Equals(t, tcase.expected, renderExprTree(optimizedPlan.Root()))
})
}
}