@@ -6,10 +6,12 @@ package factor
66import (
77 "math"
88 "sort"
9+ "strings"
910 "testing"
1011
1112 "github.com/pingcap/tiproxy/pkg/balance/metricsreader"
1213 "github.com/pingcap/tiproxy/pkg/util/monotime"
14+ "github.com/prometheus/common/expfmt"
1315 "github.com/prometheus/common/model"
1416 "github.com/stretchr/testify/require"
1517)
@@ -282,3 +284,60 @@ func TestHealthBalanceCount(t *testing.T) {
282284 require .Equal (t , test .count , count , "test idx: %d" , i )
283285 }
284286}
287+
288+ func TestHealthQueryRule (t * testing.T ) {
289+ tests := []struct {
290+ text string
291+ curValue []model.SampleValue
292+ finalValue []model.SampleValue
293+ }{
294+ {
295+ text : `tidb_tikvclient_backoff_seconds_count{type=""} 0
296+ tidb_tikvclient_backoff_seconds_count{type="dataNotReady"} 0
297+ tidb_tikvclient_backoff_seconds_count{type="pdRPC"} 0
298+ tidb_tikvclient_backoff_seconds_count{type="regionMiss"} 10
299+ tidb_tikvclient_backoff_seconds_count{type="tikvRPC"} 0
300+ ` ,
301+ curValue : []model.SampleValue {0 , 10 },
302+ finalValue : []model.SampleValue {model .SampleValue (math .NaN ()), model .SampleValue (math .NaN ())},
303+ },
304+ {
305+ text : `tidb_tikvclient_backoff_seconds_count{type=""} 10
306+ tidb_tikvclient_backoff_seconds_count{type="dataNotReady"} 10
307+ tidb_tikvclient_backoff_seconds_count{type="pdRPC"} 10
308+ tidb_tikvclient_backoff_seconds_count{type="regionMiss"} 110
309+ tidb_tikvclient_backoff_seconds_count{type="tikvRPC"} 100
310+ ` ,
311+ curValue : []model.SampleValue {10 , 210 },
312+ finalValue : []model.SampleValue {10 , 200 },
313+ },
314+ {
315+ text : `tidb_tikvclient_backoff_seconds_count{type=""} 10
316+ tidb_tikvclient_backoff_seconds_count{type="dataNotReady"} 10
317+ tidb_tikvclient_backoff_seconds_count{type="pdRPC"} 10
318+ tidb_tikvclient_backoff_seconds_count{type="regionMiss"} 110
319+ tidb_tikvclient_backoff_seconds_count{type="tikvRPC"} 100
320+ ` ,
321+ curValue : []model.SampleValue {10 , 210 },
322+ finalValue : []model.SampleValue {10 , 200 },
323+ },
324+ }
325+
326+ historyPair := make ([][]model.SamplePair , len (errDefinitions ))
327+ for i , test := range tests {
328+ var parser expfmt.TextParser
329+ mfs , err := parser .TextToMetricFamilies (strings .NewReader (test .text ))
330+ require .NoError (t , err , "case %d" , i )
331+ for j , ed := range errDefinitions {
332+ value := ed .queryRule .Metric2Value (mfs )
333+ require .Equal (t , test .curValue [j ], value , "case %d %d" , i , j )
334+ historyPair [j ] = append (historyPair [j ], model.SamplePair {Value : value })
335+ value = ed .queryRule .Range2Value (historyPair [j ])
336+ if math .IsNaN (float64 (test .finalValue [j ])) {
337+ require .True (t , math .IsNaN (float64 (value )), "case %d %d" , i , j )
338+ } else {
339+ require .Equal (t , test .finalValue [j ], value , "case %d %d" , i , j )
340+ }
341+ }
342+ }
343+ }
0 commit comments