github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/metrics/gauge_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func BenchmarkGuage(b *testing.B) {
     9  	g := NewGauge()
    10  	b.ResetTimer()
    11  	for i := 0; i < b.N; i++ {
    12  		g.Update(int64(i))
    13  	}
    14  }
    15  
    16  func TestGauge(t *testing.T) {
    17  	g := NewGauge()
    18  	g.Update(int64(47))
    19  	if v := g.Value(); 47 != v {
    20  		t.Errorf("g.Value(): 47 != %v\n", v)
    21  	}
    22  }
    23  
    24  func TestGaugeSnapshot(t *testing.T) {
    25  	g := NewGauge()
    26  	g.Update(int64(47))
    27  	snapshot := g.Snapshot()
    28  	g.Update(int64(0))
    29  	if v := snapshot.Value(); 47 != v {
    30  		t.Errorf("g.Value(): 47 != %v\n", v)
    31  	}
    32  }
    33  
    34  func TestGetOrRegisterGauge(t *testing.T) {
    35  	r := NewRegistry()
    36  	NewRegisteredGauge("foo", r).Update(47)
    37  	if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
    38  		t.Fatal(g)
    39  	}
    40  }
    41  
    42  func TestFunctionalGauge(t *testing.T) {
    43  	var counter int64
    44  	fg := NewFunctionalGauge(func() int64 {
    45  		counter++
    46  		return counter
    47  	})
    48  	fg.Value()
    49  	fg.Value()
    50  	if counter != 2 {
    51  		t.Error("counter != 2")
    52  	}
    53  }
    54  
    55  func TestGetOrRegisterFunctionalGauge(t *testing.T) {
    56  	r := NewRegistry()
    57  	NewRegisteredFunctionalGauge("foo", r, func() int64 { return 47 })
    58  	if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
    59  		t.Fatal(g)
    60  	}
    61  }
    62  
    63  func ExampleGetOrRegisterGauge() {
    64  	m := "server.bytes_sent"
    65  	g := GetOrRegisterGauge(m, nil)
    66  	g.Update(47)
    67  	fmt.Println(g.Value())
    68  }