github.com/rcrowley/go-metrics@v0.0.0-20201227073835-cf1acfcdf475/gauge_float64_test.go (about)

     1  package metrics
     2  
     3  import "testing"
     4  
     5  func BenchmarkGuageFloat64(b *testing.B) {
     6  	g := NewGaugeFloat64()
     7  	b.ResetTimer()
     8  	for i := 0; i < b.N; i++ {
     9  		g.Update(float64(i))
    10  	}
    11  }
    12  
    13  func BenchmarkGuageFloat64Parallel(b *testing.B) {
    14  	g := NewGaugeFloat64()
    15  	b.ResetTimer()
    16  	b.RunParallel(func(pb *testing.PB) {
    17  		for pb.Next() {
    18  			g.Update(float64(1))
    19  		}
    20  	})
    21  }
    22  
    23  func TestGaugeFloat64(t *testing.T) {
    24  	g := NewGaugeFloat64()
    25  	g.Update(float64(47.0))
    26  	if v := g.Value(); float64(47.0) != v {
    27  		t.Errorf("g.Value(): 47.0 != %v\n", v)
    28  	}
    29  }
    30  
    31  func TestGaugeFloat64Snapshot(t *testing.T) {
    32  	g := NewGaugeFloat64()
    33  	g.Update(float64(47.0))
    34  	snapshot := g.Snapshot()
    35  	g.Update(float64(0))
    36  	if v := snapshot.Value(); float64(47.0) != v {
    37  		t.Errorf("g.Value(): 47.0 != %v\n", v)
    38  	}
    39  }
    40  
    41  func TestGetOrRegisterGaugeFloat64(t *testing.T) {
    42  	r := NewRegistry()
    43  	NewRegisteredGaugeFloat64("foo", r).Update(float64(47.0))
    44  	t.Logf("registry: %v", r)
    45  	if g := GetOrRegisterGaugeFloat64("foo", r); float64(47.0) != g.Value() {
    46  		t.Fatal(g)
    47  	}
    48  }
    49  
    50  func TestFunctionalGaugeFloat64(t *testing.T) {
    51  	var counter float64
    52  	fg := NewFunctionalGaugeFloat64(func() float64 {
    53  		counter++
    54  		return counter
    55  	})
    56  	fg.Value()
    57  	fg.Value()
    58  	if counter != 2 {
    59  		t.Error("counter != 2")
    60  	}
    61  }
    62  
    63  func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) {
    64  	r := NewRegistry()
    65  	NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 })
    66  	if g := GetOrRegisterGaugeFloat64("foo", r); 47 != g.Value() {
    67  		t.Fatal(g)
    68  	}
    69  }