github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ts/metrics_test.go (about)

     1  // Copyright 2018 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 ts
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/ts/tspb"
    18  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    19  )
    20  
    21  func TestTimeSeriesWriteMetrics(t *testing.T) {
    22  	defer leaktest.AfterTest(t)()
    23  	tm := newTestModelRunner(t)
    24  	tm.Start()
    25  	defer tm.Stop()
    26  
    27  	metrics := tm.DB.Metrics()
    28  
    29  	tm.storeTimeSeriesData(resolution1ns, []tspb.TimeSeriesData{
    30  		tsd("test.multimetric", "source1",
    31  			tsdp(1, 100),
    32  			tsdp(15, 300),
    33  			tsdp(17, 500),
    34  			tsdp(52, 900),
    35  		),
    36  		tsd("test.multimetric", "source2",
    37  			tsdp(5, 100),
    38  			tsdp(16, 300),
    39  			tsdp(22, 500),
    40  			tsdp(82, 900),
    41  		),
    42  	})
    43  	tm.assertKeyCount(7)
    44  	tm.assertModelCorrect()
    45  
    46  	if a, e := metrics.WriteSamples.Count(), int64(8); a != e {
    47  		t.Fatalf("samples written was %d, wanted %d", a, e)
    48  	}
    49  
    50  	originalBytes := metrics.WriteBytes.Count()
    51  	if a, e := originalBytes, int64(0); a <= e {
    52  		t.Fatalf("sample bytes written was %d, wanted more than %d", a, e)
    53  	}
    54  
    55  	if a, e := metrics.WriteErrors.Count(), int64(0); a != e {
    56  		t.Fatalf("write error count was %d, wanted %d", a, e)
    57  	}
    58  
    59  	// Introduce an error into the db.
    60  	if err := tm.DB.StoreData(context.Background(), resolutionInvalid, []tspb.TimeSeriesData{
    61  		{
    62  			Name:   "test.multimetric",
    63  			Source: "source3",
    64  			Datapoints: []tspb.TimeSeriesDatapoint{
    65  				{
    66  					Value:          1,
    67  					TimestampNanos: 1,
    68  				},
    69  			},
    70  		},
    71  	}); err == nil {
    72  		t.Fatal("StoreData for invalid resolution did not throw error, wanted an error")
    73  	}
    74  
    75  	if a, e := metrics.WriteSamples.Count(), int64(8); a != e {
    76  		t.Fatalf("samples written was %d, wanted %d", a, e)
    77  	}
    78  
    79  	if a, e := metrics.WriteBytes.Count(), originalBytes; a != e {
    80  		t.Fatalf("sample bytes written was %d, wanted %d", a, e)
    81  	}
    82  
    83  	if a, e := metrics.WriteErrors.Count(), int64(1); a != e {
    84  		t.Fatalf("write error count was %d, wanted %d", a, e)
    85  	}
    86  }