github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/stats_test.go (about)

     1  // Copyright 2015 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package kvserver
    12  
    13  import (
    14  	"context"
    15  	"reflect"
    16  	"testing"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/stateloader"
    19  	"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
    20  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    21  	"github.com/cockroachdb/cockroach/pkg/util/stop"
    22  	"github.com/kr/pretty"
    23  )
    24  
    25  // initialStats are the stats for a Replica which has been created through
    26  // bootstrapRangeOnly. These stats are not empty because we call
    27  // writeInitialState().
    28  func initialStats() enginepb.MVCCStats {
    29  	return enginepb.MVCCStats{
    30  		SysBytes: 70,
    31  		SysCount: 2,
    32  	}
    33  }
    34  func TestRangeStatsEmpty(t *testing.T) {
    35  	defer leaktest.AfterTest(t)()
    36  	tc := testContext{
    37  		bootstrapMode: bootstrapRangeOnly,
    38  	}
    39  	stopper := stop.NewStopper()
    40  	defer stopper.Stop(context.Background())
    41  	tc.Start(t, stopper)
    42  
    43  	ms := tc.repl.GetMVCCStats()
    44  	if exp := initialStats(); !reflect.DeepEqual(ms, exp) {
    45  		t.Errorf("unexpected stats diff(exp, actual):\n%s", pretty.Diff(exp, ms))
    46  	}
    47  }
    48  
    49  func TestRangeStatsInit(t *testing.T) {
    50  	defer leaktest.AfterTest(t)()
    51  	tc := testContext{}
    52  	stopper := stop.NewStopper()
    53  	defer stopper.Stop(context.Background())
    54  	tc.Start(t, stopper)
    55  	ms := enginepb.MVCCStats{
    56  		LiveBytes:       1,
    57  		KeyBytes:        2,
    58  		ValBytes:        3,
    59  		IntentBytes:     4,
    60  		LiveCount:       5,
    61  		KeyCount:        6,
    62  		ValCount:        7,
    63  		IntentCount:     8,
    64  		IntentAge:       9,
    65  		GCBytesAge:      10,
    66  		LastUpdateNanos: 11,
    67  	}
    68  	rsl := stateloader.Make(tc.repl.RangeID)
    69  	if err := rsl.SetMVCCStats(context.Background(), tc.engine, &ms); err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	loadMS, err := rsl.LoadMVCCStats(context.Background(), tc.engine)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if !reflect.DeepEqual(ms, loadMS) {
    77  		t.Errorf("mvcc stats mismatch %+v != %+v", ms, loadMS)
    78  	}
    79  }