gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/metrics_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 ( 20 "fmt" 21 "io/ioutil" 22 "log" 23 "sync" 24 "testing" 25 "time" 26 ) 27 28 const FANOUT = 128 29 30 // Stop the compiler from complaining during debugging. 31 var ( 32 _ = ioutil.Discard 33 _ = log.LstdFlags 34 ) 35 36 func BenchmarkMetrics(b *testing.B) { 37 r := NewRegistry() 38 c := NewRegisteredCounter("counter", r) 39 g := NewRegisteredGauge("gauge", r) 40 gf := NewRegisteredGaugeFloat64("gaugefloat64", r) 41 h := NewRegisteredHistogram("histogram", r, NewUniformSample(100)) 42 m := NewRegisteredMeter("meter", r) 43 t := NewRegisteredTimer("timer", r) 44 RegisterDebugGCStats(r) 45 RegisterRuntimeMemStats(r) 46 b.ResetTimer() 47 ch := make(chan bool) 48 49 wgD := &sync.WaitGroup{} 50 /* 51 wgD.Add(1) 52 go func() { 53 defer wgD.Done() 54 //log.Println("go CaptureDebugGCStats") 55 for { 56 select { 57 case <-ch: 58 //log.Println("done CaptureDebugGCStats") 59 return 60 default: 61 CaptureDebugGCStatsOnce(r) 62 } 63 } 64 }() 65 //*/ 66 67 wgR := &sync.WaitGroup{} 68 //* 69 wgR.Add(1) 70 go func() { 71 defer wgR.Done() 72 //log.Println("go CaptureRuntimeMemStats") 73 for { 74 select { 75 case <-ch: 76 //log.Println("done CaptureRuntimeMemStats") 77 return 78 default: 79 CaptureRuntimeMemStatsOnce(r) 80 } 81 } 82 }() 83 //*/ 84 85 wgW := &sync.WaitGroup{} 86 /* 87 wgW.Add(1) 88 go func() { 89 defer wgW.Done() 90 //log.Println("go Write") 91 for { 92 select { 93 case <-ch: 94 //log.Println("done Write") 95 return 96 default: 97 WriteOnce(r, ioutil.Discard) 98 } 99 } 100 }() 101 //*/ 102 103 wg := &sync.WaitGroup{} 104 wg.Add(FANOUT) 105 for i := 0; i < FANOUT; i++ { 106 go func(i int) { 107 defer wg.Done() 108 //log.Println("go", i) 109 for i := 0; i < b.N; i++ { 110 c.Inc(1) 111 g.Update(int64(i)) 112 gf.Update(float64(i)) 113 h.Update(int64(i)) 114 m.Mark(1) 115 t.Update(1) 116 } 117 //log.Println("done", i) 118 }(i) 119 } 120 wg.Wait() 121 close(ch) 122 wgD.Wait() 123 wgR.Wait() 124 wgW.Wait() 125 } 126 127 func Example() { 128 c := NewCounter() 129 Register("money", c) 130 c.Inc(17) 131 132 // Threadsafe registration 133 t := GetOrRegisterTimer("db.get.latency", nil) 134 t.Time(func() { 135 time.Sleep(time.Second) 136 }) 137 138 t.Update(1) 139 140 fmt.Println(c.Count()) 141 fmt.Println(t.Min()) 142 // Output: 17 143 // 1 144 }