github.com/theQRL/go-zond@v0.1.1/metrics/metrics_test.go (about) 1 package metrics 2 3 import ( 4 "fmt" 5 "sync" 6 "testing" 7 "time" 8 ) 9 10 const FANOUT = 128 11 12 func TestReadRuntimeValues(t *testing.T) { 13 var v runtimeStats 14 readRuntimeStats(&v) 15 t.Logf("%+v", v) 16 } 17 18 func BenchmarkMetrics(b *testing.B) { 19 r := NewRegistry() 20 c := NewRegisteredCounter("counter", r) 21 cf := NewRegisteredCounterFloat64("counterfloat64", r) 22 g := NewRegisteredGauge("gauge", r) 23 gf := NewRegisteredGaugeFloat64("gaugefloat64", r) 24 h := NewRegisteredHistogram("histogram", r, NewUniformSample(100)) 25 m := NewRegisteredMeter("meter", r) 26 t := NewRegisteredTimer("timer", r) 27 RegisterDebugGCStats(r) 28 b.ResetTimer() 29 ch := make(chan bool) 30 31 wgD := &sync.WaitGroup{} 32 /* 33 wgD.Add(1) 34 go func() { 35 defer wgD.Done() 36 //log.Println("go CaptureDebugGCStats") 37 for { 38 select { 39 case <-ch: 40 //log.Println("done CaptureDebugGCStats") 41 return 42 default: 43 CaptureDebugGCStatsOnce(r) 44 } 45 } 46 }() 47 //*/ 48 49 wgW := &sync.WaitGroup{} 50 /* 51 wgW.Add(1) 52 go func() { 53 defer wgW.Done() 54 //log.Println("go Write") 55 for { 56 select { 57 case <-ch: 58 //log.Println("done Write") 59 return 60 default: 61 WriteOnce(r, io.Discard) 62 } 63 } 64 }() 65 //*/ 66 67 wg := &sync.WaitGroup{} 68 wg.Add(FANOUT) 69 for i := 0; i < FANOUT; i++ { 70 go func(i int) { 71 defer wg.Done() 72 //log.Println("go", i) 73 for i := 0; i < b.N; i++ { 74 c.Inc(1) 75 cf.Inc(1.0) 76 g.Update(int64(i)) 77 gf.Update(float64(i)) 78 h.Update(int64(i)) 79 m.Mark(1) 80 t.Update(1) 81 } 82 //log.Println("done", i) 83 }(i) 84 } 85 wg.Wait() 86 close(ch) 87 wgD.Wait() 88 wgW.Wait() 89 } 90 91 func Example() { 92 c := NewCounter() 93 Register("money", c) 94 c.Inc(17) 95 96 // Threadsafe registration 97 t := GetOrRegisterTimer("db.get.latency", nil) 98 t.Time(func() { time.Sleep(10 * time.Millisecond) }) 99 t.Update(1) 100 101 fmt.Println(c.Snapshot().Count()) 102 fmt.Println(t.Snapshot().Min()) 103 // Output: 17 104 // 1 105 }