@@ -3,6 +3,7 @@ package queryrange
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ querier_stats "github.com/cortexproject/cortex/pkg/querier/stats"
6
7
"net/http"
7
8
"strconv"
8
9
"testing"
@@ -173,6 +174,7 @@ func mkAPIResponseWithStats(start, end, step int64, withStats bool, oldFormat bo
173
174
})
174
175
175
176
stats .Samples .TotalQueryableSamples += i
177
+ stats .Samples .PeakSamples = max (stats .Samples .PeakSamples , i )
176
178
}
177
179
}
178
180
@@ -597,11 +599,13 @@ func TestShouldCache(t *testing.T) {
597
599
func TestPartition (t * testing.T ) {
598
600
t .Parallel ()
599
601
for _ , tc := range []struct {
600
- name string
601
- input tripperware.Request
602
- prevCachedResponse []tripperware.Extent
603
- expectedRequests []tripperware.Request
604
- expectedCachedResponse []tripperware.Response
602
+ name string
603
+ input tripperware.Request
604
+ prevCachedResponse []tripperware.Extent
605
+ expectedRequests []tripperware.Request
606
+ expectedCachedResponse []tripperware.Response
607
+ expectedScannedSamplesFromCachedResponse uint64
608
+ expectedPeakSamplesFromCachedResponse uint64
605
609
}{
606
610
{
607
611
name : "Test a complete hit." ,
@@ -822,6 +826,8 @@ func TestPartition(t *testing.T) {
822
826
expectedCachedResponse : []tripperware.Response {
823
827
mkAPIResponseWithStats (0 , 100 , 10 , true , false ),
824
828
},
829
+ expectedPeakSamplesFromCachedResponse : getPeakSamples (0 , 100 , 10 ),
830
+ expectedScannedSamplesFromCachedResponse : getScannedSamples (0 , 100 , 10 ),
825
831
},
826
832
827
833
{
@@ -836,6 +842,8 @@ func TestPartition(t *testing.T) {
836
842
expectedCachedResponse : []tripperware.Response {
837
843
mkAPIResponseWithStats (0 , 100 , 10 , true , false ),
838
844
},
845
+ expectedPeakSamplesFromCachedResponse : getPeakSamples (0 , 100 , 10 ),
846
+ expectedScannedSamplesFromCachedResponse : getScannedSamples (0 , 100 , 10 ),
839
847
},
840
848
841
849
{
@@ -886,6 +894,8 @@ func TestPartition(t *testing.T) {
886
894
expectedCachedResponse : []tripperware.Response {
887
895
mkAPIResponseWithStats (50 , 100 , 10 , true , false ),
888
896
},
897
+ expectedPeakSamplesFromCachedResponse : getPeakSamples (50 , 100 , 10 ),
898
+ expectedScannedSamplesFromCachedResponse : getScannedSamples (50 , 100 , 10 ),
889
899
},
890
900
{
891
901
name : "[stats] Test multiple partial hits." ,
@@ -907,6 +917,8 @@ func TestPartition(t *testing.T) {
907
917
mkAPIResponseWithStats (100 , 120 , 10 , true , false ),
908
918
mkAPIResponseWithStats (160 , 200 , 10 , true , false ),
909
919
},
920
+ expectedPeakSamplesFromCachedResponse : max (getPeakSamples (100 , 120 , 10 ), getPeakSamples (160 , 200 , 10 )),
921
+ expectedScannedSamplesFromCachedResponse : getScannedSamples (100 , 120 , 10 ) + getScannedSamples (160 , 200 , 10 ),
910
922
},
911
923
{
912
924
name : "[stats] Partial hits with tiny gap." ,
@@ -927,7 +939,8 @@ func TestPartition(t *testing.T) {
927
939
expectedCachedResponse : []tripperware.Response {
928
940
mkAPIResponseWithStats (100 , 120 , 10 , true , false ),
929
941
},
930
- },
942
+ expectedPeakSamplesFromCachedResponse : getPeakSamples (100 , 120 , 10 ),
943
+ expectedScannedSamplesFromCachedResponse : getScannedSamples (100 , 120 , 10 )},
931
944
{
932
945
name : "[stats] Extent is outside the range and the request has a single step (same start and end)." ,
933
946
input : & tripperware.PrometheusRequest {
@@ -957,6 +970,8 @@ func TestPartition(t *testing.T) {
957
970
expectedCachedResponse : []tripperware.Response {
958
971
mkAPIResponseWithStats (100 , 105 , 10 , true , false ),
959
972
},
973
+ expectedPeakSamplesFromCachedResponse : getPeakSamples (100 , 105 , 10 ),
974
+ expectedScannedSamplesFromCachedResponse : getScannedSamples (100 , 105 , 10 ),
960
975
},
961
976
{
962
977
name : "[stats] Test when hit has a large step and only a single sample extent with old format." ,
@@ -971,6 +986,8 @@ func TestPartition(t *testing.T) {
971
986
expectedCachedResponse : []tripperware.Response {
972
987
mkAPIResponseWithStats (100 , 105 , 10 , true , false ),
973
988
},
989
+ expectedPeakSamplesFromCachedResponse : getPeakSamples (100 , 105 , 10 ),
990
+ expectedScannedSamplesFromCachedResponse : getScannedSamples (100 , 105 , 10 ),
974
991
},
975
992
} {
976
993
tc := tc
@@ -980,10 +997,13 @@ func TestPartition(t *testing.T) {
980
997
extractor : PrometheusResponseExtractor {},
981
998
minCacheExtent : 10 ,
982
999
}
983
- reqs , resps , err := s .partition (tc .input , tc .prevCachedResponse )
1000
+ stats , ctx := querier_stats .ContextWithEmptyStats (context .Background ())
1001
+ reqs , resps , err := s .partition (ctx , tc .input , tc .prevCachedResponse )
984
1002
require .Nil (t , err )
985
1003
require .Equal (t , tc .expectedRequests , reqs )
986
1004
require .Equal (t , tc .expectedCachedResponse , resps )
1005
+ require .Equal (t , tc .expectedScannedSamplesFromCachedResponse , stats .ScannedSamples )
1006
+ require .Equal (t , tc .expectedPeakSamplesFromCachedResponse , stats .PeakSamples )
987
1007
})
988
1008
}
989
1009
}
@@ -1580,3 +1600,14 @@ func TestResultsCacheFillCompatibility(t *testing.T) {
1580
1600
func toMs (t time.Duration ) int64 {
1581
1601
return int64 (t / time .Millisecond )
1582
1602
}
1603
+
1604
+ func getScannedSamples (start , end , step uint64 ) uint64 {
1605
+ lastTerm := start + ((end - start )/ step )* step
1606
+ n := (lastTerm - start )/ step + 1
1607
+
1608
+ return (n * (2 * start + (n - 1 )* step )) / 2
1609
+ }
1610
+
1611
+ func getPeakSamples (start , end , step uint64 ) uint64 {
1612
+ return start + ((end - start )/ step )* step
1613
+ }
0 commit comments