Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[performance] metrics query: range vector support streaming agg when no overlap #7380

Merged
Prev Previous commit
Next Next commit
add compare test
  • Loading branch information
liguozhong committed Oct 14, 2022
commit c77feb00074cae92e39a526b31f92647de04fb5c
120 changes: 120 additions & 0 deletions pkg/logql/range_vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -56,6 +57,125 @@ func newPoint(t time.Time, v float64) promql.Point {
return promql.Point{T: t.UnixNano() / 1e+6, V: v}
}

func Benchmark_RangeVectorIteratorCompare(b *testing.B) {

buildStreamingIt := func() (RangeVectorIterator, error) {
tt := struct {
selRange int64
step int64
offset int64
start, end time.Time
}{
(5 * time.Second).Nanoseconds(), // no overlap
(30 * time.Second).Nanoseconds(),
0,
time.Unix(10, 0),
time.Unix(100, 0),
}

iter := newfakePeekingSampleIterator()
expr := &syntax.RangeAggregationExpr{Operation: syntax.OpRangeTypeCount}
selRange := tt.selRange
step := tt.step
start := tt.start.UnixNano()
end := tt.end.UnixNano()
offset := tt.offset
if step == 0 {
step = 1
}
if offset != 0 {
start = start - offset
end = end - offset
}

it := &streamRangeVectorIterator{
iter: iter,
step: step,
end: end,
selRange: selRange,
metrics: map[string]labels.Labels{},
r: expr,
current: start - step, // first loop iteration will set it to start
offset: offset,
}
return it, nil
}

buildBatchIt := func() (RangeVectorIterator, error) {
tt := struct {
selRange int64
step int64
offset int64
start, end time.Time
}{
(5 * time.Second).Nanoseconds(), // no overlap
(30 * time.Second).Nanoseconds(),
0,
time.Unix(10, 0),
time.Unix(100, 0),
}

iter := newfakePeekingSampleIterator()
expr := &syntax.RangeAggregationExpr{Operation: syntax.OpRangeTypeCount}
selRange := tt.selRange
step := tt.step
start := tt.start.UnixNano()
end := tt.end.UnixNano()
offset := tt.offset
if step == 0 {
step = 1
}
if offset != 0 {
start = start - offset
end = end - offset
}

vectorAggregator, err := aggregator(expr)
if err != nil {
return nil, err
}

return &batchRangeVectorIterator{
iter: iter,
step: step,
end: end,
selRange: selRange,
metrics: map[string]labels.Labels{},
window: map[string]*promql.Series{},
agg: vectorAggregator,
current: start - step, // first loop iteration will set it to start
offset: offset,
}, nil
}

b.Run("streaming agg", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
it, err := buildStreamingIt()
if err != nil {
b.Fatal(err)
}
for it.Next() {
_, _ = it.At()
}
}
})

b.Run("batch agg", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
it, err := buildBatchIt()
if err != nil {
b.Fatal(err)
}
for it.Next() {
_, _ = it.At()
}
}
})

}

func Benchmark_RangeVectorIterator(b *testing.B) {
b.ReportAllocs()
tt := struct {
Expand Down