Skip to content

Commit 246d811

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

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

integration/query_fuzz_test.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,45 @@ 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+
if len(vx) != len(vy) {
647+
return false
648+
}
649+
650+
return true
651+
}
652+
653+
mx, xmat := x.(model.Matrix)
654+
my, ymat := y.(model.Matrix)
655+
656+
mxSamples := 0
657+
mySamples := 0
658+
659+
if xmat && ymat {
660+
for i := 0; i < len(mx); i++ {
661+
mxSamples += len(mx[i].Values)
662+
}
663+
for i := 0; i < len(my); i++ {
664+
mySamples += len(my[i].Values)
665+
}
666+
}
667+
668+
fmt.Println("mxSamples", mxSamples, "mySamples", mySamples)
669+
if mxSamples == mySamples {
670+
return true
671+
}
672+
673+
return false
674+
})
675+
637676
// comparer should be used to compare promql results between engines.
638677
var comparer = cmp.Comparer(func(x, y model.Value) bool {
639678
if x.Type() != y.Type() {
@@ -1354,6 +1393,7 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
13541393
break
13551394
}
13561395
}
1396+
13571397
res1, err1 := c1.Query(query, queryTime)
13581398
res2, err2 := c2.Query(query, queryTime)
13591399
cases = append(cases, &testCase{
@@ -1374,7 +1414,8 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
13741414
break
13751415
}
13761416
}
1377-
res1, err1 := c1.QueryRange(query, start, end, step)
1417+
1418+
res1, err1 := c2.QueryRange(query, start, end, step)
13781419
res2, err2 := c2.QueryRange(query, start, end, step)
13791420
cases = append(cases, &testCase{
13801421
query: query,
@@ -1397,6 +1438,11 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
13971438
t.Logf("case %d error mismatch.\n%s: %s\nerr1: %v\nerr2: %v\n", i, qt, tc.query, tc.err1, tc.err2)
13981439
failures++
13991440
}
1441+
} else if shouldUseSampleNumComparer(tc.query) {
1442+
if !cmp.Equal(tc.res1, tc.res2, sampleNumComparer) {
1443+
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())
1444+
failures++
1445+
}
14001446
} else if !cmp.Equal(tc.res1, tc.res2, comparer) {
14011447
t.Logf("case %d results mismatch.\n%s: %s\nres1: %s\nres2: %s\n", i, qt, tc.query, tc.res1.String(), tc.res2.String())
14021448
failures++
@@ -1407,6 +1453,13 @@ func runQueryFuzzTestCases(t *testing.T, ps *promqlsmith.PromQLSmith, c1, c2 *e2
14071453
}
14081454
}
14091455

1456+
func shouldUseSampleNumComparer(query string) bool {
1457+
if strings.Contains(query, "bottomk") || strings.Contains(query, "topk") {
1458+
return true
1459+
}
1460+
return false
1461+
}
1462+
14101463
func isValidQuery(generatedQuery parser.Expr, maxDepth int) bool {
14111464
isValid := true
14121465
currentDepth := 0

0 commit comments

Comments
 (0)