github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/metrics/metrics_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package metrics 19 20 import ( 21 "fmt" 22 "io/ioutil" 23 "log" 24 "sync" 25 "testing" 26 "time" 27 ) 28 29 const FANOUT = 128 30 31 // Stop the compiler from complaining during debugging. 32 var ( 33 _ = ioutil.Discard 34 _ = log.LstdFlags 35 ) 36 37 func BenchmarkMetrics(b *testing.B) { 38 r := NewRegistry() 39 c := NewRegisteredCounter("counter", r) 40 g := NewRegisteredGauge("gauge", r) 41 gf := NewRegisteredGaugeFloat64("gaugefloat64", r) 42 h := NewRegisteredHistogram("histogram", r, NewUniformSample(100)) 43 m := NewRegisteredMeter("meter", r) 44 t := NewRegisteredTimer("timer", r) 45 RegisterDebugGCStats(r) 46 RegisterRuntimeMemStats(r) 47 b.ResetTimer() 48 ch := make(chan bool) 49 50 wgD := &sync.WaitGroup{} 51 /* 52 wgD.Add(1) 53 go func() { 54 defer wgD.Done() 55 //log.Println("go CaptureDebugGCStats") 56 for { 57 select { 58 case <-ch: 59 //log.Println("done CaptureDebugGCStats") 60 return 61 default: 62 CaptureDebugGCStatsOnce(r) 63 } 64 } 65 }() 66 //*/ 67 68 wgR := &sync.WaitGroup{} 69 //* 70 wgR.Add(1) 71 go func() { 72 defer wgR.Done() 73 //log.Println("go CaptureRuntimeMemStats") 74 for { 75 select { 76 case <-ch: 77 //log.Println("done CaptureRuntimeMemStats") 78 return 79 default: 80 CaptureRuntimeMemStatsOnce(r) 81 } 82 } 83 }() 84 //*/ 85 86 wgW := &sync.WaitGroup{} 87 /* 88 wgW.Add(1) 89 go func() { 90 defer wgW.Done() 91 //log.Println("go Write") 92 for { 93 select { 94 case <-ch: 95 //log.Println("done Write") 96 return 97 default: 98 WriteOnce(r, ioutil.Discard) 99 } 100 } 101 }() 102 //*/ 103 104 wg := &sync.WaitGroup{} 105 wg.Add(FANOUT) 106 for i := 0; i < FANOUT; i++ { 107 go func(i int) { 108 defer wg.Done() 109 //log.Println("go", i) 110 for i := 0; i < b.N; i++ { 111 c.Inc(1) 112 g.Update(int64(i)) 113 gf.Update(float64(i)) 114 h.Update(int64(i)) 115 m.Mark(1) 116 t.Update(1) 117 } 118 //log.Println("done", i) 119 }(i) 120 } 121 wg.Wait() 122 close(ch) 123 wgD.Wait() 124 wgR.Wait() 125 wgW.Wait() 126 } 127 128 func Example() { 129 c := NewCounter() 130 Register("money", c) 131 c.Inc(17) 132 133 // Threadsafe registration 134 t := GetOrRegisterTimer("db.get.latency", nil) 135 t.Time(func() { time.Sleep(10 * time.Millisecond) }) 136 t.Update(1) 137 138 fmt.Println(c.Count()) 139 fmt.Println(t.Min()) 140 // Output: 17 141 // 1 142 }