github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/rtree/logging_test.go (about)

     1  // Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0.
     2  
     3  package rtree_test
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  
     9  	. "github.com/pingcap/check"
    10  	backuppb "github.com/pingcap/kvproto/pkg/backup"
    11  	"go.uber.org/zap"
    12  	"go.uber.org/zap/zapcore"
    13  
    14  	"github.com/pingcap/br/pkg/rtree"
    15  )
    16  
    17  var _ = Suite(&testLoggingSuite{})
    18  
    19  type testLoggingSuite struct{}
    20  
    21  func (s *testLoggingSuite) TestLogRanges(c *C) {
    22  	cases := []struct {
    23  		count  int
    24  		expect string
    25  	}{
    26  		{0, `{"ranges": {"total": 0, "ranges": [], "totalFiles": 0, "totalKVs": 0, "totalBytes": 0, "totalSize": 0}}`},
    27  		{1, `{"ranges": {"total": 1, "ranges": ["[30, 31)"], "totalFiles": 1, "totalKVs": 0, "totalBytes": 0, "totalSize": 0}}`},
    28  		{2, `{"ranges": {"total": 2, "ranges": ["[30, 31)", "[31, 32)"], "totalFiles": 2, "totalKVs": 1, "totalBytes": 1, "totalSize": 1}}`},
    29  		{3, `{"ranges": {"total": 3, "ranges": ["[30, 31)", "[31, 32)", "[32, 33)"], "totalFiles": 3, "totalKVs": 3, "totalBytes": 3, "totalSize": 3}}`},
    30  		{4, `{"ranges": {"total": 4, "ranges": ["[30, 31)", "[31, 32)", "[32, 33)", "[33, 34)"], "totalFiles": 4, "totalKVs": 6, "totalBytes": 6, "totalSize": 6}}`},
    31  		{5, `{"ranges": {"total": 5, "ranges": ["[30, 31)", "(skip 3)", "[34, 35)"], "totalFiles": 5, "totalKVs": 10, "totalBytes": 10, "totalSize": 10}}`},
    32  		{6, `{"ranges": {"total": 6, "ranges": ["[30, 31)", "(skip 4)", "[35, 36)"], "totalFiles": 6, "totalKVs": 15, "totalBytes": 15, "totalSize": 15}}`},
    33  		{1024, `{"ranges": {"total": 1024, "ranges": ["[30, 31)", "(skip 1022)", "[31303233, 31303234)"], "totalFiles": 1024, "totalKVs": 523776, "totalBytes": 523776, "totalSize": 523776}}`},
    34  	}
    35  
    36  	encoder := zapcore.NewConsoleEncoder(zapcore.EncoderConfig{})
    37  	for _, cs := range cases {
    38  		ranges := make([]rtree.Range, cs.count)
    39  		for j := 0; j < cs.count; j++ {
    40  			ranges[j] = *newRange([]byte(fmt.Sprintf("%d", j)), []byte(fmt.Sprintf("%d", j+1)))
    41  			ranges[j].Files = append(ranges[j].Files, &backuppb.File{TotalKvs: uint64(j), TotalBytes: uint64(j)})
    42  		}
    43  		out, err := encoder.EncodeEntry(zapcore.Entry{}, []zap.Field{rtree.ZapRanges(ranges)})
    44  		c.Assert(err, IsNil)
    45  		c.Assert(strings.TrimRight(out.String(), "\n"), Equals, cs.expect)
    46  	}
    47  }