gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/gauge_float64_test.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The aquachain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package metrics 18 19 import "testing" 20 21 func BenchmarkGuageFloat64(b *testing.B) { 22 g := NewGaugeFloat64() 23 b.ResetTimer() 24 for i := 0; i < b.N; i++ { 25 g.Update(float64(i)) 26 } 27 } 28 29 func TestGaugeFloat64(t *testing.T) { 30 g := NewGaugeFloat64() 31 g.Update(float64(47.0)) 32 if v := g.Value(); float64(47.0) != v { 33 t.Errorf("g.Value(): 47.0 != %v\n", v) 34 } 35 } 36 37 func TestGaugeFloat64Snapshot(t *testing.T) { 38 g := NewGaugeFloat64() 39 g.Update(float64(47.0)) 40 snapshot := g.Snapshot() 41 g.Update(float64(0)) 42 if v := snapshot.Value(); float64(47.0) != v { 43 t.Errorf("g.Value(): 47.0 != %v\n", v) 44 } 45 } 46 47 func TestGetOrRegisterGaugeFloat64(t *testing.T) { 48 r := NewRegistry() 49 NewRegisteredGaugeFloat64("foo", r).Update(float64(47.0)) 50 t.Logf("registry: %v", r) 51 if g := GetOrRegisterGaugeFloat64("foo", r); float64(47.0) != g.Value() { 52 t.Fatal(g) 53 } 54 } 55 56 func TestFunctionalGaugeFloat64(t *testing.T) { 57 var counter float64 58 fg := NewFunctionalGaugeFloat64(func() float64 { 59 counter++ 60 return counter 61 }) 62 fg.Value() 63 fg.Value() 64 if counter != 2 { 65 t.Error("counter != 2") 66 } 67 } 68 69 func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) { 70 r := NewRegistry() 71 NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 }) 72 if g := GetOrRegisterGaugeFloat64("foo", r); 47 != g.Value() { 73 t.Fatal(g) 74 } 75 }