github.com/aquanetwork/aquachain@v1.7.8/common/metrics/metrics_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"sync"
     8  	"testing"
     9  )
    10  
    11  const FANOUT = 128
    12  
    13  // Stop the compiler from complaining during debugging.
    14  var (
    15  	_ = ioutil.Discard
    16  	_ = log.LstdFlags
    17  )
    18  
    19  func BenchmarkMetrics(b *testing.B) {
    20  	r := NewRegistry()
    21  	c := NewRegisteredCounter("counter", 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  	RegisterRuntimeMemStats(r)
    29  	b.ResetTimer()
    30  	ch := make(chan bool)
    31  
    32  	wgD := &sync.WaitGroup{}
    33  	/*
    34  		wgD.Add(1)
    35  		go func() {
    36  			defer wgD.Done()
    37  			//log.Println("go CaptureDebugGCStats")
    38  			for {
    39  				select {
    40  				case <-ch:
    41  					//log.Println("done CaptureDebugGCStats")
    42  					return
    43  				default:
    44  					CaptureDebugGCStatsOnce(r)
    45  				}
    46  			}
    47  		}()
    48  	//*/
    49  
    50  	wgR := &sync.WaitGroup{}
    51  	//*
    52  	wgR.Add(1)
    53  	go func() {
    54  		defer wgR.Done()
    55  		//log.Println("go CaptureRuntimeMemStats")
    56  		for {
    57  			select {
    58  			case <-ch:
    59  				//log.Println("done CaptureRuntimeMemStats")
    60  				return
    61  			default:
    62  				CaptureRuntimeMemStatsOnce(r)
    63  			}
    64  		}
    65  	}()
    66  	//*/
    67  
    68  	wgW := &sync.WaitGroup{}
    69  	/*
    70  		wgW.Add(1)
    71  		go func() {
    72  			defer wgW.Done()
    73  			//log.Println("go Write")
    74  			for {
    75  				select {
    76  				case <-ch:
    77  					//log.Println("done Write")
    78  					return
    79  				default:
    80  					WriteOnce(r, ioutil.Discard)
    81  				}
    82  			}
    83  		}()
    84  	//*/
    85  
    86  	wg := &sync.WaitGroup{}
    87  	wg.Add(FANOUT)
    88  	for i := 0; i < FANOUT; i++ {
    89  		go func(i int) {
    90  			defer wg.Done()
    91  			//log.Println("go", i)
    92  			for i := 0; i < b.N; i++ {
    93  				c.Inc(1)
    94  				g.Update(int64(i))
    95  				gf.Update(float64(i))
    96  				h.Update(int64(i))
    97  				m.Mark(1)
    98  				t.Update(1)
    99  			}
   100  			//log.Println("done", i)
   101  		}(i)
   102  	}
   103  	wg.Wait()
   104  	close(ch)
   105  	wgD.Wait()
   106  	wgR.Wait()
   107  	wgW.Wait()
   108  }
   109  
   110  func Example() {
   111  	c := NewCounter()
   112  	Register("money", c)
   113  	c.Inc(17)
   114  
   115  	// Threadsafe registration
   116  	t := GetOrRegisterTimer("db.get.latency", nil)
   117  	t.Time(func() {})
   118  	t.Update(1)
   119  
   120  	fmt.Println(c.Count())
   121  	fmt.Println(t.Min())
   122  	// Output: 17
   123  	// 1
   124  }