trpc.group/trpc-go/trpc-go@v1.0.3/metrics/counter_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package metrics_test
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  	"trpc.group/trpc-go/trpc-go/metrics"
    21  )
    22  
    23  func Test_counter_Incr(t *testing.T) {
    24  	metrics.RegisterMetricsSink(&metrics.ConsoleSink{})
    25  	type fields struct {
    26  		name string
    27  	}
    28  	tests := []struct {
    29  		name   string
    30  		fields fields
    31  	}{
    32  		{"counter-1", fields{"counter-req.total"}},
    33  	}
    34  	for _, tt := range tests {
    35  		t.Run(tt.name, func(t *testing.T) {
    36  			c := metrics.Counter(tt.fields.name)
    37  			c.Incr()
    38  			c.IncrBy(10)
    39  			assert.NotNil(t, c)
    40  		})
    41  	}
    42  }
    43  
    44  func TestGetSameMetrics(t *testing.T) {
    45  	c1 := metrics.Counter("x")
    46  	c2 := metrics.Counter("x")
    47  	c3 := metrics.Counter("y")
    48  
    49  	assert.Equal(t, c1, c2)
    50  	assert.NotEqual(t, c1, c3)
    51  
    52  	g1 := metrics.Gauge("x")
    53  	g2 := metrics.Gauge("x")
    54  	g3 := metrics.Gauge("y")
    55  
    56  	assert.Equal(t, g1, g2)
    57  	assert.NotEqual(t, g1, g3)
    58  
    59  	t1 := metrics.Timer("x")
    60  	t2 := metrics.Timer("x")
    61  	t3 := metrics.Timer("y")
    62  
    63  	assert.Equal(t, t1, t2)
    64  	assert.NotEqual(t, t1, t3)
    65  
    66  	h1 := metrics.Histogram("x", metrics.NewDurationBounds())
    67  	h2 := metrics.Histogram("x", metrics.NewDurationBounds())
    68  	h3 := metrics.Histogram("y", metrics.NewDurationBounds())
    69  
    70  	assert.Equal(t, h1, h2)
    71  	assert.NotEqual(t, h1, h3)
    72  }
    73  
    74  func BenchmarkReportCounter(b *testing.B) {
    75  
    76  	b.RunParallel(func(pb *testing.PB) {
    77  		for pb.Next() {
    78  			c := metrics.Counter("x")
    79  			c.Incr()
    80  		}
    81  	})
    82  }
    83  
    84  func BenchmarkReportGauge(b *testing.B) {
    85  	b.RunParallel(func(pb *testing.PB) {
    86  		for pb.Next() {
    87  			c := metrics.Gauge("x")
    88  			c.Set(1)
    89  		}
    90  	})
    91  }
    92  
    93  func BenchmarkReportTimer(b *testing.B) {
    94  	b.RunParallel(func(pb *testing.PB) {
    95  		for pb.Next() {
    96  			c := metrics.Timer("x")
    97  			c.Record()
    98  		}
    99  	})
   100  }
   101  
   102  func BenchmarkReportHistogram(b *testing.B) {
   103  	b.RunParallel(func(pb *testing.PB) {
   104  		for pb.Next() {
   105  			c := metrics.Histogram("x", metrics.NewValueBounds(10, 20, 50, 100))
   106  			c.AddSample(1)
   107  		}
   108  	})
   109  }