2015-05-12 11:41:57 +00:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
2015-03-30 17:13:36 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
2022-12-20 16:58:29 +00:00
|
|
|
"context"
|
2015-05-12 11:41:57 +00:00
|
|
|
"path/filepath"
|
2022-12-20 16:58:29 +00:00
|
|
|
"strings"
|
2015-03-30 17:13:36 +00:00
|
|
|
"testing"
|
2022-12-20 16:58:29 +00:00
|
|
|
"time"
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-12-20 16:58:29 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/util/teststorage"
|
2015-03-30 17:13:36 +00:00
|
|
|
)
|
|
|
|
|
2015-05-12 11:41:57 +00:00
|
|
|
func TestEvaluations(t *testing.T) {
|
|
|
|
files, err := filepath.Glob("testdata/*.test")
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2015-05-12 11:41:57 +00:00
|
|
|
for _, fn := range files {
|
2020-07-31 15:03:02 +00:00
|
|
|
t.Run(fn, func(t *testing.T) {
|
|
|
|
test, err := newTestFromFile(t, fn)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, test.Run())
|
2019-10-10 00:06:53 +00:00
|
|
|
|
2020-07-31 15:03:02 +00:00
|
|
|
test.Close()
|
|
|
|
})
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-20 16:58:29 +00:00
|
|
|
|
|
|
|
// Run a lot of queries at the same time, to check for race conditions.
|
|
|
|
func TestConcurrentRangeQueries(t *testing.T) {
|
|
|
|
stor := teststorage.New(t)
|
|
|
|
defer stor.Close()
|
|
|
|
opts := EngineOpts{
|
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 50000000,
|
|
|
|
Timeout: 100 * time.Second,
|
|
|
|
}
|
|
|
|
engine := NewEngine(opts)
|
|
|
|
|
|
|
|
const interval = 10000 // 10s interval.
|
|
|
|
// A day of data plus 10k steps.
|
|
|
|
numIntervals := 8640 + 10000
|
|
|
|
err := setupRangeQueryTestData(stor, engine, interval, numIntervals)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cases := rangeQueryCases()
|
|
|
|
|
|
|
|
// Limit the number of queries running at the same time.
|
|
|
|
const numConcurrent = 4
|
|
|
|
sem := make(chan struct{}, numConcurrent)
|
|
|
|
for i := 0; i < numConcurrent; i++ {
|
|
|
|
sem <- struct{}{}
|
|
|
|
}
|
|
|
|
var g errgroup.Group
|
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
|
|
|
if strings.Contains(c.expr, "count_values") && c.steps > 10 {
|
|
|
|
continue // This test is too big to run with -race.
|
|
|
|
}
|
2023-03-08 14:23:47 +00:00
|
|
|
if strings.Contains(c.expr, "[1d]") && c.steps > 100 {
|
|
|
|
continue // This test is too slow.
|
|
|
|
}
|
2022-12-20 16:58:29 +00:00
|
|
|
<-sem
|
|
|
|
g.Go(func() error {
|
|
|
|
defer func() {
|
|
|
|
sem <- struct{}{}
|
|
|
|
}()
|
|
|
|
qry, err := engine.NewRangeQuery(
|
|
|
|
stor, nil, c.expr,
|
|
|
|
time.Unix(int64((numIntervals-c.steps)*10), 0),
|
|
|
|
time.Unix(int64(numIntervals*10), 0), time.Second*10)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res := qry.Exec(context.Background())
|
|
|
|
if res.Err != nil {
|
2023-01-19 15:52:29 +00:00
|
|
|
t.Logf("Query: %q, steps: %d, result: %s", c.expr, c.steps, res.Err)
|
2022-12-20 16:58:29 +00:00
|
|
|
return res.Err
|
|
|
|
}
|
|
|
|
qry.Close()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err = g.Wait()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|