2015-05-11 06:56:35 -07:00
// Copyright 2015 The Prometheus Authors
// 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.
2024-04-29 02:47:45 -07:00
package promqltest
2015-05-11 06:56:35 -07:00
import (
2017-10-24 21:21:42 -07:00
"context"
2023-08-18 11:48:59 -07:00
"embed"
2022-06-08 01:47:52 -07:00
"errors"
2015-05-11 06:56:35 -07:00
"fmt"
2023-08-18 11:48:59 -07:00
"io/fs"
2015-05-11 06:56:35 -07:00
"strconv"
"strings"
2023-08-18 11:48:59 -07:00
"testing"
2015-05-11 06:56:35 -07:00
"time"
2022-02-12 15:58:27 -08:00
"github.com/grafana/regexp"
2015-08-20 08:18:46 -07:00
"github.com/prometheus/common/model"
2021-09-13 12:19:20 -07:00
"github.com/stretchr/testify/require"
2020-10-22 02:00:08 -07:00
2021-11-08 06:23:17 -08:00
"github.com/prometheus/prometheus/model/exemplar"
2023-08-25 14:35:42 -07:00
"github.com/prometheus/prometheus/model/histogram"
2021-11-08 06:23:17 -08:00
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/timestamp"
2024-04-29 02:47:45 -07:00
"github.com/prometheus/prometheus/promql"
2020-02-03 08:32:23 -08:00
"github.com/prometheus/prometheus/promql/parser"
2023-09-14 09:57:31 -07:00
"github.com/prometheus/prometheus/promql/parser/posrange"
2015-05-11 06:56:35 -07:00
"github.com/prometheus/prometheus/storage"
2024-04-29 04:37:32 -07:00
"github.com/prometheus/prometheus/util/almost"
2019-08-08 18:35:39 -07:00
"github.com/prometheus/prometheus/util/teststorage"
2015-05-29 04:30:30 -07:00
"github.com/prometheus/prometheus/util/testutil"
2015-05-11 06:56:35 -07:00
)
var (
patSpace = regexp . MustCompile ( "[\t ]+" )
patLoad = regexp . MustCompile ( ` ^load\s+(.+?)$ ` )
patEvalInstant = regexp . MustCompile ( ` ^eval(?:_(fail|ordered))?\s+instant\s+(?:at\s+(.+?))?\s+(.+)$ ` )
2024-03-26 04:22:22 -07:00
patEvalRange = regexp . MustCompile ( ` ^eval(?:_(fail))?\s+range\s+from\s+(.+)\s+to\s+(.+)\s+step\s+(.+?)\s+(.+)$ ` )
2015-05-11 06:56:35 -07:00
)
const (
2024-04-29 05:13:23 -07:00
defaultEpsilon = 0.000001 // Relative error allowed for sample values.
DefaultMaxSamplesPerQuery = 10000
2015-05-11 06:56:35 -07:00
)
2020-03-29 09:35:39 -07:00
var testStartTime = time . Unix ( 0 , 0 ) . UTC ( )
2016-12-23 04:51:59 -08:00
2023-08-18 11:48:59 -07:00
// LoadedStorage returns storage with generated data using the provided load statements.
// Non-load statements will cause test errors.
func LoadedStorage ( t testutil . T , input string ) * teststorage . TestStorage {
test , err := newTest ( t , input )
require . NoError ( t , err )
for _ , cmd := range test . cmds {
switch cmd . ( type ) {
case * loadCmd :
require . NoError ( t , test . exec ( cmd , nil ) )
default :
t . Errorf ( "only 'load' commands accepted, got '%s'" , cmd )
}
}
return test . storage
}
2024-04-29 05:13:23 -07:00
func NewTestEngine ( enablePerStepStats bool , lookbackDelta time . Duration , maxSamples int ) * promql . Engine {
return promql . NewEngine ( promql . EngineOpts {
Logger : nil ,
Reg : nil ,
2024-05-04 06:05:11 -07:00
MaxSamples : maxSamples ,
2024-04-29 05:13:23 -07:00
Timeout : 100 * time . Second ,
NoStepSubqueryIntervalFn : func ( int64 ) int64 { return durationMilliseconds ( 1 * time . Minute ) } ,
EnableAtModifier : true ,
EnableNegativeOffset : true ,
EnablePerStepStats : enablePerStepStats ,
LookbackDelta : lookbackDelta ,
} )
}
2023-08-18 11:48:59 -07:00
// RunBuiltinTests runs an acceptance test suite against the provided engine.
2024-04-29 02:47:45 -07:00
func RunBuiltinTests ( t * testing . T , engine promql . QueryEngine ) {
2023-11-28 06:10:12 -08:00
t . Cleanup ( func ( ) { parser . EnableExperimentalFunctions = false } )
parser . EnableExperimentalFunctions = true
2023-08-18 11:48:59 -07:00
files , err := fs . Glob ( testsFs , "*/*.test" )
require . NoError ( t , err )
for _ , fn := range files {
t . Run ( fn , func ( t * testing . T ) {
content , err := fs . ReadFile ( testsFs , fn )
require . NoError ( t , err )
RunTest ( t , string ( content ) , engine )
} )
}
}
// RunTest parses and runs the test against the provided engine.
2024-04-29 02:47:45 -07:00
func RunTest ( t testutil . T , input string , engine promql . QueryEngine ) {
2024-03-26 04:22:22 -07:00
require . NoError ( t , runTest ( t , input , engine ) )
}
2024-04-29 02:47:45 -07:00
func runTest ( t testutil . T , input string , engine promql . QueryEngine ) error {
2023-08-18 11:48:59 -07:00
test , err := newTest ( t , input )
2024-03-26 04:22:22 -07:00
// Why do this before checking err? newTest() can create the test storage and then return an error,
// and we want to make sure to clean that up to avoid leaking goroutines.
2023-08-18 11:48:59 -07:00
defer func ( ) {
2024-03-26 04:22:22 -07:00
if test == nil {
return
}
2023-08-18 11:48:59 -07:00
if test . storage != nil {
test . storage . Close ( )
}
if test . cancelCtx != nil {
test . cancelCtx ( )
}
} ( )
2024-03-26 04:22:22 -07:00
if err != nil {
return err
}
2023-08-18 11:48:59 -07:00
for _ , cmd := range test . cmds {
2024-03-26 04:22:22 -07:00
if err := test . exec ( cmd , engine ) ; err != nil {
// TODO(fabxc): aggregate command errors, yield diffs for result
// comparison errors.
return err
}
2023-08-18 11:48:59 -07:00
}
2024-03-26 04:22:22 -07:00
return nil
2023-08-18 11:48:59 -07:00
}
// test is a sequence of read and write commands that are run
2015-05-11 06:56:35 -07:00
// against a test storage.
2023-08-18 11:48:59 -07:00
type test struct {
2015-05-23 03:23:33 -07:00
testutil . T
2015-05-11 06:56:35 -07:00
cmds [ ] testCommand
2020-04-29 09:16:14 -07:00
storage * teststorage . TestStorage
2016-12-25 02:34:22 -08:00
2023-08-18 11:48:59 -07:00
context context . Context
cancelCtx context . CancelFunc
2015-05-11 06:56:35 -07:00
}
2023-08-18 11:48:59 -07:00
// newTest returns an initialized empty Test.
func newTest ( t testutil . T , input string ) ( * test , error ) {
test := & test {
2015-05-11 06:56:35 -07:00
T : t ,
cmds : [ ] testCommand { } ,
}
err := test . parse ( input )
test . clear ( )
return test , err
}
2023-08-18 11:48:59 -07:00
//go:embed testdata
var testsFs embed . FS
2015-06-04 09:09:20 -07:00
2015-05-11 06:56:35 -07:00
func raise ( line int , format string , v ... interface { } ) error {
2020-02-03 09:00:41 -08:00
return & parser . ParseErr {
2020-02-03 09:48:27 -08:00
LineOffset : line ,
2022-06-08 01:47:52 -07:00
Err : fmt . Errorf ( format , v ... ) ,
2015-05-11 06:56:35 -07:00
}
}
2018-11-22 00:51:38 -08:00
func parseLoad ( lines [ ] string , i int ) ( int , * loadCmd , error ) {
2015-05-11 06:56:35 -07:00
if ! patLoad . MatchString ( lines [ i ] ) {
return i , nil , raise ( i , "invalid load command. (load <step:duration>)" )
}
parts := patLoad . FindStringSubmatch ( lines [ i ] )
2020-02-03 09:48:27 -08:00
gap , err := model . ParseDuration ( parts [ 1 ] )
2015-05-11 06:56:35 -07:00
if err != nil {
return i , nil , raise ( i , "invalid step definition %q: %s" , parts [ 1 ] , err )
}
2016-01-29 06:23:11 -08:00
cmd := newLoadCmd ( time . Duration ( gap ) )
2015-05-11 06:56:35 -07:00
for i + 1 < len ( lines ) {
i ++
defLine := lines [ i ]
if len ( defLine ) == 0 {
i --
break
}
2023-08-25 14:35:42 -07:00
metric , vals , err := parseSeries ( defLine , i )
2015-05-11 06:56:35 -07:00
if err != nil {
return i , nil , err
}
cmd . set ( metric , vals ... )
}
return i , cmd , nil
}
2023-08-25 14:35:42 -07:00
func parseSeries ( defLine string , line int ) ( labels . Labels , [ ] parser . SequenceValue , error ) {
metric , vals , err := parser . ParseSeriesDesc ( defLine )
if err != nil {
parser . EnrichParseError ( err , func ( parseErr * parser . ParseErr ) {
parseErr . LineOffset = line
} )
return labels . Labels { } , nil , err
}
return metric , vals , nil
}
2023-08-18 11:48:59 -07:00
func ( t * test ) parseEval ( lines [ ] string , i int ) ( int , * evalCmd , error ) {
2024-03-26 04:22:22 -07:00
instantParts := patEvalInstant . FindStringSubmatch ( lines [ i ] )
rangeParts := patEvalRange . FindStringSubmatch ( lines [ i ] )
if instantParts == nil && rangeParts == nil {
return i , nil , raise ( i , "invalid evaluation command. Must be either 'eval[_fail|_ordered] instant [at <offset:duration>] <query>' or 'eval[_fail] range from <from> to <to> step <step> <query>'" )
}
isInstant := instantParts != nil
var mod string
var expr string
if isInstant {
mod = instantParts [ 1 ]
expr = instantParts [ 3 ]
} else {
mod = rangeParts [ 1 ]
expr = rangeParts [ 5 ]
}
2020-02-03 09:00:41 -08:00
_ , err := parser . ParseExpr ( expr )
2015-05-11 06:56:35 -07:00
if err != nil {
2023-08-25 14:35:42 -07:00
parser . EnrichParseError ( err , func ( parseErr * parser . ParseErr ) {
parseErr . LineOffset = i
2023-09-14 09:57:31 -07:00
posOffset := posrange . Pos ( strings . Index ( lines [ i ] , expr ) )
2023-08-25 14:35:42 -07:00
parseErr . PositionRange . Start += posOffset
parseErr . PositionRange . End += posOffset
parseErr . Query = lines [ i ]
} )
2015-08-02 15:26:21 -07:00
return i , nil , err
2015-05-11 06:56:35 -07:00
}
2024-03-26 04:22:22 -07:00
formatErr := func ( format string , args ... any ) error {
combinedArgs := [ ] any { expr , i + 1 }
combinedArgs = append ( combinedArgs , args ... )
return fmt . Errorf ( "error in eval %s (line %v): " + format , combinedArgs ... )
}
var cmd * evalCmd
if isInstant {
at := instantParts [ 2 ]
offset , err := model . ParseDuration ( at )
if err != nil {
return i , nil , formatErr ( "invalid timestamp definition %q: %s" , at , err )
}
ts := testStartTime . Add ( time . Duration ( offset ) )
cmd = newInstantEvalCmd ( expr , ts , i + 1 )
} else {
from := rangeParts [ 2 ]
to := rangeParts [ 3 ]
step := rangeParts [ 4 ]
parsedFrom , err := model . ParseDuration ( from )
if err != nil {
return i , nil , formatErr ( "invalid start timestamp definition %q: %s" , from , err )
}
parsedTo , err := model . ParseDuration ( to )
if err != nil {
return i , nil , formatErr ( "invalid end timestamp definition %q: %s" , to , err )
}
if parsedTo < parsedFrom {
return i , nil , formatErr ( "invalid test definition, end timestamp (%s) is before start timestamp (%s)" , to , from )
}
parsedStep , err := model . ParseDuration ( step )
if err != nil {
return i , nil , formatErr ( "invalid step definition %q: %s" , step , err )
}
cmd = newRangeEvalCmd ( expr , testStartTime . Add ( time . Duration ( parsedFrom ) ) , testStartTime . Add ( time . Duration ( parsedTo ) ) , time . Duration ( parsedStep ) , i + 1 )
2015-05-11 06:56:35 -07:00
}
switch mod {
case "ordered" :
2024-03-26 04:22:22 -07:00
// Ordered results are not supported for range queries, but the regex for range query commands does not allow
// asserting an ordered result, so we don't need to do any error checking here.
2015-05-11 06:56:35 -07:00
cmd . ordered = true
case "fail" :
cmd . fail = true
}
for j := 1 ; i + 1 < len ( lines ) ; j ++ {
i ++
defLine := lines [ i ]
if len ( defLine ) == 0 {
i --
break
}
2024-06-06 08:56:25 -07:00
if cmd . fail && strings . HasPrefix ( defLine , "expected_fail_message" ) {
cmd . expectedFailMessage = strings . TrimSpace ( strings . TrimPrefix ( defLine , "expected_fail_message" ) )
break
}
if cmd . fail && strings . HasPrefix ( defLine , "expected_fail_regexp" ) {
pattern := strings . TrimSpace ( strings . TrimPrefix ( defLine , "expected_fail_regexp" ) )
cmd . expectedFailRegexp , err = regexp . Compile ( pattern )
if err != nil {
return i , nil , formatErr ( "invalid regexp '%s' for expected_fail_regexp: %w" , pattern , err )
}
break
}
2015-05-11 06:56:35 -07:00
if f , err := parseNumber ( defLine ) ; err == nil {
2022-03-09 14:16:54 -08:00
cmd . expect ( 0 , parser . SequenceValue { Value : f } )
2015-05-11 06:56:35 -07:00
break
}
2023-08-25 14:35:42 -07:00
metric , vals , err := parseSeries ( defLine , i )
2015-05-11 06:56:35 -07:00
if err != nil {
return i , nil , err
}
// Currently, we are not expecting any matrices.
2024-03-26 04:22:22 -07:00
if len ( vals ) > 1 && isInstant {
return i , nil , formatErr ( "expecting multiple values in instant evaluation not allowed" )
2015-05-11 06:56:35 -07:00
}
2022-03-09 14:16:54 -08:00
cmd . expectMetric ( j , metric , vals ... )
2015-05-11 06:56:35 -07:00
}
return i , cmd , nil
}
2018-11-22 00:51:38 -08:00
// getLines returns trimmed lines after removing the comments.
func getLines ( input string ) [ ] string {
2015-05-11 06:56:35 -07:00
lines := strings . Split ( input , "\n" )
for i , l := range lines {
l = strings . TrimSpace ( l )
if strings . HasPrefix ( l , "#" ) {
l = ""
}
lines [ i ] = l
}
2018-11-22 00:51:38 -08:00
return lines
}
2015-05-11 06:56:35 -07:00
2018-11-22 00:51:38 -08:00
// parse the given command sequence and appends it to the test.
2023-08-18 11:48:59 -07:00
func ( t * test ) parse ( input string ) error {
2018-11-22 00:51:38 -08:00
lines := getLines ( input )
var err error
2015-05-11 06:56:35 -07:00
// Scan for steps line by line.
for i := 0 ; i < len ( lines ) ; i ++ {
l := lines [ i ]
if len ( l ) == 0 {
continue
}
var cmd testCommand
switch c := strings . ToLower ( patSpace . Split ( l , 2 ) [ 0 ] ) ; {
case c == "clear" :
cmd = & clearCmd { }
case c == "load" :
2018-11-22 00:51:38 -08:00
i , cmd , err = parseLoad ( lines , i )
2015-05-11 06:56:35 -07:00
case strings . HasPrefix ( c , "eval" ) :
i , cmd , err = t . parseEval ( lines , i )
default :
return raise ( i , "invalid command %q" , l )
}
if err != nil {
return err
}
t . cmds = append ( t . cmds , cmd )
}
return nil
}
// testCommand is an interface that ensures that only the package internal
// types can be a valid command for a test.
type testCommand interface {
testCmd ( )
}
func ( * clearCmd ) testCmd ( ) { }
func ( * loadCmd ) testCmd ( ) { }
func ( * evalCmd ) testCmd ( ) { }
// loadCmd is a command that loads sequences of sample values for specific
// metrics into the storage.
type loadCmd struct {
2021-05-06 13:53:52 -07:00
gap time . Duration
metrics map [ uint64 ] labels . Labels
2024-04-29 02:47:45 -07:00
defs map [ uint64 ] [ ] promql . Sample
2021-05-06 13:53:52 -07:00
exemplars map [ uint64 ] [ ] exemplar . Exemplar
2015-05-11 06:56:35 -07:00
}
func newLoadCmd ( gap time . Duration ) * loadCmd {
return & loadCmd {
2021-05-06 13:53:52 -07:00
gap : gap ,
metrics : map [ uint64 ] labels . Labels { } ,
2024-04-29 02:47:45 -07:00
defs : map [ uint64 ] [ ] promql . Sample { } ,
2021-05-06 13:53:52 -07:00
exemplars : map [ uint64 ] [ ] exemplar . Exemplar { } ,
2015-05-11 06:56:35 -07:00
}
}
func ( cmd loadCmd ) String ( ) string {
return "load"
}
// set a sequence of sample values for the given metric.
2020-02-03 08:45:41 -08:00
func ( cmd * loadCmd ) set ( m labels . Labels , vals ... parser . SequenceValue ) {
2016-12-23 04:51:59 -08:00
h := m . Hash ( )
2015-05-11 06:56:35 -07:00
2024-04-29 02:47:45 -07:00
samples := make ( [ ] promql . Sample , 0 , len ( vals ) )
2015-05-11 06:56:35 -07:00
ts := testStartTime
for _ , v := range vals {
2020-02-03 09:48:27 -08:00
if ! v . Omitted {
2024-04-29 02:47:45 -07:00
samples = append ( samples , promql . Sample {
2016-12-24 02:23:06 -08:00
T : ts . UnixNano ( ) / int64 ( time . Millisecond / time . Nanosecond ) ,
promql: Separate `Point` into `FPoint` and `HPoint`
In other words: Instead of having a “polymorphous” `Point` that can
either contain a float value or a histogram value, use an `FPoint` for
floats and an `HPoint` for histograms.
This seemingly small change has a _lot_ of repercussions throughout
the codebase.
The idea here is to avoid the increase in size of `Point` arrays that
happened after native histograms had been added.
The higher-level data structures (`Sample`, `Series`, etc.) are still
“polymorphous”. The same idea could be applied to them, but at each
step the trade-offs needed to be evaluated.
The idea with this change is to do the minimum necessary to get back
to pre-histogram performance for functions that do not touch
histograms. Here are comparisons for the `changes` function. The test
data doesn't include histograms yet. Ideally, there would be no change
in the benchmark result at all.
First runtime v2.39 compared to directly prior to this commit:
```
name old time/op new time/op delta
RangeQuery/expr=changes(a_one[1d]),steps=1-16 391µs ± 2% 542µs ± 1% +38.58% (p=0.000 n=9+8)
RangeQuery/expr=changes(a_one[1d]),steps=10-16 452µs ± 2% 617µs ± 2% +36.48% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_one[1d]),steps=100-16 1.12ms ± 1% 1.36ms ± 2% +21.58% (p=0.000 n=8+10)
RangeQuery/expr=changes(a_one[1d]),steps=1000-16 7.83ms ± 1% 8.94ms ± 1% +14.21% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_ten[1d]),steps=1-16 2.98ms ± 0% 3.30ms ± 1% +10.67% (p=0.000 n=9+10)
RangeQuery/expr=changes(a_ten[1d]),steps=10-16 3.66ms ± 1% 4.10ms ± 1% +11.82% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_ten[1d]),steps=100-16 10.5ms ± 0% 11.8ms ± 1% +12.50% (p=0.000 n=8+10)
RangeQuery/expr=changes(a_ten[1d]),steps=1000-16 77.6ms ± 1% 87.4ms ± 1% +12.63% (p=0.000 n=9+9)
RangeQuery/expr=changes(a_hundred[1d]),steps=1-16 30.4ms ± 2% 32.8ms ± 1% +8.01% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=10-16 37.1ms ± 2% 40.6ms ± 2% +9.64% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=100-16 105ms ± 1% 117ms ± 1% +11.69% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=1000-16 783ms ± 3% 876ms ± 1% +11.83% (p=0.000 n=9+10)
```
And then runtime v2.39 compared to after this commit:
```
name old time/op new time/op delta
RangeQuery/expr=changes(a_one[1d]),steps=1-16 391µs ± 2% 547µs ± 1% +39.84% (p=0.000 n=9+8)
RangeQuery/expr=changes(a_one[1d]),steps=10-16 452µs ± 2% 616µs ± 2% +36.15% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_one[1d]),steps=100-16 1.12ms ± 1% 1.26ms ± 1% +12.20% (p=0.000 n=8+10)
RangeQuery/expr=changes(a_one[1d]),steps=1000-16 7.83ms ± 1% 7.95ms ± 1% +1.59% (p=0.000 n=10+8)
RangeQuery/expr=changes(a_ten[1d]),steps=1-16 2.98ms ± 0% 3.38ms ± 2% +13.49% (p=0.000 n=9+10)
RangeQuery/expr=changes(a_ten[1d]),steps=10-16 3.66ms ± 1% 4.02ms ± 1% +9.80% (p=0.000 n=10+9)
RangeQuery/expr=changes(a_ten[1d]),steps=100-16 10.5ms ± 0% 10.8ms ± 1% +3.08% (p=0.000 n=8+10)
RangeQuery/expr=changes(a_ten[1d]),steps=1000-16 77.6ms ± 1% 78.1ms ± 1% +0.58% (p=0.035 n=9+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=1-16 30.4ms ± 2% 33.5ms ± 4% +10.18% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=10-16 37.1ms ± 2% 40.0ms ± 1% +7.98% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=100-16 105ms ± 1% 107ms ± 1% +1.92% (p=0.000 n=10+10)
RangeQuery/expr=changes(a_hundred[1d]),steps=1000-16 783ms ± 3% 775ms ± 1% -1.02% (p=0.019 n=9+9)
```
In summary, the runtime doesn't really improve with this change for
queries with just a few steps. For queries with many steps, this
commit essentially reinstates the old performance. This is good
because the many-step queries are the one that matter most (longest
absolute runtime).
In terms of allocations, though, this commit doesn't make a dent at
all (numbers not shown). The reason is that most of the allocations
happen in the sampleRingIterator (in the storage package), which has
to be addressed in a separate commit.
Signed-off-by: beorn7 <beorn@grafana.com>
2022-10-28 07:58:40 -07:00
F : v . Value ,
2023-08-25 14:35:42 -07:00
H : v . Histogram ,
2015-05-11 06:56:35 -07:00
} )
}
ts = ts . Add ( cmd . gap )
}
2016-12-23 04:51:59 -08:00
cmd . defs [ h ] = samples
cmd . metrics [ h ] = m
2015-05-11 06:56:35 -07:00
}
// append the defined time series to the storage.
2017-01-13 05:48:01 -08:00
func ( cmd * loadCmd ) append ( a storage . Appender ) error {
2016-12-28 00:16:48 -08:00
for h , smpls := range cmd . defs {
m := cmd . metrics [ h ]
for _ , s := range smpls {
2023-08-25 14:35:42 -07:00
if err := appendSample ( a , s , m ) ; err != nil {
2017-01-13 05:48:01 -08:00
return err
}
2016-12-28 00:16:48 -08:00
}
}
2017-01-13 05:48:01 -08:00
return nil
2015-05-11 06:56:35 -07:00
}
2024-04-29 02:47:45 -07:00
func appendSample ( a storage . Appender , s promql . Sample , m labels . Labels ) error {
2023-08-25 14:35:42 -07:00
if s . H != nil {
if _ , err := a . AppendHistogram ( 0 , m , s . T , nil , s . H ) ; err != nil {
return err
}
} else {
if _ , err := a . Append ( 0 , m , s . T , s . F ) ; err != nil {
return err
}
}
return nil
}
2015-05-11 06:56:35 -07:00
// evalCmd is a command that evaluates an expression for the given time (range)
// and expects a specific result.
type evalCmd struct {
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
expr string
start time . Time
2024-03-26 04:22:22 -07:00
end time . Time
step time . Duration
2018-06-14 07:18:16 -07:00
line int
2015-05-11 06:56:35 -07:00
2024-06-06 08:56:25 -07:00
isRange bool // if false, instant query
fail , ordered bool
expectedFailMessage string
expectedFailRegexp * regexp . Regexp
2015-05-11 06:56:35 -07:00
2016-12-23 04:51:59 -08:00
metrics map [ uint64 ] labels . Labels
expected map [ uint64 ] entry
2015-05-11 06:56:35 -07:00
}
type entry struct {
pos int
2020-02-03 08:45:41 -08:00
vals [ ] parser . SequenceValue
2015-05-11 06:56:35 -07:00
}
func ( e entry ) String ( ) string {
return fmt . Sprintf ( "%d: %s" , e . pos , e . vals )
}
2024-03-26 04:22:22 -07:00
func newInstantEvalCmd ( expr string , start time . Time , line int ) * evalCmd {
2015-05-11 06:56:35 -07:00
return & evalCmd {
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
expr : expr ,
start : start ,
2018-06-14 07:18:16 -07:00
line : line ,
2015-05-11 06:56:35 -07:00
2016-12-23 04:51:59 -08:00
metrics : map [ uint64 ] labels . Labels { } ,
expected : map [ uint64 ] entry { } ,
2015-05-11 06:56:35 -07:00
}
}
2024-03-26 04:22:22 -07:00
func newRangeEvalCmd ( expr string , start , end time . Time , step time . Duration , line int ) * evalCmd {
return & evalCmd {
expr : expr ,
start : start ,
end : end ,
step : step ,
line : line ,
isRange : true ,
metrics : map [ uint64 ] labels . Labels { } ,
expected : map [ uint64 ] entry { } ,
}
}
2015-05-11 06:56:35 -07:00
func ( ev * evalCmd ) String ( ) string {
return "eval"
}
2022-03-09 14:16:54 -08:00
// expect adds a sequence of values to the set of expected
2015-05-11 06:56:35 -07:00
// results for the query.
2022-03-09 14:16:54 -08:00
func ( ev * evalCmd ) expect ( pos int , vals ... parser . SequenceValue ) {
ev . expected [ 0 ] = entry { pos : pos , vals : vals }
}
// expectMetric adds a new metric with a sequence of values to the set of expected
// results for the query.
func ( ev * evalCmd ) expectMetric ( pos int , m labels . Labels , vals ... parser . SequenceValue ) {
2016-12-23 04:51:59 -08:00
h := m . Hash ( )
ev . metrics [ h ] = m
ev . expected [ h ] = entry { pos : pos , vals : vals }
2015-05-11 06:56:35 -07:00
}
// compareResult compares the result value with the defined expectation.
2020-02-03 08:32:23 -08:00
func ( ev * evalCmd ) compareResult ( result parser . Value ) error {
2015-05-11 06:56:35 -07:00
switch val := result . ( type ) {
2024-04-29 02:47:45 -07:00
case promql . Matrix :
2024-03-26 04:22:22 -07:00
if ev . ordered {
return fmt . Errorf ( "expected ordered result, but query returned a matrix" )
}
if err := assertMatrixSorted ( val ) ; err != nil {
return err
}
seen := map [ uint64 ] bool { }
for _ , s := range val {
hash := s . Metric . Hash ( )
if _ , ok := ev . metrics [ hash ] ; ! ok {
2024-04-03 01:57:08 -07:00
return fmt . Errorf ( "unexpected metric %s in result, has %s" , s . Metric , formatSeriesResult ( s ) )
2024-03-26 04:22:22 -07:00
}
seen [ hash ] = true
exp := ev . expected [ hash ]
2024-04-29 02:47:45 -07:00
var expectedFloats [ ] promql . FPoint
var expectedHistograms [ ] promql . HPoint
2024-03-26 04:22:22 -07:00
for i , e := range exp . vals {
ts := ev . start . Add ( time . Duration ( i ) * ev . step )
if ts . After ( ev . end ) {
return fmt . Errorf ( "expected %v points for %s, but query time range cannot return this many points" , len ( exp . vals ) , ev . metrics [ hash ] )
}
t := ts . UnixNano ( ) / int64 ( time . Millisecond / time . Nanosecond )
if e . Histogram != nil {
2024-04-29 02:47:45 -07:00
expectedHistograms = append ( expectedHistograms , promql . HPoint { T : t , H : e . Histogram } )
2024-03-26 04:22:22 -07:00
} else if ! e . Omitted {
2024-04-29 02:47:45 -07:00
expectedFloats = append ( expectedFloats , promql . FPoint { T : t , F : e . Value } )
2024-03-26 04:22:22 -07:00
}
}
if len ( expectedFloats ) != len ( s . Floats ) || len ( expectedHistograms ) != len ( s . Histograms ) {
return fmt . Errorf ( "expected %v float points and %v histogram points for %s, but got %s" , len ( expectedFloats ) , len ( expectedHistograms ) , ev . metrics [ hash ] , formatSeriesResult ( s ) )
}
for i , expected := range expectedFloats {
actual := s . Floats [ i ]
if expected . T != actual . T {
return fmt . Errorf ( "expected float value at index %v for %s to have timestamp %v, but it had timestamp %v (result has %s)" , i , ev . metrics [ hash ] , expected . T , actual . T , formatSeriesResult ( s ) )
}
2024-04-29 04:37:32 -07:00
if ! almost . Equal ( actual . F , expected . F , defaultEpsilon ) {
2024-03-26 04:22:22 -07:00
return fmt . Errorf ( "expected float value at index %v (t=%v) for %s to be %v, but got %v (result has %s)" , i , actual . T , ev . metrics [ hash ] , expected . F , actual . F , formatSeriesResult ( s ) )
}
}
for i , expected := range expectedHistograms {
actual := s . Histograms [ i ]
if expected . T != actual . T {
return fmt . Errorf ( "expected histogram value at index %v for %s to have timestamp %v, but it had timestamp %v (result has %s)" , i , ev . metrics [ hash ] , expected . T , actual . T , formatSeriesResult ( s ) )
}
2024-03-27 11:00:16 -07:00
if ! actual . H . Equals ( expected . H . Compact ( 0 ) ) {
2024-03-26 04:22:22 -07:00
return fmt . Errorf ( "expected histogram value at index %v (t=%v) for %s to be %v, but got %v (result has %s)" , i , actual . T , ev . metrics [ hash ] , expected . H , actual . H , formatSeriesResult ( s ) )
}
}
}
for hash := range ev . expected {
if ! seen [ hash ] {
return fmt . Errorf ( "expected metric %s not found" , ev . metrics [ hash ] )
}
}
2015-05-11 06:56:35 -07:00
2024-04-29 02:47:45 -07:00
case promql . Vector :
2016-12-23 04:51:59 -08:00
seen := map [ uint64 ] bool { }
2015-05-11 06:56:35 -07:00
for pos , v := range val {
2016-12-23 04:51:59 -08:00
fp := v . Metric . Hash ( )
2015-05-11 06:56:35 -07:00
if _ , ok := ev . metrics [ fp ] ; ! ok {
2024-04-03 01:57:08 -07:00
if v . H != nil {
return fmt . Errorf ( "unexpected metric %s in result, has value %v" , v . Metric , v . H )
}
return fmt . Errorf ( "unexpected metric %s in result, has value %v" , v . Metric , v . F )
2015-05-11 06:56:35 -07:00
}
exp := ev . expected [ fp ]
if ev . ordered && exp . pos != pos + 1 {
2022-06-08 01:47:52 -07:00
return fmt . Errorf ( "expected metric %s with %v at position %d but was at %d" , v . Metric , exp . vals , exp . pos , pos + 1 )
2015-05-11 06:56:35 -07:00
}
2023-08-25 14:35:42 -07:00
exp0 := exp . vals [ 0 ]
expH := exp0 . Histogram
2024-03-26 04:22:22 -07:00
if expH == nil && v . H != nil {
return fmt . Errorf ( "expected float value %v for %s but got histogram %s" , exp0 , v . Metric , HistogramTestExpression ( v . H ) )
}
if expH != nil && v . H == nil {
return fmt . Errorf ( "expected histogram %s for %s but got float value %v" , HistogramTestExpression ( expH ) , v . Metric , v . F )
}
2024-03-27 11:00:16 -07:00
if expH != nil && ! expH . Compact ( 0 ) . Equals ( v . H ) {
2023-08-25 14:35:42 -07:00
return fmt . Errorf ( "expected %v for %s but got %s" , HistogramTestExpression ( expH ) , v . Metric , HistogramTestExpression ( v . H ) )
}
2024-04-29 04:37:32 -07:00
if ! almost . Equal ( exp0 . Value , v . F , defaultEpsilon ) {
2023-08-25 14:35:42 -07:00
return fmt . Errorf ( "expected %v for %s but got %v" , exp0 . Value , v . Metric , v . F )
2015-05-11 06:56:35 -07:00
}
seen [ fp ] = true
}
for fp , expVals := range ev . expected {
if ! seen [ fp ] {
2022-06-08 01:47:52 -07:00
return fmt . Errorf ( "expected metric %s with %v not found" , ev . metrics [ fp ] , expVals )
2015-05-11 06:56:35 -07:00
}
}
2024-04-29 02:47:45 -07:00
case promql . Scalar :
2023-08-25 14:35:42 -07:00
if len ( ev . expected ) != 1 {
return fmt . Errorf ( "expected vector result, but got scalar %s" , val . String ( ) )
}
exp0 := ev . expected [ 0 ] . vals [ 0 ]
if exp0 . Histogram != nil {
return fmt . Errorf ( "expected Histogram %v but got scalar %s" , exp0 . Histogram . TestExpression ( ) , val . String ( ) )
}
2024-04-29 04:37:32 -07:00
if ! almost . Equal ( exp0 . Value , val . V , defaultEpsilon ) {
2023-08-25 14:35:42 -07:00
return fmt . Errorf ( "expected Scalar %v but got %v" , val . V , exp0 . Value )
2015-05-11 06:56:35 -07:00
}
default :
2022-06-08 01:47:52 -07:00
panic ( fmt . Errorf ( "promql.Test.compareResult: unexpected result type %T" , result ) )
2015-05-11 06:56:35 -07:00
}
return nil
}
2024-06-06 08:56:25 -07:00
func ( ev * evalCmd ) checkExpectedFailure ( actual error ) error {
if ev . expectedFailMessage != "" {
if ev . expectedFailMessage != actual . Error ( ) {
return fmt . Errorf ( "expected error %q evaluating query %q (line %d), but got: %s" , ev . expectedFailMessage , ev . expr , ev . line , actual . Error ( ) )
}
}
if ev . expectedFailRegexp != nil {
if ! ev . expectedFailRegexp . MatchString ( actual . Error ( ) ) {
return fmt . Errorf ( "expected error matching pattern %q evaluating query %q (line %d), but got: %s" , ev . expectedFailRegexp . String ( ) , ev . expr , ev . line , actual . Error ( ) )
}
}
// We're not expecting a particular error, or we got the error we expected.
// This test passes.
return nil
}
2024-04-29 02:47:45 -07:00
func formatSeriesResult ( s promql . Series ) string {
2024-03-26 04:22:22 -07:00
floatPlural := "s"
histogramPlural := "s"
if len ( s . Floats ) == 1 {
floatPlural = ""
}
if len ( s . Histograms ) == 1 {
histogramPlural = ""
}
return fmt . Sprintf ( "%v float point%s %v and %v histogram point%s %v" , len ( s . Floats ) , floatPlural , s . Floats , len ( s . Histograms ) , histogramPlural , s . Histograms )
}
2023-08-25 14:35:42 -07:00
// HistogramTestExpression returns TestExpression() for the given histogram or "" if the histogram is nil.
func HistogramTestExpression ( h * histogram . FloatHistogram ) string {
if h != nil {
return h . TestExpression ( )
}
return ""
}
2015-05-11 06:56:35 -07:00
// clearCmd is a command that wipes the test's storage state.
type clearCmd struct { }
func ( cmd clearCmd ) String ( ) string {
return "clear"
}
2021-01-20 02:57:39 -08:00
type atModifierTestCase struct {
expr string
evalTime time . Time
}
func atModifierTestCases ( exprStr string , evalTime time . Time ) ( [ ] atModifierTestCase , error ) {
expr , err := parser . ParseExpr ( exprStr )
if err != nil {
return nil , err
}
ts := timestamp . FromTime ( evalTime )
containsNonStepInvariant := false
// Setting the @ timestamp for all selectors to be evalTime.
// If there is a subquery, then the selectors inside it don't get the @ timestamp.
// If any selector already has the @ timestamp set, then it is untouched.
parser . Inspect ( expr , func ( node parser . Node , path [ ] parser . Node ) error {
2024-05-04 06:22:28 -07:00
if hasAtModifier ( path ) {
2021-01-20 02:57:39 -08:00
// There is a subquery with timestamp in the path,
// hence don't change any timestamps further.
return nil
}
switch n := node . ( type ) {
case * parser . VectorSelector :
if n . Timestamp == nil {
n . Timestamp = makeInt64Pointer ( ts )
}
case * parser . MatrixSelector :
if vs := n . VectorSelector . ( * parser . VectorSelector ) ; vs . Timestamp == nil {
vs . Timestamp = makeInt64Pointer ( ts )
}
case * parser . SubqueryExpr :
if n . Timestamp == nil {
n . Timestamp = makeInt64Pointer ( ts )
}
case * parser . Call :
2024-04-29 02:47:45 -07:00
_ , ok := promql . AtModifierUnsafeFunctions [ n . Func . Name ]
2021-01-20 02:57:39 -08:00
containsNonStepInvariant = containsNonStepInvariant || ok
}
return nil
} )
if containsNonStepInvariant {
2022-11-28 09:09:18 -08:00
// Expression contains a function whose result can vary with evaluation
// time, even though its arguments are step invariant: skip it.
2021-01-20 02:57:39 -08:00
return nil , nil
}
newExpr := expr . String ( ) // With all the @ evalTime set.
additionalEvalTimes := [ ] int64 { - 10 * ts , 0 , ts / 5 , ts , 10 * ts }
if ts == 0 {
additionalEvalTimes = [ ] int64 { - 1000 , - ts , 1000 }
}
testCases := make ( [ ] atModifierTestCase , 0 , len ( additionalEvalTimes ) )
for _ , et := range additionalEvalTimes {
testCases = append ( testCases , atModifierTestCase {
expr : newExpr ,
evalTime : timestamp . Time ( et ) ,
} )
}
return testCases , nil
}
2024-05-04 06:22:28 -07:00
func hasAtModifier ( path [ ] parser . Node ) bool {
for _ , node := range path {
if n , ok := node . ( * parser . SubqueryExpr ) ; ok {
if n . Timestamp != nil {
return true
}
}
}
return false
}
2016-03-02 00:16:56 -08:00
// exec processes a single step of the test.
2024-04-29 02:47:45 -07:00
func ( t * test ) exec ( tc testCommand , engine promql . QueryEngine ) error {
2015-05-11 06:56:35 -07:00
switch cmd := tc . ( type ) {
case * clearCmd :
t . clear ( )
case * loadCmd :
2020-07-24 07:10:51 -07:00
app := t . storage . Appender ( t . context )
2017-01-13 05:48:01 -08:00
if err := cmd . append ( app ) ; err != nil {
app . Rollback ( )
return err
}
2016-12-25 02:34:22 -08:00
if err := app . Commit ( ) ; err != nil {
return err
}
2015-05-11 06:56:35 -07:00
case * evalCmd :
2024-03-26 04:22:22 -07:00
return t . execEval ( cmd , engine )
default :
panic ( "promql.Test.exec: unknown test command type" )
}
return nil
}
2024-04-29 02:47:45 -07:00
func ( t * test ) execEval ( cmd * evalCmd , engine promql . QueryEngine ) error {
2024-03-26 04:22:22 -07:00
if cmd . isRange {
return t . execRangeEval ( cmd , engine )
}
return t . execInstantEval ( cmd , engine )
}
2024-04-29 02:47:45 -07:00
func ( t * test ) execRangeEval ( cmd * evalCmd , engine promql . QueryEngine ) error {
2024-03-26 04:22:22 -07:00
q , err := engine . NewRangeQuery ( t . context , t . storage , nil , cmd . expr , cmd . start , cmd . end , cmd . step )
if err != nil {
2024-04-03 01:57:08 -07:00
return fmt . Errorf ( "error creating range query for %q (line %d): %w" , cmd . expr , cmd . line , err )
2024-03-26 04:22:22 -07:00
}
res := q . Exec ( t . context )
if res . Err != nil {
if cmd . fail {
2024-06-06 08:56:25 -07:00
return cmd . checkExpectedFailure ( res . Err )
2024-03-26 04:22:22 -07:00
}
return fmt . Errorf ( "error evaluating query %q (line %d): %w" , cmd . expr , cmd . line , res . Err )
}
if res . Err == nil && cmd . fail {
return fmt . Errorf ( "expected error evaluating query %q (line %d) but got none" , cmd . expr , cmd . line )
}
defer q . Close ( )
if err := cmd . compareResult ( res . Value ) ; err != nil {
return fmt . Errorf ( "error in %s %s (line %d): %w" , cmd , cmd . expr , cmd . line , err )
}
return nil
}
2024-04-29 02:47:45 -07:00
func ( t * test ) execInstantEval ( cmd * evalCmd , engine promql . QueryEngine ) error {
2024-03-26 04:22:22 -07:00
queries , err := atModifierTestCases ( cmd . expr , cmd . start )
if err != nil {
return err
}
queries = append ( [ ] atModifierTestCase { { expr : cmd . expr , evalTime : cmd . start } } , queries ... )
for _ , iq := range queries {
q , err := engine . NewInstantQuery ( t . context , t . storage , nil , iq . expr , iq . evalTime )
2018-08-17 08:24:35 -07:00
if err != nil {
2024-04-03 01:57:08 -07:00
return fmt . Errorf ( "error creating instant query for %q (line %d): %w" , cmd . expr , cmd . line , err )
2018-08-17 08:24:35 -07:00
}
2024-03-26 04:22:22 -07:00
defer q . Close ( )
res := q . Exec ( t . context )
if res . Err != nil {
if cmd . fail {
2024-06-06 08:56:25 -07:00
if err := cmd . checkExpectedFailure ( res . Err ) ; err != nil {
return err
}
2021-01-20 02:57:39 -08:00
continue
}
2024-03-26 04:22:22 -07:00
return fmt . Errorf ( "error evaluating query %q (line %d): %w" , iq . expr , cmd . line , res . Err )
}
if res . Err == nil && cmd . fail {
return fmt . Errorf ( "expected error evaluating query %q (line %d) but got none" , iq . expr , cmd . line )
}
err = cmd . compareResult ( res . Value )
if err != nil {
return fmt . Errorf ( "error in %s %s (line %d): %w" , cmd , iq . expr , cmd . line , err )
}
// Check query returns same result in range mode,
// by checking against the middle step.
q , err = engine . NewRangeQuery ( t . context , t . storage , nil , iq . expr , iq . evalTime . Add ( - time . Minute ) , iq . evalTime . Add ( time . Minute ) , time . Minute )
if err != nil {
2024-04-03 01:57:08 -07:00
return fmt . Errorf ( "error creating range query for %q (line %d): %w" , cmd . expr , cmd . line , err )
2024-03-26 04:22:22 -07:00
}
rangeRes := q . Exec ( t . context )
if rangeRes . Err != nil {
return fmt . Errorf ( "error evaluating query %q (line %d) in range mode: %w" , iq . expr , cmd . line , rangeRes . Err )
}
defer q . Close ( )
if cmd . ordered {
// Range queries are always sorted by labels, so skip this test case that expects results in a particular order.
continue
}
2024-04-29 02:47:45 -07:00
mat := rangeRes . Value . ( promql . Matrix )
2024-03-26 04:22:22 -07:00
if err := assertMatrixSorted ( mat ) ; err != nil {
return err
}
2024-04-29 02:47:45 -07:00
vec := make ( promql . Vector , 0 , len ( mat ) )
2024-03-26 04:22:22 -07:00
for _ , series := range mat {
// We expect either Floats or Histograms.
for _ , point := range series . Floats {
if point . T == timeMilliseconds ( iq . evalTime ) {
2024-04-29 02:47:45 -07:00
vec = append ( vec , promql . Sample { Metric : series . Metric , T : point . T , F : point . F } )
2024-03-26 04:22:22 -07:00
break
2023-08-25 14:35:42 -07:00
}
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
}
2024-03-26 04:22:22 -07:00
for _ , point := range series . Histograms {
if point . T == timeMilliseconds ( iq . evalTime ) {
2024-04-29 02:47:45 -07:00
vec = append ( vec , promql . Sample { Metric : series . Metric , T : point . T , H : point . H } )
2024-03-26 04:22:22 -07:00
break
}
2021-01-20 02:57:39 -08:00
}
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
}
2024-04-29 02:47:45 -07:00
if _ , ok := res . Value . ( promql . Scalar ) ; ok {
err = cmd . compareResult ( promql . Scalar { V : vec [ 0 ] . F } )
2024-03-26 04:22:22 -07:00
} else {
err = cmd . compareResult ( vec )
}
if err != nil {
return fmt . Errorf ( "error in %s %s (line %d) range mode: %w" , cmd , iq . expr , cmd . line , err )
}
}
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
2024-03-26 04:22:22 -07:00
return nil
}
2024-04-29 02:47:45 -07:00
func assertMatrixSorted ( m promql . Matrix ) error {
2024-03-26 04:22:22 -07:00
if len ( m ) <= 1 {
return nil
2015-05-11 06:56:35 -07:00
}
2024-03-26 04:22:22 -07:00
for i , s := range m [ : len ( m ) - 1 ] {
nextIndex := i + 1
nextMetric := m [ nextIndex ] . Metric
if labels . Compare ( s . Metric , nextMetric ) > 0 {
return fmt . Errorf ( "matrix results should always be sorted by labels, but matrix is not sorted: series at index %v with labels %s sorts before series at index %v with labels %s" , nextIndex , nextMetric , i , s . Metric )
}
}
2015-05-11 06:56:35 -07:00
return nil
}
// clear the current test storage of all inserted samples.
2023-08-18 11:48:59 -07:00
func ( t * test ) clear ( ) {
2016-12-25 02:34:22 -08:00
if t . storage != nil {
2021-09-13 12:19:20 -07:00
err := t . storage . Close ( )
require . NoError ( t . T , err , "Unexpected error while closing test storage." )
2015-05-11 06:56:35 -07:00
}
2016-09-15 15:58:06 -07:00
if t . cancelCtx != nil {
t . cancelCtx ( )
2015-05-11 06:56:35 -07:00
}
2019-08-08 18:35:39 -07:00
t . storage = teststorage . New ( t )
2016-09-15 15:58:06 -07:00
t . context , t . cancelCtx = context . WithCancel ( context . Background ( ) )
2015-05-11 06:56:35 -07:00
}
func parseNumber ( s string ) ( float64 , error ) {
2020-02-03 09:48:27 -08:00
n , err := strconv . ParseInt ( s , 0 , 64 )
2015-05-11 06:56:35 -07:00
f := float64 ( n )
if err != nil {
2020-02-03 09:48:27 -08:00
f , err = strconv . ParseFloat ( s , 64 )
2015-05-11 06:56:35 -07:00
}
if err != nil {
2022-06-08 01:47:52 -07:00
return 0 , fmt . Errorf ( "error parsing number: %w" , err )
2015-05-11 06:56:35 -07:00
}
return f , nil
}
2018-11-22 00:51:38 -08:00
// LazyLoader lazily loads samples into storage.
// This is specifically implemented for unit testing of rules.
type LazyLoader struct {
loadCmd * loadCmd
2021-03-07 00:19:33 -08:00
storage storage . Storage
SubqueryInterval time . Duration
2018-11-22 00:51:38 -08:00
2024-04-29 02:47:45 -07:00
queryEngine * promql . Engine
2018-11-22 00:51:38 -08:00
context context . Context
cancelCtx context . CancelFunc
2021-06-30 14:43:39 -07:00
opts LazyLoaderOpts
}
// LazyLoaderOpts are options for the lazy loader.
type LazyLoaderOpts struct {
2022-01-11 08:01:02 -08:00
// Both of these must be set to true for regular PromQL (as of
// Prometheus v2.33). They can still be disabled here for legacy and
// other uses.
2021-06-30 14:43:39 -07:00
EnableAtModifier , EnableNegativeOffset bool
2018-11-22 00:51:38 -08:00
}
// NewLazyLoader returns an initialized empty LazyLoader.
2024-03-21 02:23:40 -07:00
func NewLazyLoader ( input string , opts LazyLoaderOpts ) ( * LazyLoader , error ) {
2018-11-22 00:51:38 -08:00
ll := & LazyLoader {
2021-06-30 14:43:39 -07:00
opts : opts ,
2018-11-22 00:51:38 -08:00
}
err := ll . parse ( input )
2024-03-21 02:23:40 -07:00
if err != nil {
return nil , err
}
err = ll . clear ( )
2018-11-22 00:51:38 -08:00
return ll , err
}
// parse the given load command.
func ( ll * LazyLoader ) parse ( input string ) error {
lines := getLines ( input )
// Accepts only 'load' command.
for i := 0 ; i < len ( lines ) ; i ++ {
l := lines [ i ]
if len ( l ) == 0 {
continue
}
if strings . ToLower ( patSpace . Split ( l , 2 ) [ 0 ] ) == "load" {
_ , cmd , err := parseLoad ( lines , i )
if err != nil {
return err
}
ll . loadCmd = cmd
return nil
}
2019-01-08 10:26:02 -08:00
return raise ( i , "invalid command %q" , l )
2018-11-22 00:51:38 -08:00
}
return errors . New ( "no \"load\" command found" )
}
// clear the current test storage of all inserted samples.
2024-03-21 02:23:40 -07:00
func ( ll * LazyLoader ) clear ( ) error {
2018-11-22 00:51:38 -08:00
if ll . storage != nil {
2024-03-21 02:23:40 -07:00
if err := ll . storage . Close ( ) ; err != nil {
return fmt . Errorf ( "closing test storage: %w" , err )
}
2018-11-22 00:51:38 -08:00
}
if ll . cancelCtx != nil {
ll . cancelCtx ( )
}
2024-03-21 02:23:40 -07:00
var err error
ll . storage , err = teststorage . NewWithError ( )
if err != nil {
return err
}
2018-11-22 00:51:38 -08:00
2024-04-29 02:47:45 -07:00
opts := promql . EngineOpts {
2021-03-07 00:19:33 -08:00
Logger : nil ,
Reg : nil ,
MaxSamples : 10000 ,
Timeout : 100 * time . Second ,
NoStepSubqueryIntervalFn : func ( int64 ) int64 { return durationMilliseconds ( ll . SubqueryInterval ) } ,
2021-06-30 14:43:39 -07:00
EnableAtModifier : ll . opts . EnableAtModifier ,
EnableNegativeOffset : ll . opts . EnableNegativeOffset ,
2018-11-22 00:51:38 -08:00
}
2024-04-29 02:47:45 -07:00
ll . queryEngine = promql . NewEngine ( opts )
2018-11-22 00:51:38 -08:00
ll . context , ll . cancelCtx = context . WithCancel ( context . Background ( ) )
2024-03-21 02:23:40 -07:00
return nil
2018-11-22 00:51:38 -08:00
}
// appendTill appends the defined time series to the storage till the given timestamp (in milliseconds).
func ( ll * LazyLoader ) appendTill ( ts int64 ) error {
2020-07-24 07:10:51 -07:00
app := ll . storage . Appender ( ll . Context ( ) )
2018-11-22 00:51:38 -08:00
for h , smpls := range ll . loadCmd . defs {
m := ll . loadCmd . metrics [ h ]
for i , s := range smpls {
if s . T > ts {
// Removing the already added samples.
ll . loadCmd . defs [ h ] = smpls [ i : ]
break
}
2023-08-25 14:35:42 -07:00
if err := appendSample ( app , s , m ) ; err != nil {
2018-11-22 00:51:38 -08:00
return err
}
2019-01-07 05:37:27 -08:00
if i == len ( smpls ) - 1 {
ll . loadCmd . defs [ h ] = nil
}
2018-11-22 00:51:38 -08:00
}
}
return app . Commit ( )
}
// WithSamplesTill loads the samples till given timestamp and executes the given function.
func ( ll * LazyLoader ) WithSamplesTill ( ts time . Time , fn func ( error ) ) {
2020-03-29 09:35:39 -07:00
tsMilli := ts . Sub ( time . Unix ( 0 , 0 ) . UTC ( ) ) / time . Millisecond
2018-11-22 00:51:38 -08:00
fn ( ll . appendTill ( int64 ( tsMilli ) ) )
}
// QueryEngine returns the LazyLoader's query engine.
2024-04-29 02:47:45 -07:00
func ( ll * LazyLoader ) QueryEngine ( ) * promql . Engine {
2018-11-22 00:51:38 -08:00
return ll . queryEngine
}
// Queryable allows querying the LazyLoader's data.
// Note: only the samples till the max timestamp used
2022-09-07 02:30:48 -07:00
// in `WithSamplesTill` can be queried.
2018-11-22 00:51:38 -08:00
func ( ll * LazyLoader ) Queryable ( ) storage . Queryable {
return ll . storage
}
// Context returns the LazyLoader's context.
func ( ll * LazyLoader ) Context ( ) context . Context {
return ll . context
}
// Storage returns the LazyLoader's storage.
func ( ll * LazyLoader ) Storage ( ) storage . Storage {
return ll . storage
}
// Close closes resources associated with the LazyLoader.
2024-03-21 02:23:40 -07:00
func ( ll * LazyLoader ) Close ( ) error {
2018-11-22 00:51:38 -08:00
ll . cancelCtx ( )
2024-03-21 02:23:40 -07:00
return ll . storage . Close ( )
2018-11-22 00:51:38 -08:00
}
2024-04-29 02:47:45 -07:00
func makeInt64Pointer ( val int64 ) * int64 {
valp := new ( int64 )
* valp = val
return valp
}
func timeMilliseconds ( t time . Time ) int64 {
return t . UnixNano ( ) / int64 ( time . Millisecond / time . Nanosecond )
}
func durationMilliseconds ( d time . Duration ) int64 {
return int64 ( d / ( time . Millisecond / time . Nanosecond ) )
}