github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/metrics/gauge_float64_test.go (about) 1 package metrics 2 3 import ( 4 "sync" 5 "testing" 6 ) 7 8 func BenchmarkGaugeFloat64(b *testing.B) { 9 g := NewGaugeFloat64() 10 b.ResetTimer() 11 for i := 0; i < b.N; i++ { 12 g.Update(float64(i)) 13 } 14 } 15 16 func BenchmarkGaugeFloat64Parallel(b *testing.B) { 17 c := NewGaugeFloat64() 18 var wg sync.WaitGroup 19 for i := 0; i < 10; i++ { 20 wg.Add(1) 21 go func() { 22 for i := 0; i < b.N; i++ { 23 c.Update(float64(i)) 24 } 25 wg.Done() 26 }() 27 } 28 wg.Wait() 29 if have, want := c.Snapshot().Value(), float64(b.N-1); have != want { 30 b.Fatalf("have %f want %f", have, want) 31 } 32 } 33 34 func TestGaugeFloat64Snapshot(t *testing.T) { 35 g := NewGaugeFloat64() 36 g.Update(47.0) 37 snapshot := g.Snapshot() 38 g.Update(float64(0)) 39 if v := snapshot.Value(); v != 47.0 { 40 t.Errorf("g.Value(): 47.0 != %v\n", v) 41 } 42 } 43 44 func TestGetOrRegisterGaugeFloat64(t *testing.T) { 45 r := NewRegistry() 46 NewRegisteredGaugeFloat64("foo", r).Update(47.0) 47 t.Logf("registry: %v", r) 48 if g := GetOrRegisterGaugeFloat64("foo", r).Snapshot(); g.Value() != 47.0 { 49 t.Fatal(g) 50 } 51 }