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

Change query fuzz test for bottomk, topk #6350

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions integration/query_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,36 @@ func TestProtobufCodecFuzz(t *testing.T) {
runQueryFuzzTestCases(t, ps, c1, c2, now, start, end, scrapeInterval, 1000)
}

var sampleNumComparer = cmp.Comparer(func(x, y model.Value) bool {
if x.Type() != y.Type() {
return false
}

vx, xvec := x.(model.Vector)
vy, yvec := y.(model.Vector)

if xvec && yvec {
return len(vx) == len(vy)
}

mx, xmat := x.(model.Matrix)
my, ymat := y.(model.Matrix)

mxSamples := 0
mySamples := 0

if xmat && ymat {
for i := 0; i < len(mx); i++ {
mxSamples += len(mx[i].Values)
}
for i := 0; i < len(my); i++ {
mySamples += len(my[i].Values)
}
}

return mxSamples == mySamples
})

// comparer should be used to compare promql results between engines.
var comparer = cmp.Comparer(func(x, y model.Value) bool {
if x.Type() != y.Type() {
Expand Down Expand Up @@ -1354,6 +1384,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
break
}
}

res1, err1 := c1.Query(query, queryTime)
res2, err2 := c2.Query(query, queryTime)
cases = append(cases, &testCase{
Expand All @@ -1374,6 +1405,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
break
}
}

res1, err1 := c1.QueryRange(query, start, end, step)
res2, err2 := c2.QueryRange(query, start, end, step)
cases = append(cases, &testCase{
Expand All @@ -1397,6 +1429,11 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
t.Logf("case %d error mismatch.\n%s: %s\nerr1: %v\nerr2: %v\n", i, qt, tc.query, tc.err1, tc.err2)
failures++
}
} else if shouldUseSampleNumComparer(tc.query) {
if !cmp.Equal(tc.res1, tc.res2, sampleNumComparer) {
t.Logf("case %d # of samples mismatch.\n%s: %s\nres1: %s\nres2: %s\n", i, qt, tc.query, tc.res1.String(), tc.res2.String())
failures++
}
} else if !cmp.Equal(tc.res1, tc.res2, comparer) {
t.Logf("case %d results mismatch.\n%s: %s\nres1: %s\nres2: %s\n", i, qt, tc.query, tc.res1.String(), tc.res2.String())
failures++
Expand All @@ -1407,6 +1444,13 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
}
}

func shouldUseSampleNumComparer(query string) bool {
if strings.Contains(query, "bottomk") || strings.Contains(query, "topk") {
return true
}
return false
}

func isValidQuery(generatedQuery parser.Expr, maxDepth int) bool {
isValid := true
currentDepth := 0
Expand Down
Loading