github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/metrics/gauge_float64_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:41</date>
    10  //</624342648710238208>
    11  
    12  package metrics
    13  
    14  import "testing"
    15  
    16  func BenchmarkGuageFloat64(b *testing.B) {
    17  	g := NewGaugeFloat64()
    18  	b.ResetTimer()
    19  	for i := 0; i < b.N; i++ {
    20  		g.Update(float64(i))
    21  	}
    22  }
    23  
    24  func TestGaugeFloat64(t *testing.T) {
    25  	g := NewGaugeFloat64()
    26  	g.Update(float64(47.0))
    27  	if v := g.Value(); float64(47.0) != v {
    28  		t.Errorf("g.Value(): 47.0 != %v\n", v)
    29  	}
    30  }
    31  
    32  func TestGaugeFloat64Snapshot(t *testing.T) {
    33  	g := NewGaugeFloat64()
    34  	g.Update(float64(47.0))
    35  	snapshot := g.Snapshot()
    36  	g.Update(float64(0))
    37  	if v := snapshot.Value(); float64(47.0) != v {
    38  		t.Errorf("g.Value(): 47.0 != %v\n", v)
    39  	}
    40  }
    41  
    42  func TestGetOrRegisterGaugeFloat64(t *testing.T) {
    43  	r := NewRegistry()
    44  	NewRegisteredGaugeFloat64("foo", r).Update(float64(47.0))
    45  	t.Logf("registry: %v", r)
    46  	if g := GetOrRegisterGaugeFloat64("foo", r); float64(47.0) != g.Value() {
    47  		t.Fatal(g)
    48  	}
    49  }
    50  
    51  func TestFunctionalGaugeFloat64(t *testing.T) {
    52  	var counter float64
    53  	fg := NewFunctionalGaugeFloat64(func() float64 {
    54  		counter++
    55  		return counter
    56  	})
    57  	fg.Value()
    58  	fg.Value()
    59  	if counter != 2 {
    60  		t.Error("counter != 2")
    61  	}
    62  }
    63  
    64  func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) {
    65  	r := NewRegistry()
    66  	NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 })
    67  	if g := GetOrRegisterGaugeFloat64("foo", r); 47 != g.Value() {
    68  		t.Fatal(g)
    69  	}
    70  }
    71