Skip to content

Commit 87ab76d

Browse files
committed
Add sampleNumComparer for bottomk, topk query fuzz test
Signed-off-by: SungJin1212 <[email protected]>
1 parent 71dccee commit 87ab76d

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

integration/query_fuzz_test.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,41 @@ func TestProtobufCodecFuzz(t *testing.T) {
634634
runQueryFuzzTestCases(t, ps, c1, c2, now, start, end, scrapeInterval, 1000)
635635
}
636636

637+
var sampleNumComparer = cmp.Comparer(func(x, y model.Value) bool {
638+
if x.Type() != y.Type() {
639+
return false
640+
}
641+
642+
vx, xvec := x.(model.Vector)
643+
vy, yvec := y.(model.Vector)
644+
645+
if xvec && yvec {
646+
return len(vx) == len(vy)
647+
}
648+
649+
mx, xmat := x.(model.Matrix)
650+
my, ymat := y.(model.Matrix)
651+
652+
mxSamples := 0
653+
mySamples := 0
654+
655+
if xmat && ymat {
656+
for i := 0; i < len(mx); i++ {
657+
mxSamples += len(mx[i].Values)
658+
}
659+
for i := 0; i < len(my); i++ {
660+
mySamples += len(my[i].Values)
661+
}
662+
}
663+
664+
fmt.Println("mxSamples", mxSamples, "mySamples", mySamples)
665+
if mxSamples == mySamples {
666+
return true
667+
}
668+
669+
return false
670+
})
671+
637672
// comparer should be used to compare promql results between engines.
638673
var comparer = cmp.Comparer(func(x, y model.Value) bool {
639674
if x.Type() != y.Type() {
@@ -1354,6 +1389,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
13541389
break
13551390
}
13561391
}
1392+
13571393
res1, err1 := c1.Query(query, queryTime)
13581394
res2, err2 := c2.Query(query, queryTime)
13591395
cases = append(cases, &testCase{
@@ -1374,7 +1410,8 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
13741410
break
13751411
}
13761412
}
1377-
res1, err1 := c1.QueryRange(query, start, end, step)
1413+
1414+
res1, err1 := c2.QueryRange(query, start, end, step)
13781415
res2, err2 := c2.QueryRange(query, start, end, step)
13791416
cases = append(cases, &testCase{
13801417
query: query,
@@ -1397,6 +1434,11 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
13971434
t.Logf("case %d error mismatch.\n%s: %s\nerr1: %v\nerr2: %v\n", i, qt, tc.query, tc.err1, tc.err2)
13981435
failures++
13991436
}
1437+
} else if shouldUseSampleNumComparer(tc.query) {
1438+
if !cmp.Equal(tc.res1, tc.res2, sampleNumComparer) {
1439+
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())
1440+
failures++
1441+
}
14001442
} else if !cmp.Equal(tc.res1, tc.res2, comparer) {
14011443
t.Logf("case %d results mismatch.\n%s: %s\nres1: %s\nres2: %s\n", i, qt, tc.query, tc.res1.String(), tc.res2.String())
14021444
failures++
@@ -1407,6 +1449,13 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
14071449
}
14081450
}
14091451

1452+
func shouldUseSampleNumComparer(query string) bool {
1453+
if strings.Contains(query, "bottomk") || strings.Contains(query, "topk") {
1454+
return true
1455+
}
1456+
return false
1457+
}
1458+
14101459
func isValidQuery(generatedQuery parser.Expr, maxDepth int) bool {
14111460
isValid := true
14121461
currentDepth := 0

0 commit comments

Comments
 (0)