Skip to content

Commit

Permalink
add unit test for summaryStoresLoadByEngine
Browse files Browse the repository at this point in the history
Signed-off-by: bufferflies <1045931706@qq.com>
  • Loading branch information
bufferflies committed Apr 14, 2023
1 parent 73cb558 commit fc4bd54
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions pkg/statistics/store_collection_test.go
Expand Up @@ -15,6 +15,8 @@
package statistics

import (
"github.com/tikv/pd/pkg/core/constant"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -88,3 +90,62 @@ func TestStoreStatistics(t *testing.T) {
re.Equal(4, stats.LabelCounter["host:h2"])
re.Equal(2, stats.LabelCounter["zone:unknown"])
}

func TestSummaryStoreInfos(t *testing.T) {
re := require.New(t)
rw := Read
kind := constant.LeaderKind
collector := newTikvCollector()
storeHistoryLoad := NewStoreHistoryLoads(DimLen)
storeInfos := make(map[uint64]*StoreSummaryInfo)
storeLoads := make(map[uint64][]float64)
for _, storeID := range []int{1, 3} {
storeInfos[uint64(storeID)] = &StoreSummaryInfo{
isTiFlash: false,
StoreInfo: core.NewStoreInfo(&metapb.Store{Id: uint64(storeID), Address: "mock://tikv" + strconv.Itoa(storeID)}, core.SetLastHeartbeatTS(time.Now())),
}
storeLoads[uint64(storeID)] = []float64{1, 2, 0, 0, 5}
for i, v := range storeLoads[uint64(storeID)] {
storeLoads[uint64(storeID)][i] = v * float64(storeID)
}
}

// case 1: put one element into history load
details := summaryStoresLoadByEngine(storeInfos, storeLoads, storeHistoryLoad, nil, rw, kind, collector)
re.Len(details, 2)
re.Empty(details[0].LoadPred.Current.HistoryLoads)
re.Empty(details[1].LoadPred.Current.HistoryLoads)
expectHistoryLoads := []float64{1, 2, 5}
for _, storeID := range []uint64{1, 3} {
loads := storeHistoryLoad.Get(storeID, rw, kind)
for i := 0; i < len(loads); i++ {
for j := 0; j < len(loads[0]); j++ {
re.Equal(loads[i][j]/float64(storeID), expectHistoryLoads[i])
}
}
}

// case 2: put two elements into history load
historySampleInterval = 0
for i := 1; i < 10; i++ {
details = summaryStoresLoadByEngine(storeInfos, storeLoads, storeHistoryLoad, nil, rw, kind, collector)
expect := []float64{2, 4, 10}
for _, detail := range details {
loads := detail.LoadPred.Current.HistoryLoads
re.Len(loads[0], i)
storeID := detail.GetID()
for i := 0; i < len(loads); i++ {
for j := 0; j < len(loads[0]); j++ {
re.Equal(loads[i][j]/float64(storeID), expectHistoryLoads[i])
}
}

for i, loads := range detail.LoadPred.Expect.HistoryLoads {
for _, load := range loads {
re.Equal(load, expect[i])
}
}
}
}

}

0 comments on commit fc4bd54

Please sign in to comment.