Skip to content

Commit 64f25d3

Browse files
authored
Add sampleNumComparer for bottomk, topk query fuzz test (#6350)
Signed-off-by: SungJin1212 <[email protected]>
1 parent cdf9471 commit 64f25d3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

integration/query_fuzz_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,36 @@ func TestProtobufCodecFuzz(t *testing.T) {
751751
runQueryFuzzTestCases(t, ps, c1, c2, now, start, end, scrapeInterval, 1000)
752752
}
753753

754+
var sampleNumComparer = cmp.Comparer(func(x, y model.Value) bool {
755+
if x.Type() != y.Type() {
756+
return false
757+
}
758+
759+
vx, xvec := x.(model.Vector)
760+
vy, yvec := y.(model.Vector)
761+
762+
if xvec && yvec {
763+
return len(vx) == len(vy)
764+
}
765+
766+
mx, xmat := x.(model.Matrix)
767+
my, ymat := y.(model.Matrix)
768+
769+
mxSamples := 0
770+
mySamples := 0
771+
772+
if xmat && ymat {
773+
for i := 0; i < len(mx); i++ {
774+
mxSamples += len(mx[i].Values)
775+
}
776+
for i := 0; i < len(my); i++ {
777+
mySamples += len(my[i].Values)
778+
}
779+
}
780+
781+
return mxSamples == mySamples
782+
})
783+
754784
// comparer should be used to compare promql results between engines.
755785
var comparer = cmp.Comparer(func(x, y model.Value) bool {
756786
if x.Type() != y.Type() {
@@ -1471,6 +1501,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
14711501
break
14721502
}
14731503
}
1504+
14741505
res1, err1 := c1.Query(query, queryTime)
14751506
res2, err2 := c2.Query(query, queryTime)
14761507
cases = append(cases, &testCase{
@@ -1491,6 +1522,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
14911522
break
14921523
}
14931524
}
1525+
14941526
res1, err1 := c1.QueryRange(query, start, end, step)
14951527
res2, err2 := c2.QueryRange(query, start, end, step)
14961528
cases = append(cases, &testCase{
@@ -1514,6 +1546,11 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
15141546
t.Logf("case %d error mismatch.\n%s: %s\nerr1: %v\nerr2: %v\n", i, qt, tc.query, tc.err1, tc.err2)
15151547
failures++
15161548
}
1549+
} else if shouldUseSampleNumComparer(tc.query) {
1550+
if !cmp.Equal(tc.res1, tc.res2, sampleNumComparer) {
1551+
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())
1552+
failures++
1553+
}
15171554
} else if !cmp.Equal(tc.res1, tc.res2, comparer) {
15181555
t.Logf("case %d results mismatch.\n%s: %s\nres1: %s\nres2: %s\n", i, qt, tc.query, tc.res1.String(), tc.res2.String())
15191556
failures++
@@ -1524,6 +1561,13 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
15241561
}
15251562
}
15261563

1564+
func shouldUseSampleNumComparer(query string) bool {
1565+
if strings.Contains(query, "bottomk") || strings.Contains(query, "topk") {
1566+
return true
1567+
}
1568+
return false
1569+
}
1570+
15271571
func isValidQuery(generatedQuery parser.Expr, maxDepth int) bool {
15281572
isValid := true
15291573
currentDepth := 0

0 commit comments

Comments
 (0)