github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/metrics/gauge_float64_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 package metrics 10 11 import "testing" 12 13 func BenchmarkGuageFloat64(b *testing.B) { 14 g := NewGaugeFloat64() 15 b.ResetTimer() 16 for i := 0; i < b.N; i++ { 17 g.Update(float64(i)) 18 } 19 } 20 21 func TestGaugeFloat64(t *testing.T) { 22 g := NewGaugeFloat64() 23 g.Update(float64(47.0)) 24 if v := g.Value(); float64(47.0) != v { 25 t.Errorf("g.Value(): 47.0 != %v\n", v) 26 } 27 } 28 29 func TestGaugeFloat64Snapshot(t *testing.T) { 30 g := NewGaugeFloat64() 31 g.Update(float64(47.0)) 32 snapshot := g.Snapshot() 33 g.Update(float64(0)) 34 if v := snapshot.Value(); float64(47.0) != v { 35 t.Errorf("g.Value(): 47.0 != %v\n", v) 36 } 37 } 38 39 func TestGetOrRegisterGaugeFloat64(t *testing.T) { 40 r := NewRegistry() 41 NewRegisteredGaugeFloat64("foo", r).Update(float64(47.0)) 42 t.Logf("registry: %v", r) 43 if g := GetOrRegisterGaugeFloat64("foo", r); float64(47.0) != g.Value() { 44 t.Fatal(g) 45 } 46 } 47 48 func TestFunctionalGaugeFloat64(t *testing.T) { 49 var counter float64 50 fg := NewFunctionalGaugeFloat64(func() float64 { 51 counter++ 52 return counter 53 }) 54 fg.Value() 55 fg.Value() 56 if counter != 2 { 57 t.Error("counter != 2") 58 } 59 } 60 61 func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) { 62 r := NewRegistry() 63 NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 }) 64 if g := GetOrRegisterGaugeFloat64("foo", r); 47 != g.Value() { 65 t.Fatal(g) 66 } 67 }