github.com/gogf/gf/v2@v2.7.4/os/gmetric/gmetric_z_unit_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gmetric_test
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/v2/os/gmetric"
    14  	"github.com/gogf/gf/v2/test/gtest"
    15  )
    16  
    17  func Test_Counter(t *testing.T) {
    18  	gtest.C(t, func(t *gtest.T) {
    19  		var (
    20  			meterOption = gmetric.MeterOption{
    21  				Instrument:        "github.com/gogf/gf/example/metric/basic",
    22  				InstrumentVersion: "v1.0",
    23  			}
    24  			metricName   = "goframe.metric.demo.counter"
    25  			metricOption = gmetric.MetricOption{
    26  				Help: "This is a simple demo for Counter usage",
    27  				Unit: "%",
    28  				Attributes: gmetric.Attributes{
    29  					gmetric.NewAttribute("const_label_a", 1),
    30  				},
    31  			}
    32  			meter   = gmetric.GetGlobalProvider().Meter(meterOption)
    33  			counter = meter.MustCounter(metricName, metricOption)
    34  		)
    35  		t.Assert(counter.Info().Name(), metricName)
    36  		t.Assert(counter.Info().Help(), metricOption.Help)
    37  		t.Assert(counter.Info().Unit(), metricOption.Unit)
    38  		t.Assert(counter.Info().Attributes(), metricOption.Attributes)
    39  		t.Assert(counter.Info().Instrument().Name(), meterOption.Instrument)
    40  		t.Assert(counter.Info().Instrument().Version(), meterOption.InstrumentVersion)
    41  	})
    42  }
    43  
    44  func Test_Histogram(t *testing.T) {
    45  	gtest.C(t, func(t *gtest.T) {
    46  		var (
    47  			meterOption = gmetric.MeterOption{
    48  				Instrument:        "github.com/gogf/gf/example/metric/basic",
    49  				InstrumentVersion: "v1.0",
    50  			}
    51  			metricName   = "goframe.metric.demo.histogram"
    52  			metricOption = gmetric.MetricOption{
    53  				Help: "This is a simple demo for Histogram usage",
    54  				Unit: "%",
    55  				Attributes: gmetric.Attributes{
    56  					gmetric.NewAttribute("const_label_a", 1),
    57  				},
    58  				Buckets: []float64{0, 10, 20, 50, 100, 500, 1000, 2000, 5000, 10000},
    59  			}
    60  			meter     = gmetric.GetGlobalProvider().Meter(meterOption)
    61  			histogram = meter.MustHistogram(metricName, metricOption)
    62  		)
    63  		t.Assert(histogram.Info().Name(), metricName)
    64  		t.Assert(histogram.Info().Help(), metricOption.Help)
    65  		t.Assert(histogram.Info().Unit(), metricOption.Unit)
    66  		t.Assert(histogram.Info().Attributes(), metricOption.Attributes)
    67  		t.Assert(histogram.Info().Instrument().Name(), meterOption.Instrument)
    68  		t.Assert(histogram.Info().Instrument().Version(), meterOption.InstrumentVersion)
    69  		t.Assert(histogram.Buckets(), metricOption.Buckets)
    70  	})
    71  }
    72  
    73  func Test_CommonAttributes(t *testing.T) {
    74  	gtest.C(t, func(t *gtest.T) {
    75  		commonAttributes := gmetric.CommonAttributes()
    76  		t.AssertGT(len(commonAttributes), 1)
    77  		fmt.Println(commonAttributes)
    78  	})
    79  }